Resetting a JDialog after closing - java

I am using a JDialog to get payment info, paymentAmount and Date is submitted by a JTextfield and a datechooser.beans.DateChooserCombo.
When the user close the JDialog or clicks Cancel, JDialog closes.But when they click the Payment button and JDialog reappears, previously submitted inputs are shown.
I want JDialog to be default state whenever it appears.Is there a default way to do this, or i have to create my own reset method?

When you close a dialog it is not destroyed. It will just become invisible, but it still contains everything as it was when it was closed.
You may override the function setVisible() and reinitialize it if the dialog should be shown again.
#Override
public void setVisible(boolean bVisible)
{
if(bVisible == false)
{
super.setVisible(bVisible);
return;
}
initMyValues();
super.setVisible(bVisible);
return;
}
Alternatively you could create a WindowListener and then you get notified about various state changes of the window. Depends on what suits your needs better. The WindowListener doesn't require you to create a separate class, jsut to override the setVisible(), but you have to add some extra function required by the interface.

Another workaround would be to set a windowListener to your dialog.
myDialog.addWindowListener(new WindowListener() {
/*Implements over methods here*/
#Override
public void windowClosing(WindowEvent e) {
//set default values here
}});

Related

How to activate JTextField with a keyboard

I have two JPanels inside another JPanel. One of them has a JTextField inside, another few JButtons. I want focus to be set on the JTextField every time user starts typing (even when one of the buttons has focus at the moment).
KeyListener won't work, because in order for it to trigger key events, the component it is registered to must be focusable AND have focus, this means you'd have to attach a KeyListener to EVERY component that might be visible on the screen, this is obviously not a practical idea.
Instead, you could use a AWTEventListener which allows you to register a listener that will notify you of all the events been processed through the event queue.
The registration process allows you to specify the events your are interested, so you don't need to constantly try and filter out events which your not interested in
For example. Now, you can automatically focus the textField when a key is triggered, but you should check to see if the event was triggered by the text field and ignore it if it was
One of the other things you would be need to do is re-dispatch the key event to the text field when it isn't focused, otherwise the field will not show the character which triggered it...
Something like...
if (Character.isLetterOrDigit(e.getKeyChar())) {
filterField.setText(null);
filterField.requestFocusInWindow();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
filterField.dispatchEvent(e);
}
});
}
as an example
You need to attach a KeyListener to all controls in your JPanel, with a reference to the JTextField you want to focus like so:
panel.addKeyListener(new KeyPressedListener(yourTextField));
button1.addKeyListener(new KeyPressedListener(yourTextField));
button2.addKeyListener(new KeyPressedListener(yourTextField));
class KeyPressedListener implements KeyListener
{
private JTextField textFieldToFocus;
public KeyPressedListener(JTextField textFieldToFocus)
{
this.textFieldToFocus = textFieldToFocus;
}
#Override
public void keyPressed(KeyEvent e) {
textFieldToFocus.requestFocus();
}
}

Minimize the window (and not to close it) when the user click on the “x” button? What is wrong in this Java Swing code?

I have to change the normal behavior of a class that extend *JFrame.
The new behavior consists in the fact that when the user click on the "X" button the window will not close but it is minimized in the toolbar or the operating system.
So I have a class named MainFrame that extend JFrame and originally is something like this:
public class MainFrame extends JFrame {
public MainFrame() {
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
// do something
}
// OTHER METHODS()
}
So from what I have understand to obtain this behavior I have to change the previous line:
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
into:
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
and in this way the window is not closed when the user click on the X button, then I have to add a listener that minimize the window when the user click on the X button, something like this one:
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
((JFrame)e.getSource()).setState(JFrame.ICONIFIED);
}
});
I think that this should work but my problem is that I don't know where I have to put it !!!
I tryed to put it into my constructor but Eclipse mark me some error message:
- WindowAdapter cannot be resolved to a type
- The method addWindowListener(WindowListener) in the type Window is not applicable for the arguments (new WindowAdapter()
{})
Why? What am I missing? How can I solve?
Did you import both of the following?
java.awt.event.WindowAdapter;
java.awt.event.WindowEvent;
Especially WindowAdapter...
Also, to your question of where to put the addWindowListener() method, the constructor would be appropriate.

How to run a method only after a frame has been closed?

So, my code right now looks like this:
Class2 className = new Class2(param1, param2);
className = null;
if (Class2 == null) {
refreshState();
}
I want the refreshState method to run once the className object is destroyed. So basically Class2 is a class that runs another frame on top of my existing frame. I want the method to run only when the new frame has been closed. How can I do this?
So basically Class2 is a class that runs another frame on top of my existing frame. I want the method to run only when the new frame has been closed. How can I do this?
The better solution is to use modal dialogs such as a JOptionPane or a modal JDialog. This will stop the processing of the Swing code in the main window until the dialog window has been dealt with and is no longer visible. Then your refreshState can run immediately after the dialog has been closed:
Pseudocode:
main window code
show modal dialog
refresh state here. This will be called only after the dialog has returned.
I would say add a java.awt.event.WindowListener to the frame:
class2.getFrame().addWindowsListener(new WindowAdapter() {
public void windowClosed(WindowEvent we) {
// The frame is closed, let's dot something else
refreshState();
}
}

How can I set an action command to window closing event?

How can I set an action command to closing event of a custom frame class which is a subclass of javax.swing.JFrame?
This is the current code:
this.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// some stuff here
}
});
The code that goes in // some stuff here is shared with a button labeled quit. For the button, I have set an action command to "quit" and set the listener to an external class named NavigationHandler whose actionPerformed has a case for "quit". If I could set the action command of my window closing event to "quit", I could use the same listener for the window too.
Currently I have a method that I call from both sites, but that feels unclean.
try as follow
To reuse the actionListener of quite button you can click quite button in windowClosing .
to click a button from code call doclick() method. example
quitButton.doClick();
You want to add a WindowListener to the JFrame which will let you get close events.
frame.addWindowListener(new MyWindowListener());
...
public class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
// do something
}
}

Closing a dialog created by JOptionPane.showOptionDialog()

I am creating an options dialog using JOptionPane.showOptionDialog(...);
For the options parameter I am passing an array of JButtons each with its own ActionListener.
One of these buttons is responsible for closing the dialog. My question is: what code do I place in the close button's event handler to close the option dialog?
A point that may make a difference: the class responsible for showing this dialog is a singleton and, as such, the method responsible for displaying the dialog is static. Therefore, calling javax.swing.JInternalFrame.doDefaultCloseAction(); does not work "from a static context".
Thanks
final JButton btn = new JButton("Close");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Window w = SwingUtilities.getWindowAncestor(btn);
if (w != null) {
w.setVisible(false);
}
}
});
Try
JOptionPane.getRootFrame().dispose();
JOptionPane is closed by calling its setValue method
(Speaking of dirty side effects)

Categories

Resources