开发者

JOptionPane call stacks

I got a little problem with a JOptionPane which I use to warn user if wrong input is found. It works correct first time. Bu开发者_C百科t when I close down the JFrame which calls for that JOptionPane, and open it again it will this time call for it twice. And it will stack for every close down I do.

I have tried to look for the problem without any luck. I can provide the code, but it is quite large though.

Third EDIT: I have found and solved the problem now.

Ok, I provided the code I use. I have cut it down so it only show the necessary one. I dont think it will compile, but this is how I use the addActionListener();

public class BorderLayoutDemo extends JFrame implements ActionListener {

    private JButton button1 = new JButton("L?gg till kund");
    private JButton button2 = new JButton("Ta bort kund");
    private JButton button3 = new JButton("Visa kund");
    private JButton button4 = new JButton("Lista alla kunder");
    private JButton button5 = new JButton("Avsluta");
    private JButton button6 = new JButton("Change");

    private JTextArea TextWindow = new JTextArea("Hej\nHej\nHej\nHej\nHej\nHej\nHej\nHej\nHej\nHej\nHej\nHej\nHej\n");
    private JScrollPane scrollPane = new JScrollPane(TextWindow);   //l?gger in TextWindow s? att det f?r en scroll-bar

    private JPanel aPanel = new JPanel();
    private JFrame aFrame = new JFrame();

    private JTextField aTextfield1 = new JTextField();
    private JTextField aTextfield2 = new JTextField();

    private JButton aButton1 = new JButton("L?gg till kund");
    private JButton aButton2 = new JButton("St?ng");

    public BorderLayoutDemo() {

            setTitle("Bankregister");

            setLayout(new BorderLayout());

            JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
            panel.setLayout(new GridLayout(6,1,55,5)); //row, cols, hgap, vgap

            button1.addActionListener(this);
            button2.addActionListener(this);
            button3.addActionListener(this);
            button4.addActionListener(this);
            button5.addActionListener(this);
            button6.addActionListener(this);

            panel.add(button1);
            panel.add(button2);
            panel.add(button3);
            panel.add(button4);
            panel.add(button5);
            panel.add(button6);

            JPanel panel2 = new JPanel();
            panel2.add(panel);

            add(panel2,BorderLayout.WEST);
            add(scrollPane,BorderLayout.CENTER);

            setJMenuBar(menu());

            setSize(600,300);

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);

    }

    public void addCustomer(boolean status) {


            if(status) {    

                aFrame.setTitle("L?gg till kund");
                aFrame.setSize(200,300);
                aFrame.setLayout(new GridLayout(3,1));

                aPanel.setLayout(new GridLayout(2,1)); //rad, kolumn

                aPanel.add(aTextfield1);
                aPanel.add(aTextfield2);

                aButton1.addActionListener(this);
                aButton2.addActionListener(this);
                System.out.println("Foo!!!!!!!!!!!!!");

                aFrame.add(aPanel);
                aFrame.add(aButton1);
                aFrame.add(aButton2);

                aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                aFrame.setLocationRelativeTo(null);
                aFrame.setVisible(true);
            }
            else {

                aFrame.setVisible(false);


            }
    }

    public static void main(String[] args) {

        new BorderLayoutDemo();
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == button1) {
            setEnabled(false);
            addCustomer(true);
        }

        //IFs f?r addCustomer();
        else if(e.getSource() == aButton1) {

            if((aTextfield1.getText().isEmpty() || aTextfield2.getText().isEmpty())) {
                JOptionPane.showMessageDialog(null, "You miss to fill out the fields");
            }
            else {
                JOptionPane.showMessageDialog(null, "Added");
                Kund kund = new Kund(aTextfield1.getText(),aTextfield2.getText());
                setEnabled(true);
                register.add(kund);
            }
        }
        else if(e.getSource() == aButton2) {
            setEnabled(true);
            addCustomer(false);
        }


Sounds like you are adding the "validation listener" every time you open the JFrame. So check your "addListenerXXX" code to make sure it is only added/created once.

Which also leads to the question why are you using a JFrame for this? Typically an application has a single JFrame. Then, if you need a window to enter data you create a JDialog.


By passing null as the first parameter of that method you are creating a default JFrame that the JOptionPane uses as its parent component and not the JFrame you have created in your code. If you provide more detail in your question I'm sure someone here will provide you with a much more detailed answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