0

I want to know how to open this JFrame form (1) when I click a button in the second JFrame (2). The problem is that I am unable to get the .setVisible method in the Form 2. Please help. Thanks & Regards ! :)

Form 1 (to be opened when a button is clicked on Form 2

public class FlightForm {

    public FlightForm() {
        initialize();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FlightForm window = new FlightForm();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Form 2

public class MainMenu{

private JFrame frame;

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

/**
 * Create the application.
 */
public MainMenu() {
    frame = new JFrame("Main Menu");
    setBounds(100, 100, 830, 574);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnNewButton = new JButton("Flight Form");
    );
    btnNewButton.setFont(new Font("Candara", Font.BOLD, 15));
    btnNewButton.setBounds(169, 328, 193, 77);
    frame.getContentPane().add(btnNewButton);

    JButton btnNewButton_1 = new JButton("Passenger Form");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            PassengerForm window = new PassengerForm();
                window.setVisible(true); // This is not working
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
niibz
  • 3
  • 1
  • 3
  • possible duplicate of [Java Open a new window by clicking a button](http://stackoverflow.com/questions/11273267/java-open-a-new-window-by-clicking-a-button) – ControlAltDel Apr 17 '15 at 13:30
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Apr 17 '15 at 18:49

1 Answers1

0

You can call setVisible(true) on PassengerForm() only if PassengerForm class extends JFrame. If no you should use something like:

PassengerForm window = new PassengerForm();
window.getFrame().setVisible(true)
baza92
  • 344
  • 1
  • 10
  • This does not work man. I have solved it on my own :D I need to extend a JPanel first in the form that I want to open. – niibz Apr 17 '15 at 14:00