As I have answered something similar in this question, when you have multiple windows in the same application, the most recommended is to use JDialog
for secondary windows, and JFrame
only to the main window, because through the JDialog
you can have more control of which window will be in focus, the exchange of data between windows is facilitated because, while the Jdialog is open, other windows are locked1 for selection, awaiting the end of the operation on it. Of course this requires a certain hierarchy control between windows.
1- only if the Modalitytype is not Modeless, which leaves the other windows free for change.
In the code below, taken from the other question, there is an executable example of how the Jdialogs, and control between them.
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
Dialog01 d1 = new Dialog01(getInstance());
d1.start();
}
});
setLayout(new BorderLayout());
add(new JLabel("Este é o frame principal"), BorderLayout.CENTER);
add(btnFrame, BorderLayout.PAGE_END);
setVisible(true);
setLocationRelativeTo(null);
}
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;
}
private 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
Dialog02 d2 = new Dialog02(getInstance());
d2.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);
}
private void start() {
add(new JLabel("Esta é a segunda janela modal"));
setSize(200, 200);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setLocationRelativeTo(getParent());
setVisible(true);
}
}
}
"Better way" makes the question opinionated.
– user28595
Or what is a way to not open so many windows in the windows bar? Dialogs?
– Ulisses Gimenes
I edited, see if you didn’t change your line of reasoning.
– user28595
I get it, thank you.
– Ulisses Gimenes