Repainting in a multithreaded environment

I am working on an applet with about ten different data sources (e.g. statistics / error log / ...). Each data source is updated with a single network connection and reports updates through a monitoring mechanism. An applet has different views displaying pieces of data. Each point of view is only interested in certain parts of the data and is registered as an Observer in the necessary Observables.

Views (extended JPanels) mainly consist of standard swing components (e.g. JLabels, JButton, ...). Some component attributes in views are dependent on information from the underlying data model.

Example:

StatisticPanel::paintComponent(Graphics g) {
  clearStatisticButton.setEnabled(stat.hasEntries());
  minValueLabel.setText(stat.getMinValue());
  super.paintComponent(g);
}

This logic is implemented in the paintComponent()StatisticPanel method , and the methods update()simply calls repaint (), because I did not want to manipulate the components outside the EDT.

Is this the intended way to update swing components in a multi-threaded environment? Is it better to use runnable with SwingUtitlies.invokeLater()? Are there more effective approaches to this problem?

+3
source share
2 answers

Second recommendations on camickr, but regarding this piece of code:

StatisticPanel::paintComponent(Graphics g) {
  clearStatisticButton.setEnabled(stat.hasEntries());
  minValueLabel.setText(stat.getMinValue());
  super.paintComponent(g);
}

paintComponent ( ) , , 1) , , , , , 2) aboslute, , . , paintComponent, Swing-, EDT.

1
, , StaticPanel , :

   public void doMyUpdate() {
      if (SwingUtilities.isEventDispatchThread()) {
         clearStatisticButton.setEnabled(stat.hasEntries());
         minValueLabel.setText(stat.getMinValue());
      } else {
         SwingUtilities.invokeLater(new Runnable() {
            public void run() {
               clearStatisticButton.setEnabled(stat.hasEntries());
               minValueLabel.setText(stat.getMinValue());
            }
         });
      }
      repaint(); // if the paintComponent method has more than just a super call.
   }

2
, , : check-if-thread-is-edt-is-necessary

+6

repaint() Swing RepaintManger, , , , , repaint . RepaintManager , EDT.

+4

All Articles