I have a parent window which will start another window.When the child window is started, I want it to display like a modal dialog.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ModalDialogTest {
public void createUI(){
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel mainPanel = new JPanel();
mainPanel.setBorder(BorderFactory.createEmptyBorder(200, 200, 200, 200));
JButton openButton = new JButton("Open a frame");
openButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
AnotherWindow anotherWindow = new AnotherWindow();
anotherWindow.createUI();
}
});
mainPanel.add(openButton,BorderLayout.CENTER);
frame.add(mainPanel,BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
ModalDialogTest modelDialogTest = new ModalDialogTest();
modelDialogTest.createUI();
}
class AnotherWindow{
public void createUI(){
JFrame frame = new JFrame("Dialog");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel mainPanel = new JPanel();
mainPanel.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
JLabel label = new JLabel("I want to be a modal dialog");
mainPanel.add(label,BorderLayout.CENTER);
frame.add(mainPanel,BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
}
The demo above describes the process of my app and the architecture is just the same.So what is the solution?