change TitledBorder color dynamically in java
15,171
Solution 1
You don't state how titledborder
is assigned but this is how it would work:
TitledBorder titledBorder = BorderFactory.createTitledBorder(...);
panel.setBorder(titledBorder);
then at runtime:
titledBorder.setTitleColor(theColor);
repaint(); // revalidate not necessry
Solution 2
If you know your panel has a titled border you can just do this:
TitledBorder titledBorder = (TitledBorder)jPanel1.getBorder();
titledBorder.setTitleColor(Color.red);
Author by
sajad
Updated on June 04, 2022Comments
-
sajad almost 2 years
I have created a TitledBorder and set it to a JPanel.
JPanel panel = new JPanel(); panel.setBorder(javax.swing.BorderFactory. createTitledBorder(null, "title", javax.swing.border. TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border. TitledBorder.DEFAULT_POSITION, null, java.awt.Color.red));
now I want to change the color of the title text of the border; and if possible border lines. I see when I change color of the border by the method
titledborder.setTitleColor(theColor);
andrevalidate()
andrepaint()
; the panel on form is not affected. I also created new instance ofthiledBorder
and assign it to the panel; but not effective. Is it necessary to renew the panel, and then set it new a border instance? thank you -
camickr about 11 years+1, for noting the difference between revalidate() and repaint(). Since the size of the component doesn't change you can just repaint() it.