I'm new to Java and Swing. I created a jframe and I added a menubar and MenuItem in it.
On clicking a menu item, a jdialog should open. Now the jdialog has a jtextfield in it and a jlabel. Now the problem for me is 'when dialog is opened for first time, the textfield is empty and thats correct. Now i close the jdialog and i open it again but now instead of getting an empty textfield in jdialog, i get the data entered previously' which is not what should happen as the jdialogs 'default close operation' property is set to 'dispose'. but that is not happening for me...
I dont know what i'm doing wrong. I have never tried applet/swing before in any other way (consider this as my first demo learning programme)
Second Image here
The JTextField is retaining it's value because it isn't being affected by the JDialog closing, instead it is being hidden as it's parent (the JDialog) is invisible
Setting the dialog to dispose isn't re-initialising the child components, so they keep their values. Some additional information on this behaviour is available here:
JDialog setVisible(false) vs dispose()
JDialog
One way you can prevent / control this is by "informing" the dialog to wipe the textfield as it is closing by adding a WindowEvent and providing the necessary functionality in the windowClosing() method
Netbeans gui-builder will generate this for you with the following:
Right click Dialog
Events
Window
WindowClosing
Providing:
private void jDialog1WindowClosing(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
In which you can add: textfield.setText(""); to clear the textfield
Another approach is to create your own dialog and setting up the components in the constructor. As creating a new instance will contain the components with their default values, effectively resetting it
Related
i have displyed this JDialog , and have passed an Object which is a JPanel on it , thus my JDialog displays my JPanel on it when Invoked as required.
and on this JPanel I have a JButton, on pressing i want some operations to happen which i have written in it's ActionListener and in the end i have to dispose that JDialog, but i have no clue how to do it !!
This s my JDialog Statement and help me with HOW TO EVEN REMOVE AN ICON from JDialog as even after keeping the ICON PARAMETER NULL it displays the ICON.
JOptionPane.showOptionDialog(null, "SELECT ITEM AND THEN EDIT THE DETAILS.",
"EDIT ITEM DETAILS", int1, int2 , null, objEditMorePane, null);
It sounds like you want to make it a JOptionPane.PLAIN_MESSAGE. That is what you need to put instead of whatever int2 is. I was going to link you to the tutorial but Space Pope already did that. You don't need to create a custom dialog to change the default icon, just change the message type to a plain message. The tutorial covers all this stuff.
You'll need to keep a reference to the dialog if you want to close it yourself. See the Oracle tutorial on custom Dialogs. The constructor you're using also puts in an icon by default; if you make your own dialog, you can control that part too.
JOptionPane closes its dialog when its value property changes. So, you can obtain the parent JOptionPane and set its value to close the window:
JOptionPane optionPane = (JOptionPane)
SwingUtilities.getAncestorOfClass(JOptionPane.class, button);
optionPane.setValue(JOptionPane.CLOSED_OPTION);
If the window wasn't created by JOptionPane, you can use the getTopLevelAncestor method of JComponent to obtain the parent window:
Window window = (Window) button.getTopLevelAncestor();
window.dispose();
I have a Java Swing Dialog with a hidden JLabel above each input component (i.e. JTextField). The purpose of this hidden JLabel is to use it as validation output for its input component.
Let's say, there is an input field for the description of some entity, which has to be non empty, and should contain some special stuff. On error, the action could call the following method:
private void invalidateDescription(String errMessage) {
errDescriptionLabel.setText(errMessage);
errDescriptionLabel.setVisible(true);
descriptionTextField.setBackground(ERR_COLOR);
}
After that, I call pack() and invalidate()
The problem is, that the JDialog still has the same vertical size, so that some of the components (the buttons in the bottom of the dialog) disapear (because they're out of view).
Do you have any suggestion how to fix it?
Best Regards.
edit: I forgott to mention, that the JDialog has a "Free Design" Layout (Netbeans GUI Builder default).
edit 2: I'm looking for a solution which doesn't require kind of a placeholder for (error) JLabel. "Empty Space" is not a desired solution because the dialog doesn't look balanced.
Use CardLayout place your labesl and empty JPanels ans swap thm when necessary.
Instead of using setVisible(), give errDescriptionLabel a background color that matches that of the enclosing panel when the entry is valid.
I have a Java Swing Dialog with a hidden JLabel above each input component
I would not use a hidden component for this. I would change:
private void invalidateDescription(String errMessage)
to
private void invalidateDescription(String errMessage, component inputComponent)
Then I would display a popup with the error message. You could use a non-decorated JDialog as the popup. You might even be able to use a JPopupMenu as the popup.
When you display the popup you would position the popup releative to the input component.
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.
I'm using JOptionPane.showOptionDialog to show a JDialog. I would like to know how:
set the dimension of the dialog (for now I'm using setPreferredSize() method on the given panel but I know that such method shouldn't be used).
make the showed dialog resizable.
My code looks like:
JPanel panel; //my JPanel built with dialog contents
int ret = JOptionPane.showOptionDialog(myFrame,
panel,
"titel",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
options,
options[1]);
I know that I could obtain the desired result building a JDialog this way:
JDialog dialog = new JDialog(panel);
dialog.setResizable(true);
dialog.setSize(800,600);
dialog.setVisible(true);
The problem with the last solution is that I can't get the return value.
EDIT:
in response to #camickr observations:
Why do you need to set the preferred size? If you build the panel
properly is should be displayed at its preferred size.
I'm not sure of having fully understood Swing on this point. The problem is, for example, that I'm displaying through a JDialog a ChartPanel built with JFreeChart. Now, I suppose that panel has it's own preferred size, but I want to see it bigger. How can I do that without explicitly use setPreferredSize()?
Read the JOptionPane API. Search for "Direct Use". It shows you how to
directly access the dialog used by the option pane and you can
I read it but I can't find the right method to understand which button (Ok or Cancel) has been pressed on the JDialog.
This hack using a HierarchyListener to get access to the JOptionPane's also works:
http://blogs.oracle.com/scblog/entry/tip_making_joptionpane_dialog_resizable
// TIP: Make the JOptionPane resizable using the HierarchyListener
pane.addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
Window window = SwingUtilities.getWindowAncestor(pane);
if (window instanceof Dialog) {
Dialog dialog = (Dialog)window;
if (!dialog.isResizable()) {
dialog.setResizable(true);
}
}
}
});
Why do you need to set the preferred size? If you build the panel properly is should be displayed at its preferred size.
Read the JOptionPane API. Search for "Direct Use". It shows you how to directly access the dialog used by the option pane and you can
With you second approach why are you setting the size? Again just pack() the dialog and it will be displayed at the panels preferred size.
What do you mean you can't get the return value? You have access to any method of your custom panel. So you can just invoke the getXXX() method when you receive control again. Just make sure the dialog is modal and the code after the setVisible(true) will block until the dialog is closed.
If you want to go the second way completely, you have to create and position your own "YES" and "NO" buttons somewhere (since a raw JDialog is just an empty modable frame). Therefore, you need to attach a MouseListener to both buttons and handle click events. On a click, you will know what button was pressed, and you'll just have to call dispose() on the dialog to close it.
Does exist in the JDialog class a way to prevent that a child window (JDialog) would be displayed more than once when the button from the main window (JFrame) used to open it, is pressed several times? Many thanks in advance!
Yes, and you don't need to make the box modal to do it (although making it modal would be the easiest way).
Simply do something like the following
In your member delcarations:
private final MyDialog dialog = new MyDialog();
In your code:
private void showDialog() {
dialog.setVisible(true);
dialog.requestFocus(); // May be needed to bring window to front
}
This will ensure that you only instantiate the box once. Simply call showDialog() whenever the button is pressed.
Another way that I've done in the past with Swing is that when the button is pressed the first thing I do is disable the button. Then I use the observable pattern to look at the child window and re-enable the button when the child window is closed. That way if it takes a while to display the child window for some reason the user can't click on it multiple times and mess things up.
You could make the JDialog modal, then the parent window would not react until it is closed.
Or you could initialize the JDialog before, and just make it visible when your button is pressed. Making it visible twice will not display it twice.