'Open JFrame from JDialog and it shows on top of JDialog
This is the scenario,
My JFrame
has a button it will open a JDialog
when click it and it is a model dialog.
JDialog
has another button and i want to open another JFrmae
open when click it.
Result : another Jframe
open but it will not come to the top.It shows under the dialog.I want to open the 2nd JFrame
on top of that dialog.
can use secondFrame.setAlwaysOnTop(true);
but i don't have control to close it or move it.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class FrameTest
{
public static void main(String args[])
{
JFrame firstFrame = new JFrame("My 1st Frame");
JButton button = new JButton("Frame Click");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JDialog dialog = new JDialog();
dialog.setSize(100, 100);
dialog.setModal(true);
JButton button1 = new JButton("Dialog Click");
button1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JFrame secondFrame = new JFrame("My 2nd Frame");
secondFrame.setVisible(true);
secondFrame.setSize(400, 200);
secondFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
secondFrame.setAlwaysOnTop(true);
}
});
dialog.add(button1);
dialog.setVisible(true);
}
});
firstFrame.add(button);
firstFrame.setVisible(true);
firstFrame.setSize(400, 200);
firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Solution 1:[1]
JDialog has another button and i want to open another JFrmae open when click it.
Don't do that. A tipical Swing application has a single main JFrame
and several JDialog
s. See this topic The Use of Multiple JFrames, Good/Bad Practice?
Result : another Jframe open but it will not come to the top.It shows under the dialog.I want to open the 2nd JFrame on top of that dialog.
Of course it does because the dialog is modal.
can use secondFrame.setAlwaysOnTop(true); but i don't have control to close it or move it.
It won't solve anything because the problem has to do with modality in dialogs. See this article: How to Use Modality in Dialogs to understand how modality works. There's an explanation in this answer too.
Solution 2:[2]
Try
secondFrame.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
It worked for me in the same situation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | legolas108 |