Moving from one frame to another in java swing

user2681161 picture user2681161 · Aug 14, 2013 · Viewed 48.5k times · Source

I'm just making a small application on window builder and need some help with it. I've made 2 frames individually and I don't know how to specify the action of the button in such a way that when I click on the 'next' botton in the first frame, I want it to move to the second frame.

Here's the source code for each file.

first.java

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.AbstractAction;

import java.awt.event.ActionEvent;

import javax.swing.Action;
import javax.swing.JTextArea;
import java.awt.Font;
import java.awt.event.ActionListener;


public class first extends JFrame {

private JPanel contentPane;
private final Action action = new SwingAction();
private final Action action_1 = new SwingAction();

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                first frame = new first();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public first() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnNext = new JButton("Next");
    btnNext.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnNext.setAction(action_1);
    btnNext.setBounds(257, 228, 55, 23);
    contentPane.add(btnNext);

    JButton btnExit = new JButton("Exit");
    btnExit.setBounds(344, 228, 51, 23);
    contentPane.add(btnExit);

    JRadioButton rdbtnAdd = new JRadioButton("Add");
    rdbtnAdd.setBounds(27, 80, 109, 23);
    contentPane.add(rdbtnAdd);

    JRadioButton rdbtnDelete = new JRadioButton("Delete");
    rdbtnDelete.setBounds(27, 130, 109, 23);
    contentPane.add(rdbtnDelete);

    JRadioButton rdbtnEdit = new JRadioButton("Edit");
    rdbtnEdit.setBounds(27, 180, 109, 23);
    contentPane.add(rdbtnEdit);

    JLabel lblSelectAnOption = new JLabel("Select an Option");
    lblSelectAnOption.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblSelectAnOption.setBounds(27, 36, 121, 23);
    contentPane.add(lblSelectAnOption);
}
private class SwingAction extends AbstractAction {
    public SwingAction() {
        putValue(NAME, "Next");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }
    public void actionPerformed(ActionEvent e) {
        new second_add();
    }
}
}

second.java

    import java.awt.BorderLayout;
    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JComboBox;
    import javax.swing.AbstractAction;
    import java.awt.event.ActionEvent;
    import javax.swing.Action;
    import java.awt.event.ActionListener;


    public class second_add extends JFrame {

        private JPanel contentPane;
        private JTextField txtTypeYourQuestion;
        private JTextField txtQuestionWeight;
        private JTextField txtEnter;
        private JTextField txtEnter_1;
        private JTextField txtValue;
        private JTextField txtValue_1;
        private final Action action = new SwingAction();

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        second_add frame = new second_add();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the frame.
         */
        public second_add() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);

            txtTypeYourQuestion = new JTextField();
            txtTypeYourQuestion.setBounds(22, 11, 177, 20);
            txtTypeYourQuestion.setText("Type your Question Here");
            contentPane.add(txtTypeYourQuestion);
            txtTypeYourQuestion.setColumns(10);

            txtQuestionWeight = new JTextField();
            txtQuestionWeight.setBounds(209, 11, 86, 20);
            txtQuestionWeight.setText("Question weight");
            contentPane.add(txtQuestionWeight);
            txtQuestionWeight.setColumns(10);

            txtEnter = new JTextField();
            txtEnter.setBounds(22, 55, 86, 20);
            txtEnter.setText("Enter . . .");
            contentPane.add(txtEnter);
            txtEnter.setColumns(10);

            txtEnter_1 = new JTextField();
            txtEnter_1.setText("Enter . . . ");
            txtEnter_1.setBounds(22, 104, 86, 20);
            contentPane.add(txtEnter_1);
            txtEnter_1.setColumns(10);

            txtValue = new JTextField();
            txtValue.setText("Value . .");
            txtValue.setBounds(118, 55, 51, 20);
            contentPane.add(txtValue);
            txtValue.setColumns(10);

            txtValue_1 = new JTextField();
            txtValue_1.setText("Value . .");
            txtValue_1.setBounds(118, 104, 51, 20);
            contentPane.add(txtValue_1);
            txtValue_1.setColumns(10);

            JButton btnFinish = new JButton("Finish");
            btnFinish.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                }
            });
            btnFinish.setAction(action);
            btnFinish.setBounds(335, 228, 89, 23);
            contentPane.add(btnFinish);

            JButton btnAddChoice = new JButton("Add choice");
            btnAddChoice.setBounds(236, 228, 89, 23);
            contentPane.add(btnAddChoice);

            JButton btnAddQuestion = new JButton("Add question");
            btnAddQuestion.setBounds(136, 228, 89, 23);
            contentPane.add(btnAddQuestion);

        }
        private class SwingAction extends AbstractAction {
            public SwingAction() {
                putValue(NAME, "");
                putValue(SHORT_DESCRIPTION, "Some short description");
            }
            public void actionPerformed(ActionEvent e) {
            }
        }
    }

Answer

maneesh picture maneesh · Aug 14, 2013
  • Having multiple JFrame instances in one application is bad usability
  • Consider using something like a CardLayout instead.