简体   繁体   中英

How to hide a JFrame but then open it when the message box closes

I'm trying to create a MessageBox Creator.

I Have tried to hide the Creator when the Message Box Opens and then show when the Message Box Closes. I am using the following plugins for Eclipse Neon:

  • WindowBuilder
  • Swing Designer

to help me create the program.

A similar one is here but it did not help: Click Me

The source code is here:

package org.us.me.****.messagebox.creator;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JProgressBar;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MessageBoxCreator {

    private JFrame frmD;
    private JTextField txtMessageGoesHere;

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

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

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmD = new JFrame();
        frmD.setTitle("MessageBox: Creator");
        frmD.setBounds(100, 100, 260, 113);
        frmD.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmD.getContentPane().setLayout(null);

        JTextField MessageBox = new JTextField();
        MessageBox.setText("Message goes here...");
        MessageBox.setBounds(10, 11, 222, 20);
        frmD.getContentPane().add(MessageBox);
        MessageBox.setColumns(10);

        JButton btnGenerate = new JButton("Generate");
        btnGenerate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, MessageBox);

            }
        });
        btnGenerate.setBounds(10, 42, 86, 23);
        frmD.getContentPane().add(btnGenerate);
    }
}

Please Help.

Seems like you are trying to hide the Frame on button click and then on popup's button click you want the initial frame back as visible.

The easiest and straight way forward way to do so is to create your own popup box.

Try this out:

  class CustomPopup extends JFrame
  {
    public CustomPopup()
    {
        frmD.setVisible(false);
        this.setName("Popup");
        this.setLayout(new BorderLayout());
        JLabel l = new JLabel("Enter Message here");
        JButton b = new JButton("Submit");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                frmD.setVisible(true);
                CustomPopup.this.setVisible(false);
                CustomPopup.this.dispose();
            }
        });

        this.add(l,BorderLayout.CENTER);
        this.add(b,BorderLayout.SOUTH);
        this.setSize(300, 150);
        this.setResizable(false);
        this.setDefaultCloseOperation(0);
        this.setVisible(true);
    }

}

Then in your button action listener do this:

btnGenerate.addActionListener(new ActionListener()
{
        public void actionPerformed(ActionEvent e) 
        {
            new CustomPopup();
        }
    });

Note: The above example is in with respect to your code. I have used Border layout for popup in the example as it is the simplest to implement. I believe you can customize the look and feel of your custom popup on your own. There are also more options to create popups with custom settings.

Refer to this for more info: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

I think the design is not good, but to make the frame hide and show change this line:

JOptionPane.showMessageDialog(null, MessageBox);

To

frmD.setVisible(false);
JOptionPane.showMessageDialog(null, MessageBox);
frmD.setVisible(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM