Results 1 to 5 of 5
- 12-26-2011, 01:49 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
JOptionPane message duplicates itself
Hi all,
I got a problem with my java code.
What i have is a frame (frame) with a button to go to an other frame (frame1) to get an overview with a jtable.
On frame1 there is also a button to export the jtable to a CSV file. Everything works well except for one thing.
When i click the convert to excel button on frame1 a JOptionPane message shows up if it went fine or with an error.
The problem is when i close frame1 so iam on frame again and go to the overview again and click the convert to excel button again
the JOptionPane is shown twice. When i do this again it will be shown 3 times and i can go on like this until * times :P.
But when i keep frame1 open and click the convert to excel button and do it again without closing frame1 everything works well..
Here is the code of my frame 1.
Hope someone can help me cus iam going crazy on this part :PJava Code:private void frame1() { frame1 = new JFrame("Rittenregistratie overzicht"); frame1.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("Images/auto_icon.png"))); frame1.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); frame1.setUndecorated(true); frame1.getRootPane().setWindowDecorationStyle(JRootPane.NONE); ((DefaultTableModel)table.getModel()).setNumRows(0); table.setEnabled(false); frame1.setResizable(false); if (kilometers.size() > 0 || bestuurders.size() > 0) { for (int i = 0; i < kilometers.size(); i++) { Kilometer kilometer = (Kilometer)kilometers.get(i); Bestuurder bestuurder = (Bestuurder)bestuurders.get(i); model.addRow(new String[]{bestuurder.getNaam(), bestuurder.getDate(), kilometer.getBeginstand() + " KM" , kilometer.getEindstand() + " KM", kilometer.getAfstand() + " KM","€ " + totaalPrijs(i)}); } } SwingUtilities.updateComponentTreeUI(scrollPane); excelButton.setBounds(0, 0, 180, 30); exitButton.setBounds(520,0,80,30); frame1.add(excelButton); frame1.add(exitButton); frame1.add(scrollPane); frame1.setLayout(null); frame1.setSize(600, 600); scrollPane.setBounds(0, 30, 600, 570); excelButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { JOptionPane.showMessageDialog(null, "Success"); CSVExporterUtils.exportToCSV( table, "RittenregistratieData" ); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Some Error.\n" + ex.getMessage()); } }}); exitButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { button2.setEnabled(true); frame1.setVisible(false); frame1.dispose(); }}); frame1.setVisible(true); }
-
Re: JOptionPane message duplicates itself
It seems to me that you are adding an ActionListener more than once to a JButton.
Looking at your code, in your frame1() method, you create a new JFrame called frame1, but populate it with a JButton called excelButton, but this JButton isn't created anew inside of the method, but rather is probably a class field that has been created elsewhere and only once. Each time this frame1() method is called, this same excelButton has a new ActionListener added to it in addition to any ActionListeners that have been added before, and each ActionListener code is activated when the button is pressed.
A solution is to either create a new excelButton JButton (and the exitButton too), each time this method is called, or to have a frame1 field and create this only once in the program but display it whenever the frame1() method is called.
As an aside, your frame1 JFrame should probably be a JDialog, not another JFrame, since it is behaving more as a dialog as it is dependent upon the main JFrame.
Oh, and welcome to the Java-Forums!Last edited by Fubarable; 12-26-2011 at 02:11 PM.
- 12-26-2011, 02:33 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Re: JOptionPane message duplicates itself
Omg that was so fast! :D
And you solved my problem! I made a jdialog of it insteed of a jframe.
And also said that excelButton = new JButton("blabla") and same for exitButton;
Thank you!!
-
Re: JOptionPane message duplicates itself
You're welcome, and again welcome to the java-forums.org.
Similar Threads
-
JOptionPane - Message dialog box does not display
By aibao in forum NetBeansReplies: 4Last Post: 05-15-2011, 04:47 AM -
java message box, how to not show multiple message box with same title or content
By oohay in forum AWT / SwingReplies: 6Last Post: 06-04-2010, 08:43 PM -
How to display array integer in JOptionPane message dialog ?
By Reero8532 in forum New To JavaReplies: 13Last Post: 03-20-2010, 01:03 AM -
JOptionPane - message styles
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 09:20 AM -
JOptionPane - message dialog
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 09:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks