1

I work in HPC. I have both an applet and a Java client application that display information about jobs (jobID, jobname, userID, etc.) running on the cluster via information received from our TORQUE job scheduler.

Recently, I added buttons to sort the data by category by ascending or descending values. With this addition, where the user has the ability to force an update of the display by sorting the data (previously I had a timer that received new data from the server every 10 seconds and subsequently updated the display), I noticed a disparity in performance - the applet lagged significantly. Putting in timing code, I found that the application took an average of 0.05 seconds to setContentPane() whereas the applet took an average of 1.50 seconds to perform the same operation with the same code. This issue is the same whether the applet is being run in appletviewer or a browser.

I want to emphasize that virtually all of the code is the same. The only significant difference is that the JApplet makes setContentPane() (and other) calls on itself, whereas my application makes these calls on the JFrame (e.g. frame.setContentPane() ).

Curiously, the first call to setContentPane() for the applet returns in ~0.13 seconds. However, all subsequent calls require the previously noted time.

Any suggestions? I'd much rather have a functioning applet so I don't have to push the application on my user community.

Edit: JApplet's event handling is not the issue: performance regarding mouse movement and position reading as well as popup drawing on the glass pane is identical between the JApplet and the Java application.

Edit2: I just edited the JApplet code so a JFrame pops up with the application running inside and the performance matches that of the application! What is it with Applets? I've been reading about them for weeks and no one has shed light on this problem - It's almost exclusively "applets don't run in my firefox browser on my linux box" or "how do I turn my application into an applet." No one can tell me why the same GUI code that is literally identical will run ridiculously slow inside a JApplet. That is, why does setContentPane() have a 30x slowdown in a JApplet created from a JFrame???

The reason I'm running an applet is because i want it embedded in a web page, the pop-up JFrame is too intrusive. Thoughts?

Edit3: In continuing my diagnostics of this problem, I found that a call to setContentPane() by the JApplet spikes my CPU usage up to anywhere from 60-100%. This is not the case for the application's setContentPane() which will only jump from 0.1% to ~4%. What is causing all of the operational and performance-taxing overhead for the JApplet?

  • You could also try `appletviewer`. – trashgod Jul 14 '12 at 19:30
  • "As a convenience `add` and its variants…have been overridden to forward to the `contentPane` as necessary."—[`JFrame`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html) – trashgod Jul 14 '12 at 19:30

3 Answers3

3

Refactor your application to use this hybrid approach. It offers two advantages:

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I am already using a SwingWorker thread to create the new display panel, so the slowdown for that operation (typically less than 0.01 seconds) is insignificant. – user1524486 Jul 14 '12 at 17:53
  • Swing GUI objects should be constructed and manipulated _only_ on the [event dispatch thread](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Jul 14 '12 at 19:28
  • Why was `SwingWorker` mentioned? We all guessed you were using it in both app. & applet. – Andrew Thompson Jul 14 '12 at 22:42
  • `SwingWorker` was a surprise to me, too. GUI components should _not_ be constructed on a worker's `doInBackground()` thread. The consequences cannot be predicted. – trashgod Jul 15 '12 at 00:44
3

Curiously, the first call to setContentPane() for the applet returns in ~0.13 seconds. However, .. Any suggestions?

In that first call, add a panel with a CardLayout. Never call setContentPane() again, but instead use the panel with card layout to 'add' anything more.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • This may work. Though I need a glass pane for some of the functionality. I'm not sure how to do this without adding a window to a container (not allowed). – user1524486 Jul 15 '12 at 20:58
  • 1
    Let us know how you go. If you cannot manage it, post an [SSCCE](http://sscce.org/) of your best attempt. – Andrew Thompson Jul 15 '12 at 23:08
1

Unfortunately, Applets will run slower by comparison. A lot depends upon the browser's JVM and response to Applet requests. It's one of the reasons that they're used so infrequently.

Andreas
  • 115
  • 2
  • 11