Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5945941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:45:08+00:00 2026-05-22T16:45:08+00:00

i am working on an applet with around ten different datasources(e.g. statistics/error-log/…). Each datasource

  • 0

i am working on an applet with around ten different datasources(e.g. statistics/error-log/…). Each datasource is updated by a single network connection and reports updates via the observer mechanism. The applet has different views which display parts of the data. Every view is only interested in some parts of the data and registers itself as an Observer at the necessary Observables.

The views(extended JPanels) mostly consist of standard swing components (e.g. JLabels, JButton, …). Some attributes of the components in the views depend 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() method of the StatisticPanel and the update() methods just calls repaint(), because I didn’t want the manipulate the components outside of the EDT.

Is this the intended way of updating swing components in a multi-threaded environment? Is it better to use a Runnable with SwingUtitlies.invokeLater()? Are there better approaches for this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:45:08+00:00Added an answer on May 22, 2026 at 4:45 pm

    I second camickr’s recommendations, but regarding this code snippet:

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

    You have non-painting methods in your paintComponent method (the first two methods), and that shouldn’t be as 1) you want this method to be as lean and fast as possible and thus have only painting-related code, and 2) you do not have aboslute control of when this method is called or even if it is called, and so non-painting related code and program logic does not belong in there. For these reasons, I strongly urge you to get them out of there, but instead should be called separate from paintComponent, but as with most Swing code, on the EDT.

    EDIT 1
    I’m not a professional, but how about if you gave your StaticPanel a method similar to this:

       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.
       }
    

    EDIT 2
    Also, please have a look at this thread: check-if-thread-is-edt-is-necessary

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Java applet that needs to display fancy equations. Is there
I am working with a vendor applet, The applet works fine with JRE 1.4.2_X
I'm working on a project where we're using a Java applet for part of
I am currently working on an image processing demonstration in java (Applet). I am
I'm currently working on an applet that works with Fourier's series. It doesn't really
I'm working on a runnable java applet that has a fill feature much like
the applet(which make connection to access database) i developed is working fine in applet
I'm working on an Applet, which has a JButton that I want to use
I'm currently working on an applet that displays a heap as values are added
Working on Mac OS X 10.6.2, Intel, with i686-apple-darwin10-g++-4.2.1, and compiling with the -arch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.