• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

labels on swing components

 
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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • 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.
 
Quick! Before anybody notices! Cover it up with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic