Set which Jframes will focus?

Asked

Viewed 327 times

2

I need to leave a JFrame in focus and not allow use of others, until I close this.
Every time you open a new frame the others below it should be dead.
How can I do that?

1 answer

2

For this, you must create a JFrame as main screen, and other screens as JDialog, because with it, it is possible to create modal windows, where only the one that was last opened will be available for change.

One of the builders of JDialog receives a container(Can be a Frame or other Dialog) from which it will be "dependent", and a Boolean informing whether the window will be modal or not. Through these two arguments, you can control the relationship between windows, making certain windows accessible only if your dependents are not open.

I made an executable example of how it works.

Class of the main Jframe:

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 the 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:

inserir a descrição da imagem aqui


Reference:

How to Make Dialogs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.