Solved

How to open a new window from an external class in Java

Posted on 2011-03-25
4
661 Views
Last Modified: 2012-05-11
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
Comment
Question by:deeayrian
  • 2
4 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35220776
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 35220778
           prizeCalculator object1 = new prizeCalculator();
            JFrame frame = object1.new prizeCalculatorFrame();
            frame.setVisible(true);
           
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35220783

What means:

object1.new prizeCalculatorFrame();
0
 

Author Closing Comment

by:deeayrian
ID: 35222097
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 236
Java string replace 11 61
maven module vs maven project 3 73
Website checklist for browser compatibility? 2 37
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article will show, step by step, how to integrate R code into a R Sweave document
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question