• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 667
  • Last Modified:

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

0
deeayrian
Asked:
deeayrian
  • 2
1 Solution
 
for_yanCommented:
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
0
 
objectsCommented:
           prizeCalculator object1 = new prizeCalculator();
            JFrame frame = object1.new prizeCalculatorFrame();
            frame.setVisible(true);
           
0
 
for_yanCommented:

What means:

object1.new prizeCalculatorFrame();
0
 
deeayrianAuthor Commented:
Awesome!  Works perfectly.  Thanks SO much!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now