Define which JFrames will be focused?

1

I need to keep a%% of focus in mind and not allow others to use it until I close this one.
Every time you open a new frame the others below it must be inoperative.
How can I do this?

    
asked by anonymous 02.05.2016 / 15:01

1 answer

2

For this, you should create a JFrame as the main screen, and other screens such as JDialog , because with it, you can create modal windows, where only the one that was last opened will be available for modification.

One of the constructors of JDialog receives a container (it can be a Frame or another Dialog) of which it will be "dependent", and a boolean informing if the window will be modal or not. Through these two arguments, you can control the relationship between windows, making certain windows accessible only if their dependents are not open.

I made an executable example of how it works.

Primary JFrame class:

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

public class ModalTeste extends JFrame {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> new ModalTeste().start());
    }

    private JFrame getInstance() {
        return this;
    }

    //start frames
    private void start() {
        setTitle("Frame principal");
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton btnFrame = new JButton("Abrir Dialog");
        btnFrame.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //passando a instancia do Frame para referencia do modal
                new Dialog01(getInstance()).start();
            }
        });
        setLayout(new BorderLayout());
        add(new JLabel("Este é o frame principal"), BorderLayout.CENTER);
        add(btnFrame, BorderLayout.PAGE_END);
        setVisible(true);
        setLocationRelativeTo(null);
    }

The classes below represent two JDialogs, where Dialog01 is invoked in JFrame, and Dialog02 is invoked by Dialog01 :

class Dialog01 extends JDialog {

    //precisa passar a janela mae como parametro para servir
    //de referencia ao modal
    public Dialog01(JFrame owner) {
        //recebe a janela mae, o titulo(opcional) e se é modal
        super(owner, "Dialog 01", true);
    }

    private JDialog getInstance() {
        return this;
    }

    public void start() {
        JButton btn2 = new JButton("Abrir outro dialog");
        btn2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //aqui está sendo passada a instancia do Dialog01
                //como referencia do modal da dialog02
                new Dialog02(getInstance()).start();
            }
        });
        setLayout(new BorderLayout());
        add(new JLabel("Esta é a primeira janela modal"), BorderLayout.CENTER);
        add(btn2,BorderLayout.PAGE_END);
        setSize(200, 200);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(getParent());
        setVisible(true);
    }

}
class Dialog02 extends JDialog {

    // repare que o Jdialog pode receber um JFrame ou
    // outro JDialog como argumento
    public Dialog02(Dialog owner) {
        // recebe a janela mae, o titulo e se é modal
        super(owner, "Dialog 02", true);
    }

    public void start() {

        add(new JLabel("Esta é a segunda janela modal"));
        setSize(200, 200);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(getParent());
        setVisible(true);
    }
}

And the result:

Reference:

How to Make Dialogs

    
02.05.2016 / 16:39