Results 1 to 8 of 8
  1. #1
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default unwanted bug. JMenu

    Morning all. I have a mainFrame. And in it I can open another frame called orderFrame when i close orderFrame using dispose, and then open a new orderFrame it adds a new JMenu. so if i close and open say three times I get three "file" menus. Have tryed now for hours but cant find what the problem is.

    ClearText : JMenu dont reset when i dispose orderFrame

    Java Code:
     orderFrame.setJMenuBar(menuBar());
    Java Code:
     orderFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    Would appreciate if someone can explain what im doing wrong.

    EDIT kl 07:19. It is not only JMenu that adds. JTable adds new rows too.
    Last edited by ocean; 11-20-2009 at 07:58 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

  3. #3
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Okey. Have nurfed it as much as i could.

    MainFrame
    Java Code:
    package shortpkg;
    import java.awt.Dimension;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     *
     * @author ocean
     */
    public class OrderMain {
        private JMenuBar mainMenuBar = new JMenuBar();
        private JFrame mainFrame = new JFrame("Mst Order Program");
        private JPanel mainPanel = new JPanel();
         NewOrder newOrder = new NewOrder();
        public OrderMain() {
                mainFrame();
        }
    
    public JMenuBar mainMenu() {
    
        JMenu fileMenu = new JMenu("File");
        JMenuItem newItem = new JMenuItem("New Project");
        newItem.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
            java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
               newOrder.gui();
              }
            });
           }
        });
    
        fileMenu.add(newItem);
        mainMenuBar.add(fileMenu);
        return mainMenuBar;
    }
    
    
    public JFrame mainFrame() {
        mainFrame.add(mainPanel);
        mainFrame.setPreferredSize(new Dimension(800,600));
        mainFrame.setSize(800,600);
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setJMenuBar(mainMenu());
        mainFrame.setLocationRelativeTo(null);
       mainFrame.pack();
        return mainFrame;
    }
    
        public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
               new OrderMain();
              }
          }
    );
        }
    }
    NewOrder Frame. This is the one that keep adding JMenu and Rows in table. if you close NewOrder by pushing the X in the corner and open it agin. If you close mainProgram it resets.


    Java Code:
    package shortpkg;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Vector;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    public class NewOrder extends DefaultTableModel {
     private Vector<Vector> rowData = new Vector<Vector>();
     private Vector<String> col = new Vector<String>();
     private Vector<String> row = new Vector<String>();
     private  JMenuBar menuBar = new JMenuBar();
    
     JMenuBar menuBar(){
          JMenu menu = new JMenu("File");
           JMenuItem saveItem = new JMenuItem("Save");
           JMenuItem newItem =new JMenuItem("New project");
           JMenuItem exitItem = new JMenuItem("Exit Program");
           saveItem.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e) {
                 // writeCol(col);
                 // writeRow(rowData);
               }
           });
           menu.add(saveItem);
           menu.add(newItem);
           menu.add(exitItem);
           menuBar.add(menu);
           return menuBar;
      }
       Vector orderTable(){
           row.addElement(null);
           rowData.add(row);
            return rowData;
       }
    
       void gui() {
       final JTextField addNewColumn = new JTextField("",10);
           orderTable();
           JFrame newFrame = new JFrame("New Project");
           JPanel  newPanel = new JPanel();
    
           final  DefaultTableModel newProject = new DefaultTableModel(rowData,col);
           JTable table = new JTable(newProject);
           JScrollPane scroll = new JScrollPane(table);
    
           JToolBar toolBar = new JToolBar();
           JButton newColumn = new JButton("New Column");
           newColumn.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent event) {
                    String lissenToTextCol = addNewColumn.getText();
                    newProject.addColumn(lissenToTextCol);
               }
    
           });
           JButton newRow = new JButton("New Row");
           newRow.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent event){
                  newProject.addRow(dataVector);
               }
           });
    
           toolBar.add(newColumn);
           toolBar.add(addNewColumn);
           toolBar.add(newRow);
    
           newPanel.add(scroll);
           newFrame.add(newPanel);
           newFrame.add(toolBar, BorderLayout.NORTH);
    
           newFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
           newPanel.setPreferredSize(new Dimension(500,500));
           newFrame.setJMenuBar(menuBar());
           newFrame.setSize(600,600);
           newFrame.pack();
           newFrame.setVisible(true);
           newFrame.setLocationRelativeTo(null);
           newFrame.pack();
        }
      }
    Last edited by ocean; 11-20-2009 at 08:50 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    newItem.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                newOrder.gui();
             }
          });
       }
    });
    So every time the menu item is selected gui() gets called. The gui() method creates a new frame and, towards the end, says:

    Java Code:
    newFrame.setJMenuBar(menuBar());
    The menu bar that gets displayed comes from this menuBar() method. Abbreviated it is:

    Java Code:
    JMenuBar menuBar(){
       JMenu menu = new JMenu("File");
       // stuff happens here
       menuBar.add(menu);
       return menuBar;
    }
    The upshot of all this is that every time the menu item is selected in the frame you are calling mainFrame you are calling menuBar() to add yet another menu to the JMenuBar instance you are calling menuBar.

  5. #5
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Thank you for the quick replay. Is there a quick way to reset all when I close NewOrder?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Well, I haven't really read all that code... But why not scrap the "private JMenuBar menuBar = new JMenuBar();" field you have in NewOrder as you don't appear to be using it anywhere.

    Instead just say "JMenuBar menuBar = new JMenuBar();" at the start of the menuBar() method.

  7. #7
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Yea ofcourse, Thankś for opening my eyes. :D

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. link between two JFrame using JMenu
    By JavaEbony in forum NetBeans
    Replies: 8
    Last Post: 03-09-2011, 12:19 PM
  2. Using a JMenu as a real option
    By nfh in forum AWT / Swing
    Replies: 3
    Last Post: 11-09-2009, 06:04 PM
  3. Repositioning An unwanted JInternalFrame
    By marco.c84 in forum AWT / Swing
    Replies: 8
    Last Post: 03-18-2009, 10:42 PM
  4. JMenu to JPopupMenu
    By carderne in forum New To Java
    Replies: 0
    Last Post: 02-28-2009, 07:07 PM
  5. JMenu and JRadioButtonMenuItem
    By doron70 in forum AWT / Swing
    Replies: 3
    Last Post: 07-18-2007, 07:13 PM

Posting Permissions

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