Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

unwanted echo of JTextField  RSS feed

 
Jack Marsh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
K. Tsang
Bartender
Posts: 3624
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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?

 
Jack Marsh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang

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.
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!