1

I'm building a large Java application that uses multiple pop-up windows. Some of these windows must be able to be displayed at the same time (for instance, separate pop-ups for Google Earth and a webcam feed), but some of them can only be displayed one at a time (for instance, error messages). The first kind of pop-ups, the webcam kind, are working fine. But the error message pop-ups act like the webcam type of pop-ups (that is, they create new .class files and there can be multiple error pop-ups). How do I fix this? Should I create a new error class?

Also, in one of the webcam-type pop-ups, I have JTextFields which read a username and password. This log-in popup works fine, but if I use it once, close it, and use it again, doing a getText() on the JTextFields returns an empty string. I think this problem might be related to the one above, but I am not sure.

passwordAction.addActionListener(new ActionListener() {
            JFrame pwPop=new JFrame("Log in");
            JTextField unameField;
            JTextField pwField;
            public void actionPerformed(ActionEvent arg0) {
                pwPop.setBounds(250,200,300,150);
                JPanel pwPopPanel=new JPanel(new FlowLayout(FlowLayout.LEFT,10,10));
                pwPop.add(pwPopPanel);
                unameField=new JTextField();
                pwField=new JTextField();
                JButton logInButton=new JButton("Log in");
                JButton cancelButton=new JButton("Cancel");
                JLabel logInText=new JLabel("Username:");
                JLabel passwordText=new JLabel("Password:");
                JPanel buttonPanel=new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
                pwPopPanel.add(logInText);
                pwPopPanel.add(unameField);
                pwPopPanel.add(passwordText);
                pwPopPanel.add(pwField);
                buttonPanel.add(logInButton);
                buttonPanel.add(cancelButton);
                pwPopPanel.add(buttonPanel);
                logInButton.addActionListener(new ActionListener() {
                    JFrame logErrorFrame;
                    public void actionPerformed(ActionEvent arg0) {
                        if (doLogIn(unameField.getText(), pwField.getText(), "")) {
                            unameField.setText(null);
                            pwField.setText(null);
                            pwPop.setVisible(false);
                        }
                    }
                });
                pwPop.setVisible(true);
            }
        });
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • Could you use `JOptionPane.showMessageDialog` or is the thread blocking a problem? – Michael Jun 11 '12 at 01:55
  • Yes I believe I can use that, I was just not aware that it existed. Thank you very much! This will work for my log-in pop-up as well, correct? – Cameron Lattz Jun 11 '12 at 02:01
  • 1
    *"work for my log-in pop-up as well..?"* Sure, see [this answer](http://stackoverflow.com/questions/10773132/how-to-unfocus-a-jtextfield/10773412#10773412). – Andrew Thompson Jun 11 '12 at 07:28

1 Answers1

0

As Andrew Thompson showed me, JOptionPane is the solution I was looking for. It is capable of creating almost any type of popup you may need.

JOptionPane Javadoc

Simple Dialog with JOptionPane