Zack
Zack

Reputation: 330

Prevent Multiple JFrame windows from opening

Down towards the bottom of my code where I open up a new JFrame (balanceFrame or valueFrame), a new frame opens up with one of those frames when a user right clicks that menu option.

However, after closing the new balanceFrame or valueFrame that pops up, and you open up another balance/valueFrame, two open up. After you close those two and open another, three open up. Any idea on how to stop this? It seems like my programs remembering past values for the variable 'value' and opening up multiple windows.

    table.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            System.out.println("Pressed");

        }
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                JTable source = (JTable)e.getSource();
                int row = source.rowAtPoint( e.getPoint() );
                int column = source.columnAtPoint( e.getPoint() );
                String value = table.getModel().getValueAt(row, column).toString();

                if (! source.isRowSelected(row)) 
                    source.changeSelection(row, column, false, false);

                popup.show(e.getComponent(), e.getX(), e.getY());

                menuItemBalanceSheet.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        try {
                            balanceFrame = new BalanceFrame("BalanceSheet", value);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                        balanceFrame.setSize(1200, 600);
                        balanceFrame.setVisible(true);  
                        balanceFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    }
                });

                menuItemCompanyValue.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        try {
                            valueFrame = new ValueFrame("Company Value", value);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                        valueFrame.setSize(1200, 600);                      
                        valueFrame.setVisible(true);    
                        valueFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                        value.
                    }
                });

            }
        }
    });
}
}

Upvotes: 2

Views: 1740

Answers (2)

UkFLSUI
UkFLSUI

Reputation: 5672

For this line one JFrame is created:

valueFrame = new ValueFrame("Company Value", value);

And again you are calling setVisible(true). That's why second one is created:

valueFrame.setVisible(true);

Consider omitting one of them.

Tips:

Try performing setSize() and setDefaultCloseOperation() inside the main ValueFrame and BalanceFrame class. And in the upper where you have declared JFrame balanceFrame; and JFrame valueFrame;, try to do that ValueFrame valueFrame; and BalanceFrame balanceFrame;

Now in the down part of code, in ActionListener try this:

menuItemCompanyValue.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        try {
            new ValueFrame("Company Value", value).setVisible(true);
        } catch (Exception e1) {
            e1.printStackTrace();
        }  
    }           
});

Upvotes: 0

Jochen Bedersdorfer
Jochen Bedersdorfer

Reputation: 4122

You keep adding action listeners to your menu when you only want one. Move this code to your initialization code:

 menuItemBalanceSheet.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        try {
                            balanceFrame = new BalanceFrame("BalanceSheet", value);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                        balanceFrame.setSize(1200, 600);
                        balanceFrame.setVisible(true);  
                        balanceFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    }
                });

                menuItemCompanyValue.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        try {
                            valueFrame = new ValueFrame("Company Value", value);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                        valueFrame.setSize(1200, 600);                      
                        valueFrame.setVisible(true);    
                        valueFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                        value.
                    }
                });

Upvotes: 2

Related Questions