简体   繁体   中英

JFrame problems

I am creating a popup JFrame that will have a message and yes/no buttons. I am using this method in 2 ways. In 1, the main program calls this method and in the other, this method is called directly after a previous JFrame is closed. This method works when being called form the main program, but when another JFrame calls it, the JFrame created in this method shows up completely blank and the GUI freezes. I cannot exit out of the JFrame, but I can move it. The freezing is a result of the Thread.yield because response is always null, but in what instances will the JFrame fail to be created properly?

Note: response is a static variable. Also when this JFrame is created by another JFrame, the original JFrame does not exit correctly. That JFrame has a JComboBox, and the selected option is frozen on the dropdown. When it does not call this method, it closes properly.

public static String confirmPropertyPurchase(String message)
    {
        response = null;
        final JFrame confirmFrame = new JFrame("Confirm");
        confirmFrame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent ev){
                response = "No";
            }
            public void windowDeactivated(WindowEvent e) {
                confirmFrame.requestFocus();
            }
        });

        final JPanel confirmPanel = new JPanel();
        final JButton yes = new JButton();
        final JButton no = new JButton();
        yes.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                response = "Yes";
                confirmFrame.setVisible(false);
            }
        });
        no.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                response = "No";
                confirmFrame.setVisible(false);
            }
        });

        final JLabel confirmLabel = new JLabel("      " + message);
        yes.setText("Yes");
        yes.setPreferredSize(new Dimension(100, 100));
        no.setText("No");
        no.setPreferredSize(new Dimension(100,100));
        confirmFrame.add(confirmLabel, BorderLayout.CENTER);
        confirmPanel.add(yes);
        confirmPanel.add(no);
        confirmFrame.add(confirmPanel, BorderLayout.AFTER_LAST_LINE);
        confirmFrame.setPreferredSize(new Dimension(520, 175
        ));

        confirmFrame.pack();
        confirmFrame.setVisible(true);

        while(response == null)
        {
            Thread.yield();
        }
        return response;
    }

Again, you shouldn't be using a JFrame as a dialog. In fact your whole bit of code can be replaced with a simple JOptionPane. eg,

  Component parent = null;  // non-null if being called by a GUI
  queryString = "Do you want fries with that?";
  int intResponse = JOptionPane.showConfirmDialog(parent, queryString,
           "Confirm", JOptionPane.YES_NO_OPTION);
  myResponse = (intResponse == JOptionPane.YES_OPTION) ? "Yes" : "No";
  System.out.println(myResponse);

And this:

    while(response == null)
    {
        Thread.yield();
    }

should never be called on the main Swing thread, the EDT or event dispatch thread. The reason the code works when it does is because you're calling this little bit above off of the EDT, but when you call it on the EDT it freezes the EDT and thus the entire GUI. Simply don't do it.

You can't do this, plain and simple. There's only one event thread, and while you're sitting in a loop waiting for somebody to click in your JFrame , you're tying up that thread such that no events can be handled.

Don't try to create your own dialog out of a JFrame -- use JOptionPane or a JDialog , which are designed to handle this situation for you internally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM