So I tried searching and I found an answer here: Java swing application, close one window and open another when button is clicked
I did the example given there and it worked just fine. But when I tried to put it in my code it's not working.
The objective of my program is to open a new window when a button is clicked. The program shows an error when dispose() is there but it still won't run when it's deleted. Is there any way to fix this? Or should I try using CardLayout? Thanks in advance.
Here's my code:
HelpME.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HelpME extends JPanel
implements ActionListener{
private JButton PetrolStation=new JButton("Petrol Stations");
private JButton Food=new JButton("Foods");
private JButton Parking=new JButton("Parkings");
private JButton Coffee=new JButton ("Coffee");
private JButton Shopping=new JButton("Shoppings");
private JButton Pharmacies=new JButton("Pharmacies");
private JButton Supermarket=new JButton ("Supermarkets");
private JButton Hospital=new JButton ("Hospitals");
private JButton Hotel=new JButton ("Hotels");
private JButton OutdoorPark=new JButton ("Outdoor Parks");
public HelpME(){
HelpME h = new HelpME();
h.setLayout(new GridLayout(5,2));
JFrame f = new JFrame("HelpMe");
f.getContentPane().add(h);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(460, 460);
f.setVisible(true);
f.setLocationRelativeTo(null);
PetrolStation.addActionListener(this);
Food.addActionListener(this);
Food.setActionCommand("Foods");
Parking.addActionListener(this);
Coffee.addActionListener(this);
Shopping.addActionListener(this);
Pharmacies.addActionListener(this);
Supermarket.addActionListener(this);
Hospital.addActionListener(this);
Hotel.addActionListener(this);
OutdoorPark.addActionListener(this);
add(PetrolStation);
add(Food);
add(Parking);
add(Coffee);
add(Shopping);
add(Pharmacies);
add(Supermarket);
add(Hospital);
add(Hotel);
add(OutdoorPark);
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if(cmd.equals("Foods"))
{
dispose();
new SecondScreen();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run()
{
new HelpME().setVisible(true);
}
});
}
}
SecondScreen.Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SecondScreen extends JFrame {
public SecondScreen() {
super("Location Finder");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JLabel("Empty JFrame"));
pack();
setVisible(true);
}
}
This is the error
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.lang.ReflectiveOperationException.<init>(ReflectiveOperationException.java:89)
at java.lang.reflect.InvocationTargetException.<init>(InvocationTargetException.java:72)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javax.swing.UIDefaults.getUI(UIDefaults.java:769)
at javax.swing.UIManager.getUI(UIManager.java:1016)
at javax.swing.JPanel.updateUI(JPanel.java:126)
at javax.swing.JPanel.<init>(JPanel.java:86)
at javax.swing.JPanel.<init>(JPanel.java:109)
at javax.swing.JPanel.<init>(JPanel.java:117)
at HelpME.<init>(HelpME.java:19)
at HelpME.<init>(HelpME.java:21)
at HelpME.<init>(HelpME.java:21)
at HelpME.<init>(HelpME.java:21)
at HelpME.<init>(HelpME.java:21)
at HelpME.<init>(HelpME.java:21)
at HelpME.<init>(HelpME.java:21)