0

I have two custom text fields: RoundTextField and RoundPasswordField. They extend JTextField and JPasswordField respectively. But they both have the exact code except the RoundPasswordField has EchoChar and functionality to handle ShowPassword checkbox. I find myself writing the same code for both of them because RoundPasswordField can't extend RoundTextField because JTextField doesn't have EchoChar. Is there any workaround?

I think extending RoundPasswordField to RoundTextField and setting the EchoChar to /u000 might be a solution but it sounds wrong semantically. So is there any cleaner solution?

  • For a small use case, it may just be easier to just double up the code, for larger use cases you can use tools that duplicate/inject the code for you at compile time. Otherwise, as you suspect it's a bit of a messy business and may require a rethink on how your classes are arranged and how methods are segmented. If you hadn't seen it already, take a look at the official Java tutorial covering the core principles: https://docs.oracle.com/javase/tutorial/java/IandI/index.html and see here: https://stackoverflow.com/questions/5836662/extending-from-two-classes – sorifiend May 17 '22 at 02:32

0 Answers0