-1

This is a very basic Java syntax question. I have a form called frmMainMenu in its own class, and that has a button labeled "Schedule". When you click "Schedule" I want it to open another form. That one is in a separate class called frmSchedule.

In the ActionListener part of frmMainMenu I have this:

public void actionPerformed(ActionEvent e) {
       System.out.println("TEST");
       frmSchedule window2 = new frmSchedule();
}

It prints the word "TEST" in the console (so I know the code is in the right place). But, it does not open the Schedule form. I also tried it like this, which also didn't work:

public void actionPerformed(ActionEvent e) {
       System.out.println("TEST");
       new frmSchedule();
}

Here's the code for frmSchedule:

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

public class frmSchedule extends JFrame {

    private JPanel contentPane;


    public frmSchedule() {
        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 btnNewButton = new JButton("Trials");
        btnNewButton.setBounds(128, 100, 89, 23);
        contentPane.add(btnNewButton);
    }

}

I guess I'm just having trouble finding the way to phrase the code to tell it to open the second form.

Cardinal System
  • 2,749
  • 3
  • 21
  • 42
Michael T
  • 1,745
  • 5
  • 30
  • 42
  • You never make the frame visible – MadProgrammer Jan 01 '18 at 22:35
  • 3
    [The Use of Multiple JFrames: Good or Bad Practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice); [Why is it frowned upon to use a null layout in Swing?](https://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing); I'd consider a `CardLayout` or possibly a `JDialog` if you need to quickly collect information from the user – MadProgrammer Jan 01 '18 at 22:37
  • Class names should start with an upper case character. Have you ever seen a class in the API that doesn't? Learn by example and follow conventions. – camickr Jan 02 '18 at 02:27
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jan 02 '18 at 03:20

1 Answers1

1

As MadProgrammer pointed out, It might help to make it visible ;)

public void actionPerformed(ActionEvent e) {
       System.out.println("TEST");
       frmSchedule window2 = new frmSchedule();
       window2.setVisible(true);
}

Also, in case you are not aware of what you doing with the layout, you should look at this: what does frame.setLayout(null)do?(frame is a JFrame)

Cardinal System
  • 2,749
  • 3
  • 21
  • 42