Call a GUI from a JButton - java

I'd like to call a GUI from a swing component JButton. There will already be a GUI running at this time, but I'd like to execute another one through the use of a JButton.
Any help would be greatly appreciated.
P.S: I've tried the .show() method, and it takes over from the current GUI. This is not what I would like it to do.

I've tried the .show() method
Don't use the show() method, it is deprecated. To make a window visible you use setVisible(true).
it takes over from the current GUI. This is not what I would like it to do.
Sounds like you want to prevent the child window from gaining focus. If so then the basic code you use is:
JDialog dialog = new JDialog();
dialog.add(...);
dialog.pack();
dialog.setFocusableWindowState( false );
dialog.setVisible( true );
You should use a JDialog for a child window (not a JFrame) since an application should only contain a single JFrame.

All You need is a JDialog which is here for short user interactions.
The easier way out is JOptionPane which one overload lets you show a component.

Related

JAVA - How do I make the user be able to utilize only the frame on top?

When two JFrames are enabled, I want the user to be able to use only one in the top (think of that like an error pop up on your screen, when you can't press anything but the popup itself). I am aware of the class JInternalFrame and I chose not to use it for my program. Thanks in advance :)
Use JDialog, you can set your main frame as the JDialog's parent frame, so that whenever your main frame and JDialog will display, you will be able to click only the JDialog, not your main frame.
You want a modal behaviour, then. I think you can try using JDialog instead of JFrame, something like:
JDialog dialog = new JDialog(parentFrame, title, true); //parameters: owner, title and modal
dialog.getContentPane().add(somePanel);
dialog.pack();
dialog.setVisible(true);
You can read more about it here: http://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html

Button keeping focus

I've got a JDialog that's created and set to visible whenever the button is clicked.
My problem is that the button keeps the focus and doesn't give it to the JDialog.
Is this a normal behaviour or there's something wrong going on ?
JDialogs aren't modal (are "modeless") by default:
Creates a modeless dialog without a title and without a specified Frame owner.
Try constructing it as:
new JDialog(owner, title, ModalityType.APPLICATION_MODAL);
(Or the equivalent super() call if you're subclassing JDialog. Or whichever modality type you want.)
Try using dialog.requestFocus() if dialog is the newly created JDialog.
See requestFocus() or requestFocusInWindow() for more information.

CustomDialog, modality and dispose on close

I have a CustomDialog that extends JDialog.
In its constructor I have
setModalityType(ModalityType.APPLICATION_MODAL);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
They dont seem to work well together: i think the Modality stucks the defaultcloseoperation, and in the end i have to click twice the X in order to get the CustomDialog closed.
How should i act to obtain both
1- always on-top visualization (i use application_modal for this)
2- dispose on close
It works for me:
dialog.setModal(true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
I answer to provide people who have the same "click the X twice in order to close" behaviour a future reference.
My issue was related to a double setVisible(true), one in the constructor and one in a "initializer" function.
With only 1 setVisible(true), the issue is gone

Swing - How to close a form from inside? How to close the form without affecting the parent form?

I am creating a form in Swing toolkit.
When I want to close it, I go to the JFrame and set it to visible false.
Since the frame creates a Java class of the form, I can easily do it from outside.
When I have a cancel button I usually send a listener inside the form that calls the outer:
Jframe.setVisible (false)
Is there a better way ?
The SwingUtilities class provide a method to get the window ancestor of a given component.
You can gat the parent Window and call setVisible or dispose or perhaps only an event.
Window window = SwingUtilities.getWindowAncestor(this);
window.setVisible( false );
// OR
window.dispose();
// OR
WindowListener[] windowListeners = window.getWindowListeners();
windowListeners[0].windowClosing( null );
try these if you do not need to code much!
click here to view image

Is this possible to make as a option dialog in Java?

alt text http://img202.imageshack.us/img202/2637/dialogo.png
I'm wondering how I could make this into a popup dialog. I designed this with Netbeans gui editor. I looked at option dialog, but all the examples only had a textfield or a combobox, not more than one thing like I have. So what would be the best way to make this in Java.
You should extend JDialog
See the dialogs tutorial
You can make a JDialog form
You could extend JDialog, then use GridBagLayout to implement GUI like that (actually JFrame also works, you just need to specify some setting like default close operations and stuff), if you use JDialog, set setModel() to true to block other GUI just like a pop up window.

Categories

Resources