Results 1 to 5 of 5
  1. #1
    victoryo is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default 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.

    Java 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);
    }
    Hope someone can help me cus iam going crazy on this part :P

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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 03:11 PM.

  3. #3
    victoryo is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default 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!!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 4
    Last Post: 05-15-2011, 05:47 AM
  2. Replies: 6
    Last Post: 06-04-2010, 09:43 PM
  3. Replies: 13
    Last Post: 03-20-2010, 02:03 AM
  4. JOptionPane - message styles
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 10:20 AM
  5. JOptionPane - message dialog
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 10:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •