Delay title Title Title / refresh, why?

I have JPanelA with a header border inside JPanelB a JTabbedPanelC. I have a method that updates the contents of A and B, which is called from time to time.

Unfortunately, all elements A and B are updated in time, but not the heading A. I obviously have to switch to another tabbed panel and return to the heading C for the heading that will display correctly. Why?

The code I use is as follows:

    TitledBorder tmp
            = (TitledBorder) this.GroupingProfilePanel.getBorder();

    // Resetting header
    if ( this.c != null ) {
        tmp.setTitle("Set - " + this.c.getName());
    } else {
        tmp.setTitle("Set");
    }
+5
source share
1 answer

After updating the header, make sure you call repaint()on a component that has a title border or one of its ancestors.

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.Date;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;

/** @see http://stackoverflow.com/questions/6566612 */
public class TitledBorderTest {

     public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new TitledBorderTest().create();
              }
          });
     }

     private void create() {

         String s = "This is a TitledBorder update test.";
         final JLabel label = new JLabel(s);
         final TitledBorder tb =
             BorderFactory.createTitledBorder(new Date().toString());
         label.setBorder(tb);
         JFrame f = new JFrame("Titled Border Test");
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.add(label);
         f.add(new JButton(new AbstractAction("Update") {

            public void actionPerformed(ActionEvent e) {
                tb.setTitle(new Date().toString());
                label.repaint();
            }
        }), BorderLayout.SOUTH);
         f.pack();
         f.setLocationRelativeTo(null);
         f.setVisible(true);
         System.out.println(tb.getMinimumSize(f));
     }
}
+9

All Articles