4

I have a weird issue... I'm a relatively new "enthusiast" Java programmer (I used to make my living hacking Perl, in a previous career), working on my first semi-real application. "Main-Class" is the MyApp class, which creates a UserInputDialog instance.

UserInputDialog is a class I wrote that extends JFrame, implements ActionListener and KeyListener, uses FlowLayout, and presents the user with a JLabel, JTextField, and Cancel/OK JButtons. When the JTextField generates a KeyEvent where keyReleased() == KeyEvent.VK_ENTER, or when the "OK" JButton generates an ActionEvent, UserInputDialog does some input validation, calls setVisible(false), and then calls MyApp.doSomething( JTextFieldInstance.getText() ).

That all works perfectly. But now I'm trying to add a progress window to MyApp, as doSomething() can occasionally take a fair amount of time to complete.

I created the ProgressWindow class, which extends JFrame, uses BorderLayout, and tosses a JProgressBar in .NORTH and a JScrollPane (wrapping a JTextArea) in .CENTER. ProgressWindow works perfectly when instantiated from ProgressWindowTester and fed test data. It also works fine if I copy-and-paste the test for loops from ProgressWindowTester into MyApp and don't have MyApp instantiate UserInputDialog (i.e., there's nothing inherent in MyApp that's causing this behavior; it seems to be some sort of interaction I'm not understanding, between UserInputDialog and ProgressWindow).

But when I try to use ProgressWindow in MyApp as intended, i.e., ProgressWindow setVisible(true), I get a blank Swing window (of the proper size, and with the title bar set properly). The JProgressBar and JScrollPane / JTextArea components don't appear. The ProgressWindow methods are being called by MyApp properly (System.err.println() messages show proper interaction), everything appears to be working fine, just, the components that should be visible in ProgressWindow ... aren't.

I can post code snippets, but it's kind of convoluted, and I'm probably just missing something obvious...

I'm familiar with the concept of separating UI and business logic generally (e.g., I used HTML::Template and Class::DBI and CGI::Application when building Perl applications), but I'm not sure I'm "doing it right" in Java...

Thanks in advance!

Oh, I get exactly the same behavior on the two environments I've tried the code in: javac 1.6.0_29 on Mac OS X 10.6.8 ("Snow Leopard"); and javac 1.7.0_02[1] on the Fedora 15 Linux distribution, kernel 2.6.31.10-3, LXDE desktop environment.

[1] Downloaded directly from oracle.com; I;m not using OpenJDK (I know JDK 7 is based on OpenJDK) or gcj or anything like that

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    Hard to tell without code, but could be a Swing thread issue, especially if the long-running task is being carried out on the Swing thread: http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html – DNA Feb 03 '12 at 22:29

1 Answers1

5

You've got a concurrency in Swing issue where you're trying to do a long running process on the Swing event thread or EDT. Since this thread is responsible for drawing all components and for processing user input, if it is tied down by your long-running process, your GUI will be effectively frozen until the process is complete. The key is to use a background thread such as that provided by a SwingWorker for long-running processes, so the event thread doesn't get locked. Check out Concurrency in Swing for more information on this. Also check out the JProgressBar Tutorial for other insights on how to use progress bars with background threads.

Also, you'll not want to use a JFrame where a dialog, such as a JDialog, is much more appropriate. Also, you'll want to avoid use of KeyListeners with Swing. Much better would be to simply add an ActionListener to your JTextField since its default behavior is to respond to presses of the key.

Oh, and welcome to StackOverflow.com!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373