I am a long time lurker and in the past I have solved many problems by creating an SSCCE, but this time is different.
I recently began writing a much larger program than I will show. I have boiled the problem down to this SSCCE.
What i wanted was a JRadioButton with no label carrying around a JTextField. When I added a restore configuration feature in the real program, i found that the text destined for the JTextField was being echoed into the JRadioButton. Perhaps it is obvious but i sure don't see what I am doing wrong. I am using Java 1.7.0 for development if that makes a difference.
Given your SSCCE as is, the radio button's label gets updated when the text field loses focus
What do you want to achieve with this TextButton class (extending JRadioButton)?
Would it be simpler to just use regular JRadioButton?
What relationship is there, if any?
K. Tsang OCPJP OCMJEA SOACP PMP
posted 4 years ago
What i want is to have an editable JRadioButton. Google was of no help. The code as is ( with the 2 add statements in the constructor reversed ) works as planned if setTextField() is never called . The user can add text to the text field and the TextButton class returns that text when queried via getText(). Notice that setText is never called on the TextButton/JRadioButton. The button label that is there occurs after a call to setFieldText is made. If what you said about focus was correct, it suggests that some JComponets are very closely coupled and are not suitable for this task.
// how does letters end up in the JRadioButton text
That is what the getText() method does. It takes the text from the text field.
Notice that setText is never called on the TextButton/JRadioButton.
This doesn't matter because you overrode the default behaviour of the getText() method. So if you invoke the setText() method, that value will be ignored. The JRadioButton UI will use the value from the getText() method to paint the radio button.
They weren't very bright, but they were very, very big. Ad contrast:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop