Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

labels on swing components  RSS feed

 
Dave Segal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't found any way to add labels to a swing component that would keep the label "attached" to the component. I've added regular text labels, but I have to positoin them correctly. If I move the component, the label must also be moved. Is there a way to add a label to a Swing component such that it "sticks" to the component?
Thanks,
Dave Segal
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way I have found to do this is to use a JPanel for every component. For istance:

Using this I can create a JPanel object with a titled border where the title is the Label I want. Then I add my Component (JTextField, JTextArea, etc) to that JPanel object. Then all I have to do is move the panel, and I move the label and the Component.


And there you have it. You could also extend this to make the CustomTitledPanel class also take in a JComponent and add the component and the label all in one method. I just chose not to do this for my own reasons.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!