deeayrian
asked on
How to open a new window from an external class in Java
I have an application that displays a main menu. Then after clicking one of the buttons on the main menu, it should open a new window. The code for the new JFrame to be displayed after clicking on the button, is in an external class.
How can I get this new window to display once the button is clicked? Currently, the code I have doesn't respond to the button click. No errors are shown, but no new window is either.
How can I get this new window to display once the button is clicked? Currently, the code I have doesn't respond to the button click. No errors are shown, but no new window is either.
//code in main class
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == exitButton)
System.exit(0);
else if (source == calculatorButton)
{
prizeCalculator object1 = new prizeCalculator();
object1.new prizeCalculatorFrame();
}
}
// Code snippet from external class that contains new window code
public class prizeCalculator
{
class prizeCalculatorFrame extends JFrame
{
public prizeCalculatorFrame()
{
setTitle("Prize Calculator");
centerWindow(this);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new prizeCalculatorPanel();
this.add(panel);
this.pack();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What means:
object1.new prizeCalculatorFrame();
ASKER
Awesome! Works perfectly. Thanks SO much!
you want to say
this.setVisible(true);
or simpler
this.show();
I guess there are also other isues