Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing , Multiple Screens , Transfer of control

Tags:

java

swing

jframe

I am developing a swing application. I am using two screens

1.A button from screen1 will launch screen 2.

PseudoCode :

ScreenA extends JFrame{

    onButtonClick(){
        Screen2.setVisible(true);
    }
    System.out.println("Hai");
}

Screen2 extends JFrame{
    onButtonClick{
         Hide this screen;
    }
}

Now the output is :

  1. The screen 2 will be displayed 2.Hai will be printed.

My Objective : I want to display hai only when a button from screen 2 is clicked and screen 2 dissppears.

How do i achieve it ?

I tried setting a flag for buttonclicked in screen two. But the program just passes through the condition and goes on to the next line . How do i keep the control ?

like image 417
Achilles Avatar asked Feb 21 '23 08:02

Achilles


2 Answers

Use a modal dialog. See How to Make Dialogs for more details.

like image 152
Andrew Thompson Avatar answered Mar 02 '23 17:03

Andrew Thompson


Hope the comments in this snippet will help you explain things.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TwoFrames 
{
    // Making our first JFrame.
    JFrame frame1 = new JFrame("FRAME 1");
    // Declaring our second JFrame.
    JFrame frame2 ;

    public void createAndDisplayGUI()
    {               
        // Used to close the JFrame graciously.
        frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);       

        // Used to position the JFrame at the middle of the screen.
        //frame1.setLocationRelativeTo(null);       

        // Use this instead for placing windows, as determined by the OS.
        frame1.setLocationByPlatform(true);     

        // Calling this method to create our frame2.
        makeNewFrame();

        // Button to show the second JFrame.
        JButton showButton = new JButton("SHOW NEW FRAME");
        showButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                // Checking if the frame2 is already visible
                // on the screen, if YES then we won't 
                // create a new frame, else a new frame 
                // will be created. This will prevent multiple
                // JFrame to be created at the click of this button.                
                if (!(frame2.isShowing()))
                {   
                    // If  you had already disposed it previously 
                    // by clicking the hide Button on the other frame
                    // then the click on this button will recreate
                    // a new frame to be displayed.
                    makeNewFrame();
                    frame2.setVisible(true);
                }
            }
        });

        // Adding the button to the South side of the frame1.
        frame1.add(showButton, BorderLayout.PAGE_END);
        frame1.pack();
        frame1.setVisible(true);
    }

    private void makeNewFrame()
    {       
        frame2 = new JFrame("FRAME 2");
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setLocationByPlatform(true);

        // Creating a JButton to be shown on the JFrame.
        JButton hideButton = new JButton("HIDE FRAME");
        hideButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                // On the click of this button, frame2 will 
                // disappear and HAI will be displayed on the console.
                frame2.dispose();
                System.out.println("HAI");
            }
        });

        // Adding the button to the South side of the frame1.
        frame2.add(hideButton, BorderLayout.PAGE_END);
        frame2.pack();
    }

    public static void main(String... args)
    {
        /* Here we are Secheduling a JOB for 
         * Event Dispatcher Thread, since Swing
         * is not Thread Safe. This is used to place
         * the code which is responsible for 
         * creating and diaplaying your GUI.
         */
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                TwoFrames tf = new TwoFrames();
                tf.createAndDisplayGUI();
            }
        });
    }
}
like image 33
nIcE cOw Avatar answered Mar 02 '23 17:03

nIcE cOw