Results 1 to 8 of 8
  1. #1
    stevedb's Avatar
    stevedb is offline Member
    Join Date
    Mar 2008
    Location
    Blackpool
    Posts
    4
    Rep Power
    0

    Default [SOLVED] Navigation between swing forms

    Hi all,

    I am using Netbeans version 6 to create a tutorial application and I cannot seem to find a way to use a button to open up a different form.

    I have created the forms using JFrames with a Main Menu, then 2 other menu forms that I want to have the user press a button to take them to one of the 2 other menu forms.

    I know that I need to use Action Listeners but it is the code to open the form when the button is pressed which is eluding me.

    Thanks in advance
    Steve

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Let's see your code.

  3. #3
    stevedb's Avatar
    stevedb is offline Member
    Join Date
    Mar 2008
    Location
    Blackpool
    Posts
    4
    Rep Power
    0

    Default

    Here it is for the Main Menu Form, the other forms are called 'UseCaseTutorialMenu.java' and 'ActivityDiagramTutorialMenu.java':

    public class UMLMenu extends javax.swing.JDialog {

    /** Creates new form UMLMenu */
    public UMLMenu(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    javax.swing.JFrame jFrame1 = new javax.swing.JFrame();
    javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
    javax.swing.JButton exitButton = new javax.swing.JButton();
    javax.swing.JButton useCaseTutorialMenuButton = new javax.swing.JButton();
    javax.swing.JButton ActDiaTutMenuButton = new javax.swing.JButton();
    javax.swing.JPanel jPanel1 = new javax.swing.JPanel();

    javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
    jFrame1.getContentPane().setLayout(jFrame1Layout);
    jFrame1Layout.setHorizontalGroup(
    jFrame1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGap(0, 400, Short.MAX_VALUE)
    );
    jFrame1Layout.setVerticalGroup(
    jFrame1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGap(0, 300, Short.MAX_VALUE)
    );

    setDefaultCloseOperation(javax.swing.WindowConstan ts.DISPOSE_ON_CLOSE);

    jLabel1.setFont(new java.awt.Font("Aliens", 0, 24));
    jLabel1.setLabelFor(this);
    jLabel1.setText("UML Diagram Tutorials");

    exitButton.setFont(new java.awt.Font("Tahoma", 1, 12));
    exitButton.setText("EXIT");
    exitButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    exitButtonActionPerformed(evt);
    }
    });

    useCaseTutorialMenuButton.setFont(new java.awt.Font("Tahoma", 1, 12));
    useCaseTutorialMenuButton.setText("Use Case Diagram Tutorial and Quiz");
    useCaseTutorialMenuButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    useCaseTutorialMenuButtonActionPerformed(evt);
    }
    });

    ActDiaTutMenuButton.setFont(new java.awt.Font("Tahoma", 1, 12));
    ActDiaTutMenuButton.setText("Activity Diagram Tutorial and Quiz");
    ActDiaTutMenuButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    ActDiaTutMenuButtonActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGap(0, 100, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGap(0, 100, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap(197, Short.MAX_VALUE)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
    .addComponent(exitButton)
    .addContainerGap())
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(126, 126, 126))))
    .addGroup(layout.createSequentialGroup()
    .addGap(41, 41, 41)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap())
    .addGroup(layout.createSequentialGroup()
    .addComponent(useCaseTutorialMenuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 120, Short.MAX_VALUE)
    .addComponent(ActDiaTutMenuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(48, 48, 48))))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
    .addGap(19, 19, 19)
    .addComponent(jLabel1)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 73, Short.MAX_VALUE)
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(57, 57, 57)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(useCaseTutorialMenuButton, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
    .addComponent(ActDiaTutMenuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(60, 60, 60)
    .addComponent(exitButton)
    .addContainerGap())
    );

    pack();
    }// </editor-fold>

    private void exitButtonActionPerformed(java.awt.event.ActionEve nt evt) {
    System.exit(0); // TODO add your handling code here:
    }

    private void useCaseTutorialMenuButtonActionPerformed(java.awt. event.ActionEvent evt) {
    // TODO add your handling code here:
    }

    private void ActDiaTutMenuButtonActionPerformed(java.awt.event. ActionEvent evt) {
    // TODO add your handling code here:
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    UMLMenu dialog = new UMLMenu(new javax.swing.JFrame(), true);
    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent e) {
    System.exit(0);
    }
    });
    dialog.setVisible(true);
    }
    });
    }

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I'm unable to compile your file. The forum software can mangle code that is posted outside the &#91;code&#93; &#91;/code&#93; tags. I have no patience for dealing with the GroupLayout machine code. If you can edit your post and post formatted code I'll try again.

  5. #5
    stevedb's Avatar
    stevedb is offline Member
    Join Date
    Mar 2008
    Location
    Blackpool
    Posts
    4
    Rep Power
    0

    Default

    Here it is again, sorry didnt realise that it needed to be in the [CODE] tags

    Java Code:
    public class UMLMenu extends javax.swing.JDialog {
        
        /** Creates new form UMLMenu */
        public UMLMenu(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
        }
        
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            javax.swing.JFrame jFrame1 = new javax.swing.JFrame();
            javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
            javax.swing.JButton exitButton = new javax.swing.JButton();
            javax.swing.JButton useCaseTutorialMenuButton = new javax.swing.JButton();
            javax.swing.JButton ActDiaTutMenuButton = new javax.swing.JButton();
            javax.swing.JPanel jPanel1 = new javax.swing.JPanel();
    
            javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
            jFrame1.getContentPane().setLayout(jFrame1Layout);
            jFrame1Layout.setHorizontalGroup(
                jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            jFrame1Layout.setVerticalGroup(
                jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
    
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    
            jLabel1.setFont(new java.awt.Font("Aliens", 0, 24));
            jLabel1.setLabelFor(this);
            jLabel1.setText("UML Diagram Tutorials");
    
            exitButton.setFont(new java.awt.Font("Tahoma", 1, 12));
            exitButton.setText("EXIT");
            exitButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    exitButtonActionPerformed(evt);
                }
            });
    
            useCaseTutorialMenuButton.setFont(new java.awt.Font("Tahoma", 1, 12));
            useCaseTutorialMenuButton.setText("Use Case Diagram Tutorial and Quiz");
            useCaseTutorialMenuButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    useCaseTutorialMenuButtonActionPerformed(evt);
                }
            });
    
            ActDiaTutMenuButton.setFont(new java.awt.Font("Tahoma", 1, 12));
            ActDiaTutMenuButton.setText("Activity Diagram Tutorial and Quiz");
            ActDiaTutMenuButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ActDiaTutMenuButtonActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 100, Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 100, Short.MAX_VALUE)
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(197, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(exitButton)
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(126, 126, 126))))
                .addGroup(layout.createSequentialGroup()
                    .addGap(41, 41, 41)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(useCaseTutorialMenuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 120, Short.MAX_VALUE)
                            .addComponent(ActDiaTutMenuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(48, 48, 48))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(19, 19, 19)
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(57, 57, 57)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(useCaseTutorialMenuButton, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                        .addComponent(ActDiaTutMenuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(60, 60, 60)
                    .addComponent(exitButton)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>
    
        private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            System.exit(0);    // TODO add your handling code here:
    }                                          
    
        private void useCaseTutorialMenuButtonActionPerformed(java.awt.event.ActionEvent evt) {
                     // TODO add your handling code here:
    }
    
        private void ActDiaTutMenuButtonActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    UMLMenu dialog = new UMLMenu(new javax.swing.JFrame(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                }
            });
        }

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class UMLMenu2 extends JDialog {
    
        public UMLMenu2(Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
        }
    
        private void initComponents() {
            JLabel jLabel1 = new JLabel();
            JButton exitButton = new JButton();
            JButton useCaseTutorialMenuButton = new JButton();
            JButton ActDiaTutMenuButton = new JButton();
            JPanel jPanel1 = new JPanel();
    
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    
            jLabel1.setFont(new Font("Aliens", 0, 24));
            jLabel1.setLabelFor(this);
            jLabel1.setText("UML Diagram Tutorials");
    
            exitButton.setFont(new Font("Tahoma", 1, 12));
            exitButton.setText("EXIT");
            exitButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.exit(0);
                }
            });
    
            useCaseTutorialMenuButton.setFont(new Font("Tahoma", 1, 12));
            useCaseTutorialMenuButton.setText("Use Case Diagram Tutorial and Quiz");
            useCaseTutorialMenuButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    String s = "This is where you launch your next " +
                               "gui with components for user interaction.";
                    String title = "Use Case Diagram Tutorial and Quiz";
                    JOptionPane.showMessageDialog(UMLMenu2.this, s, title,
                                                  JOptionPane.PLAIN_MESSAGE);
                }
            });
    
            ActDiaTutMenuButton.setFont(new Font("Tahoma", 1, 12));
            ActDiaTutMenuButton.setText("Activity Diagram Tutorial and Quiz");
            ActDiaTutMenuButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    String s = "This is where you launch your next " +
                               "gui with components for user interaction.";
                    String title = "Activity Diagram Tutorial and Quiz";
                    JOptionPane.showMessageDialog(UMLMenu2.this, s, title,
                                                  JOptionPane.PLAIN_MESSAGE);
                }
            });
    
            jPanel1.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(5,5,5,5);
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.gridwidth = 2;
            jPanel1.add(jLabel1, gbc);
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.gridwidth = 1;
            gbc.gridy = 1;
            gbc.anchor = GridBagConstraints.SOUTH;
            jPanel1.add(useCaseTutorialMenuButton, gbc);
            jPanel1.add(ActDiaTutMenuButton, gbc);
            gbc.gridy++;
            gbc.anchor = GridBagConstraints.SOUTHEAST;
            gbc.gridwidth = 2;
            jPanel1.add(exitButton, gbc);
    
            add(jPanel1, BorderLayout.CENTER);
            pack();
        }
    
        public static void main(String args[]) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    UMLMenu2 dialog = new UMLMenu2(new JFrame(), true);
                    dialog.addWindowListener(new WindowAdapter() {
                        public void windowClosing(WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                }
            });
        }
    }

  7. #7
    stevedb's Avatar
    stevedb is offline Member
    Join Date
    Mar 2008
    Location
    Blackpool
    Posts
    4
    Rep Power
    0

    Default

    Thanks for that, now I continue with the program.

    Again Thanks

  8. #8
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    stevedb, please click Thread Tools > 'Mark Thread as Solved' to let other members know this problem has been resolved. Thanks!
    Did this post help you? Please me! :cool:

Similar Threads

  1. Use multiple forms
    By coco in forum Java Applets
    Replies: 2
    Last Post: 06-14-2010, 05:14 AM
  2. Login Forms
    By dankonsoer in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 12:06 PM
  3. Wep page navigation buttons
    By anthony417 in forum Advanced Java
    Replies: 1
    Last Post: 07-26-2007, 08:40 PM
  4. Forms in jsp
    By bbq in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-05-2007, 04:28 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
  •