2

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.
                    }
                });

            }
        }
    });
}
}
Zack
  • 330
  • 3
  • 11
  • Why try-catch is used here, can you explain please?? – UkFLSUI Dec 30 '16 at 06:03
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Not sure if relevant here, but see [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Dec 30 '16 at 06:08
  • @Rakibul islam Don't know eclipse told me too. – Zack Dec 30 '16 at 06:11
  • 1
    _Don't know eclipse told me too._ Well if you don't understand the Exception process, I would suggest that you learn basic Java, Swing is not something you use from the beginning. (And the ValueFrame constructor probably trow an exception) – AxelH Dec 30 '16 at 06:20

2 Answers2

2

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.
                    }
                });
Jochen Bedersdorfer
  • 4,093
  • 24
  • 26
  • This helped a lot, thank you very much. I had to use a set and get method when I moved them for the value string. – Zack Jan 04 '17 at 05:58
0

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();
        }  
    }           
});
UkFLSUI
  • 5,509
  • 6
  • 32
  • 47