Java: second window is empty

I have a strange problem ... I am a relatively new "enthusiast" Java programmer (I used to hack Perl in my previous career) while working on my first semi-real application. The "main class" is the MyApp class, which creates an instance of UserInputDialog .

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

Everything works perfectly. But now I'm trying to add a progress window to MyApp , since doSomething() can take a sufficient amount of time from time to time.

I created a ProgressWindow class that extends a JFrame , uses a BorderLayout and sends a JProgressBar in. NORTH and a JScrollPane (wrapping a JTextArea ) in. CENTER , ProgressWindow works great when creating an instance of ProgressWindowTester and passes test data. It also works great if I copy and paste the test for loops from ProgressWindowTester to MyApp and does not have MyApp instantiate UserInputDialog ( UserInputDialog is inherent to MyApp causing this behavior; this seems to be some kind of interaction that I don't understand , between UserInputDialog and ProgressWindow ).

But when I try to use ProgressWindow in MyApp as intended, i.e. ProgressWindow setVisible (true), I get an empty Swing window (of the appropriate size and the correct title bar). JProgressBar and JScrollPane / JTextArea not displayed. The ProgressWindow methods are called correctly by MyApp ( System.err.println() messages show the correct interaction), everything works fine, just the components that should be visible in ProgressWindow ... are not.

I can post code snippets, but this is a bit confusing, and I will probably just miss something obvious ...

I am familiar with the concept of separating the user interface and the business logic as a whole (for example, I used HTML::Template and Class::DBI and CGI::Application when creating Perl applications), but I'm not sure that I am β€œdoing it right” in Java ...

Thanks in advance!

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

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

+4
source share
1 answer

You have a concurrency issue in Swing where you are trying to execute a lengthy process in a Swing or EDT event stream. Since this thread is responsible for drawing all components and processing user input, if it is bound by your lengthy process, your GUI will be effectively frozen until the process is complete. The key is to use a background thread, such as provided by SwingWorker for lengthy processes, so the event flow is not blocked. Learn more about this Concurrency in Swing . Also check out the JProgressBar tutorial for more information on how to use progress bars with background threads.

Also, you won't want to use a JFrame where a dialog like JDialog is much more appropriate. In addition, you will want to avoid using KeyListeners with Swing. It would be much better to just add an ActionListener to your JTextField, since its default behavior is to respond to keystrokes.

Oh, and welcome to StackOverflow.com!

+5
source

Source: https://habr.com/ru/post/1394704/


All Articles