Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java keep frame focused

Tags:

java

focus

swing

Could you please help me on this one? I have a JDialog with some textfields, checkboxes and buttons. I want that when the frame is not focused anymore, to disappear. So I added a focus listener to the JDialog and when the focus is lost, I call dialog.setVisible(false);. The problem is that if I click on the checkbox,textfield or button, the frame loses it's focus and disappears. How could I keep it focused until the user clicks outside it's area?

EDIT : The "frame" I am referring to is a JDialog. I don't use a Frame nor a JFrame. All the components are placed on the JDialog. I want it to hide when not focused, but keep it focused until the user clicks outside it's area.

like image 969
Teo Avatar asked Jan 18 '23 05:01

Teo


2 Answers

Seems like you had added the wrong Listener, what you should be adding is addWindowFocusListener(...), see this small sample program, is this what you want to happen :

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

public class DialogFocus 
{
    private JFrame frame;
    private MyDialog myDialog;

    public DialogFocus()
    {
    }

    private void createAndDisplayGUI()
    {
        frame = new JFrame("JFRAME");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);

        myDialog = new MyDialog(frame, "My Dialog", false);

        JButton showButton = new JButton("SHOW DIALOG");
        showButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (!(myDialog.isShowing()))
                    myDialog.setVisible(true);
            }
        });

        frame.add(showButton, BorderLayout.PAGE_END);

        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    public static void main(String\u005B\u005D args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new DialogFocus().createAndDisplayGUI();
            }
        });
    }
}

class MyDialog extends JDialog
{
    private WindowFocusListener windowFocusListener;

    public MyDialog(JFrame frame, String title, boolean isModal)
    {
        setTitle(title);
        setModal(isModal);

        JPanel contentPane = new JPanel();

        JTextField tfield = new JTextField(10);
        JComboBox cbox = new JComboBox();
        cbox.addItem("One");
        cbox.addItem("Two");
        cbox.addItem("Three");

        contentPane.add(tfield);
        contentPane.add(cbox);

        windowFocusListener = new WindowFocusListener()
        {
            public void windowGainedFocus(WindowEvent we)
            {
            }

            public void windowLostFocus(WindowEvent we)
            {
                setVisible(false);
            }
        };

        addWindowFocusListener(windowFocusListener);

        add(contentPane);
        pack();
    }
}
like image 111
nIcE cOw Avatar answered Jan 22 '23 08:01

nIcE cOw


Make the dialog modal, then the user cannot click on the frame.

like image 37
Andrew Thompson Avatar answered Jan 22 '23 09:01

Andrew Thompson