How to close only JDialog and have JFrame still usable - java

I've a JFrame and a JPanel is added in. When I click o a button in the panel, a JDialog (named choiceDialog) appears. When I click on a particular button on the dialog I just want it to close.
I would want the dialog closed and the frame usable. Is it possible?
I tried to hide the dialog with setVisible(false) but it hid both the dialog and frame. Then I tried to do choiceDialog.dispose() but I lost both the elements again. At that point I found a way to set the Frame again visible but not usable.
Can anyone help me please? I don't really know what to do.
Here's the relevant code:
if (dimField.isEnabled()){
String dimFieldText = dimField.getText();
if (dimFieldText.equals("") || !isNumeric(dimFieldText)){ //if there's an error when filling the options in the JDialog
errorLabel = new JLabel(noDim, SwingConstants.CENTER);
/*other stuff
...
*/
}else{ //if it's all ok: I want the JDialog close but the JFrame to be usable
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); //to catch the JFrame istance
choiceDialog.dispose();
topFrame.setVisible(true); //to make the JFrame visible again
//choiceDialog.setVisible(false);
}

You are setting topFrame to be the window ancestor of the button, which is the dialog itself. You need to get the window ancestor of the dialog instead. That assumes that when you created the dialog you specified the main frame as its parent rather than using the no-argument JDialog constructor.

Related

Make the X Close button open a new JFrame in Java

I would like to make it so that when you click the X close button on the top right, a new JFrame will appear and the current one will close.
How would I be able to do it?
The question is, can another JFrame open automatically when one JFrame closes. As noted in comments, two possible common solutions include use of a WindowListener or use of a modal dialog (one example of which is as mentioned by Chance Hoard, a JOptionPane, which again is one solution, but certainly not the only solution).
For a WindowListener to work, you can open the new JFrame in the windowClosed() method, for example:
JFrame frame1 = new JFrame("Frame 1");
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame1.add(Box.createRigidArea(new Dimension(500, 400)));
frame1.pack();
frame1.setLocationByPlatform(true);
frame1.setVisible(true);
frame1.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
JFrame frame2 = new JFrame("Frame 2");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.add(Box.createRigidArea(new Dimension(500, 400)));
frame2.pack();
frame2.setLocationByPlatform(true);
frame2.setVisible(true);
}
});
The key here is to set the default close operation to not be JFrame.EXIT_ON_CLOSE so as not to close down the JVM after the initial JFrame window has closed. In the window listener's windowClosed() method, create and display the 2nd JFrame.
To solve this using a modal dialog, I would create the second window, which could be a JFrame, first, to give the dialog an parent application, but then I would write code to display it after displaying the modal dialog. This way, it only shows up once the dialog is no longer visible:
JFrame frame3 = new JFrame("Frame 3");
frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame3.add(Box.createRigidArea(new Dimension(500, 400)));
frame3.pack();
frame3.setLocationByPlatform(true);
// don't display this JFrame yet
// create the dialog, passing in the JFrame and making the dialog "modal"
JDialog dialog1 = new JDialog(frame3, "Dialog 1", ModalityType.APPLICATION_MODAL);
dialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog1.add(Box.createRigidArea(new Dimension(500, 400)));
dialog1.pack();
dialog1.setLocationRelativeTo(frame3);
// show the dialog first
dialog1.setVisible(true);
// this is called only after the dialog is no longer visible
frame3.setVisible(true);
Having said all this, I still recommend that you read the The Use of Multiple JFrames, Good/Bad Practice? link and I recommend avoiding both of the above "solutions" and instead recommend that one create and display a single main application window, a JFrame, and instead swap GUI views, usually JPanels, by using a CardLayout.

How to get a JFrame position in the screen to put in a JDialog?

