1

I have a home page with title and a few buttons I cannot get a new window to open when i click on the button. Here is the code i have for the home page aswell as the class with next screen i am attempting to open trimed for what seems relevant. The NewTicketWindow class is also attached it is plain at the moment. Any help is appreciated.

public class Home
{    

private JFrame frame;
JInternalFrame internalFrame;

/**
 * Launch the application.
 */
public static void main(String[] args)
{
    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            try
            {
                Home window = new Home();
                window.frame.setVisible(true);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Home()
{
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize()
{
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel title1 = new JLabel("City of Murphy");

    JLabel title2 = new JLabel("Traffic Ticket Input System");

    JButton newTicketButton = new JButton("New Ticket");
    newTicketButton.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {

        }

    });

    JButton payTicketButton = new JButton("Make a Payment");

    JButton reportButtton = new JButton("Ticket Report");

    JButton exitButton = new JButton("Exit");
    GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
}

second class (the screen i want to open upon newticket button being pressed

public class NewTicketWindow extends JFrame
{

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args)
{
    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            try
            {
                NewTicketWindow frame = new NewTicketWindow();
                frame.setVisible(true);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public NewTicketWindow()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    JLabel lblEnterNewTicket = new JLabel("Enter New Ticket Information");
    GroupLayout gl_contentPane = new GroupLayout(contentPane);

}

2 Answers 2

1

just add these lines into your action performed code -

NewTicketWindow frame = new NewTicketWindow();
frame.setVisible(true);
Sign up to request clarification or add additional context in comments.

7 Comments

both the classes should be in same package.
This worked great I guess what i'm trying to do though is open a new page in the same window do i need to change my second class to something other than a frame. If that makes sense. It now opens a new window when I click. I know that's what I asked for in my post but i am trying to stay in the same window just open the new page (if that's the proper term on top of the home menu as to return to the home once done on the new screen.
with your current implementation, when you open newticketwindow, just close the previous window programatically. or you can make two separate panels and add them to a frame, and change these panels on button click.
copy that, thank you i used dispose. I am not there yet but when i want to repoen the home menu after entering information on the second screen what method would i call or would i just setVisible to true?
make an object of home class. I don't understand why are you creating thread unnecessarily. you can just make the object and call the setvisible function in main function directly.
|
1

The ActionListener of newTicketButton should create the new frame by calling the constructor of NewTicketWindow (same thing you are doing in the main of NewTicketWindow):

JButton newTicketButton = new JButton("New Ticket");
newTicketButton.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        try
        {
            NewTicketWindow newTicketWindow = new NewTicketWindow();
            newTicketWindow.setVisible(true);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

});

Also you need to add the newTicketButton to the home window:

frame.add(newTicketButton);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.