简体   繁体   中英

JFrame, toFront(), ActionListener

My problem is this. I got this two windows that work together and move together. 在此处输入图片说明

在此处输入图片说明

However if I then open broweser or something that will go in front of the screen, and then I try to show my program in front by clicking it on taskbar, then only one window goes in front. Dialog is in the back and I dont know how to fixe it.

I know there is function ToFront() however I stil dont know how to use it in this scenario.

Instead of creating two JFrames, create a JFrame for your main window and create all other windows as non-modal JDialogs, with the JFrame as their owner. This will cause them to be stacked as a single group; whenever the user brings one to the front, all are brought to the front.

This should solve your problem, as VGR already said... Non-modal Dialog will follow it's parent:

public class FocusMain extends JFrame {

    private static FocusMain frame;
    private static JDialog dialog;
    private JCheckBox checkBox;

    private JPanel contentPane;

    public static void main(String[] args) {
        frame = new FocusMain();
        frame.setVisible(true);
        dialog = new JDialog(frame);
        dialog.setSize(100, 100);
    }

    public FocusMain() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        setContentPane(contentPane);

        checkBox = new JCheckBox("show dialog");
        checkBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (checkBox.isSelected()) {
                    dialog.setVisible(true);
                } else {
                    dialog.setVisible(false);
                }
            }
        });
        contentPane.add(checkBox);
    }
 }

With extended JDialog you will need to pass the parent frame through the constructor and if your constructor looks like this: public ExtendedJDialog(JFrame parentFrame) then you can connect it with it's parent frame with super(parentFrame); as the first line in your constructor...

 public class FocusMain extends JFrame {

    private static FocusMain frame;
    private static FocusDialog dialog;
    private JCheckBox checkBox;

    private JPanel contentPane;

    public static void main(String[] args) {
        frame = new FocusMain();
        frame.setVisible(true);
        dialog = new FocusDialog(frame);
    }

    public FocusMain() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        setContentPane(contentPane);

        checkBox = new JCheckBox("show dialog");
        checkBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (checkBox.isSelected()) {
                    dialog.setVisible(true);
                } else {
                    dialog.setVisible(false);
                }
            }
        });
        contentPane.add(checkBox);
    }
 }

and extended JDialog

 public class FocusDialog extends JDialog {

    public FocusDialog(JFrame parentFrame) {
        super(parentFrame);
        setSize(100, 100);
    }
 }

if you need the dialog to block the parent, use super(parentFrame, 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