Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

unwanted echo of JTextField

 
Greenhorn
Posts: 5
1
  • 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.
 
Bartender
Posts: 3648
16
Mac OS X Firefox Browser Java
  • 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: 5
1
  • 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.
 
Rancher
Posts: 3073
24
  • 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.

 
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
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!