i was wondering if it possible to get the position of the location on the screen of a JFrame, to put the same location in a JDialog. I mean, that i want to do is when i open a JDialog from a JFrame button, i want to put the JDialog in the same position that the main JFrame, maybe with the .setLocationRelativeto() method, any ideas to do it??, thank you!.
this.setUndecorated(true);
initComponents();
this.setBounds(WIDTH, WIDTH, 444, 308);
JDialog dialog=new JDialog();
I want to adopt the same position from the mainJFrame, but i dont know how to to it....
dialog.setLocationRelativeTo();
is it possible to get the position of the location on the screen of a JFrame
The code in the ActionListener would be something like:
Component c = event.getComponent();
Window window = SwingUtilities.windowForComponent(c);
JDialog dialog = new JDialog(...);
...
dialog.pack();
dialog.setLocation( window.getLocation() );
dialog.setVisible( rue ;
or you could center the dialog on the frame by using:
dialog.setLocationRelativeTo( window );
dialog.setVisible( true );
EDIT For the record, JFrame extends Window, so if you already have your JFrame, that may be enough.

Trying to implement back feature into my jframe using a button [duplicate]

I have designed two JFrames in NetBeans.
When I click the "rules" button (i.e placed on JFrame1) then it opens a second JFrame (but JFrame2 opens over JFrame1's window, that's what I dont want).
In the second JFrame there is a "close" button. But when I click this button, I want JFrame1 to be opened and it is working too, but JFrame2 is actually not closed and JFrame1 is appearing over JFrame2.
In short the main form is JFrame1. When I click the "rules" button from JFrame1 it opens JFrame2 over JFrame1, and in JFrame2 there is a "close" button when it gets clicked the main form (i.e JFrame1) is lauched but it is launched over JFrame2.
The scenerio is JFframe1 -> JFrame2 -> JFrame1
Now my question is after clicking the "rules" button, JFrame1 should be closed and JFrame2 displayed on the screen and vice versa.
Assuming your button has an actionListener, after clicking the "rules button" put in:
JFrame1.dispose(); //Remove JFrame 1
JFrame2.setVisible(true) //Show other frame
And then reverese them for the opposite reaction
Somethig like this should be on the constructor or method which create JFrame2:
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//call another method in the same class which will close this Jframe
CloseFrame();
}
});
It's method which should close JFrame2
public void CloseFrame(){
super.dispose();
}
Well, if you already have a actionListener, you should add this:
JFrame1.dispose(); // This will close the frame
The JFrame1 is the name of your frame.
And if you want to open another frame that you have, add this:
JFrame2.setVisible(true); // This will put the other frame visible
I'm not an expert by any means, however, I ran into this problem as well. If you set your second JFrame to hidden, when you hit "Cancel", it will close the second JFrame.
//this is the code for the "cancel" button action listener
public void actionPerformed(ActionEvent e) {
setVisible(false);//hides the second JFrame and returns to the primary
this worked for me (Frame1 Called RegScreen and Frame2 Called MainScreen):
RegScreen.this.setVisible(false);
new MainScreen().setVisible(true);
Hope that this helps :) Regscreen was the original frame open at startup.
If this doesn't work, try this
JFrame1.dispose(); //Remove JFrame 1
JFrame2.setVisible(true) //Show other frame
JFrame2.setVisible(true);
this.dispose();
Have a MainClass with a main() method.
Have the MainClass that has the main() method encapsulate your JFrame1 and JFrame2 reference variables. Don't have JFrame1 or JFrame2 contain main() unless you have a specific reason.
After something is clicked in one of the JFrame objects, instantiate/make visible the other JFrame object and dispose of itself via your MainProgram.JFrame object methods.
Example:
//btn event inside 1st JFrame/window
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
MainProgram.openResultsForm(); //MainProgram opens 2nd window
MainProgram.queryEntryForm.dispose(); //MainProgam closes this,
//the 1st window
}

recreate JinternalFrame object after dispose() call

Situation: there is Supplier_JinternalFrame in JDesktopPane. A Supplier_button is available for call SetVisible(true) in JDesktopPane to show the Supplier frame. but when i close the Supplier Frame through Dispose(); its hide and never see again after click button again.
there is and option to use setshow(); and setHide() instead of setDispose(). but i want to recreate Jinternalframe instead of previous form. here is my Supplier_button code for call the Supplier_JInternalFrame Object.
private void Suppliers_ButtonActionPerformed(java.awt.event.ActionEvent evt) {
Suppliers_JinternalFrame.setVisible(true);//call the object of frame to show the frame
}
and in Suppliers_JinternalFrame Close button code
private void Close_SupMangActionPerformed(java.awt.event.ActionEvent evt) {
Suppliers.dispose();//To close the frame
}
after click supplier_Button again. it never show frame again.
Any Idea, what should i do to recall the InternalFrame . Is there Any WindowEvents, i suggestest to create ?
When dispose is called, the JInternalFrame is made invisible, it is also removed from the JDesktopPane (think desktopPane.remove(...))
This means that making the frame visible again has no (visible) effect. You need to add the frame back onto the JDesktopPane

Instantiate JDialog from JPanel

I've got a JPanel, which I want to respond to a mouse click and then open a JDialog. The JDialog constructor needs an instance of JFrame and not JPanel - how do I work around this?
You should really try to attach the JDialog to a parent Dialog or Frame, especially if you want it modal (by passing a parent Window, the dialog will be attached to your Window and bringing the parent will bring the child dialog as well). Otherwise, the user experience can really go wrong: lost dialogs, blocking windows without seeing the modal dialog, etc...
To find your JPanel parent Window, all you need is this code:
JPanel panel = new JPanel();
Window parentWindow = SwingUtilities.windowForComponent(panel);
// or pass 'this' if you are inside the panel
Frame parentFrame = null;
if (parentWindow instanceof Frame) {
parentFrame = (Frame)parentWindow;
}
JDialog dialog = new JDialog(parentFrame);
...
If you don't know if you are in a Frame or Dialog, make the "instanceof" test for both classes.
Using the parameter free constructor will make the dialog owner-less. I think that the best thing to do would be to make the Frame that owns your Panel the owner of the dialog.
By that, I mean that you should use the getParent() from your JPanel to find its owner and then send this object found as the owner of your JFrame.
A crude code for that would be
java.awt.Container c = myPanel.getParent();
while (!(c instanceof javax.swing.JFrame) && (c!=null)) {
c = c.getParent();
}
if (c!=null) {
JFrame owner=(javax.swing.JFrame) c;
JDialog myDialog=new JDialog(owner);
}
I have not tested this code, but it is good enought for you to understand the idea.
If you decided to go with a JOptionPane, you could add a MouseListener to the JPanel with a mouseAdapter inner class to handle mouseClicked events. You would have to declare the panel final in order to access the panel from within the inner class.
final JPanel testPanel = new JPanel();
testPanel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
JOptionPane.showMessageDialog(testPanel,"Title","InformationMessage",JOptionPane.INFORMATION_MESSAGE);
}});//end of mouseClicked method
There's a constructor that don't need argument:
JDialog dialog = new JDialog();
If what you want is to make the dialog modal, maybe you can get a static reference of you main JFrame, something like:
JDialog dialog = new JDialog(MyMainJFrame.getInstance());

Categories