Link to home
Start Free TrialLog in
Avatar of deeayrian
deeayrianFlag for United States of America

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.
//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();
    }

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

One thing that in constructore of your JFrame
you want to say

 this.setVisible(true);

or simpler

this.show();

I guess there are also other isues
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

What means:

object1.new prizeCalculatorFrame();
Avatar of deeayrian

ASKER

Awesome!  Works perfectly.  Thanks SO much!