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
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: