How to open new JFrame by clicking on button in other JFrame?

Opening a new JFrame by clicking a button

  • 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

  • Answer:

    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)

niibz at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.