2

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); and revalidate() and repaint(); the panel on form is not affected. I also created new instance of thiledBorder 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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
sajad
  • 2,094
  • 11
  • 32
  • 52
  • On which component did you call `revalidate()` and `repaint()`? Can I suggest you adjust your code into an [SSCCE](http://sscce.org/) so that others may experiment? – Duncan Jones Apr 08 '13 at 12:49

2 Answers2

6

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
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 1
    +1, for noting the difference between revalidate() and repaint(). Since the size of the component doesn't change you can just repaint() it. – camickr Apr 08 '13 at 15:25
1

If you know your panel has a titled border you can just do this:

    TitledBorder titledBorder = (TitledBorder)jPanel1.getBorder();
    titledBorder.setTitleColor(Color.red);
Dave Marsh
  • 11
  • 2