I have a form asking some details to a user. In top of that form, there are 2 controls : a JSpinner and a JToggleButton.
If the user use the JSpinner, he can select from form number 1 to 4, if he clic on the JToggleButton, the spinner is disabled an the form number 0 is displayed (if this button is toggled back, the spinner is enabled back and the form load with the number in the spinner).
So far no problems.
But I would like now, to display a popup when a form is edited, not saved and that the user use one of the 2 controls discrebed.
No problem for the popup, but I don't know how to undo modification on the control that fired the display of the popup.
Because I am using ChangeListener for the spinner and ActionListener for the button, I am displaying the popup AFTER the modification of the control.
In fact I am searching a way to be notified of an action on the controls but with the possibility to stop the modification (something like a notification listener where we need to return true or false to validate the modification).
How would you do that ?
Thanks.
Here is a sample :
package test;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SwingTest extends JFrame
{
private static final long serialVersionUID = 1L;
private JToggleButton btnRecueilPermanent;
private JSpinner spinner;
private JTextField tf;
private boolean formChanged = false;
public SwingTest()
{
super();
setLayout(new GridBagLayout());
initComponents();
loadForm(1);
}
private void initComponents()
{
JPanel panelGeneral = new JPanel();
GridBagConstraints gbc_panelGeneral = new GridBagConstraints();
gbc_panelGeneral.fill = GridBagConstraints.BOTH;
gbc_panelGeneral.anchor = java.awt.GridBagConstraints.CENTER;
gbc_panelGeneral.weightx = 100.0;
gbc_panelGeneral.weighty = 100.0;
gbc_panelGeneral.gridx = 0;
gbc_panelGeneral.gridy = 0;
add(panelGeneral, gbc_panelGeneral);
panelGeneral.setLayout(new BorderLayout(0, 0));
JPanel panelSelecteur = new JPanel();
panelGeneral.add(panelSelecteur, BorderLayout.NORTH);
JLabel lblChoixDuFormulaire = new JLabel("Choose form :");
panelSelecteur.add(lblChoixDuFormulaire);
spinner = new JSpinner(new SpinnerNumberModel(1, 1, 4, 1));
spinner.addChangeListener(new ChangeListener()
{
public void stateChanged(final ChangeEvent e)
{
loadForm((Integer) spinner.getValue());
}
});
panelSelecteur.add(spinner);
btnRecueilPermanent = new JToggleButton("Permanent form");
btnRecueilPermanent.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(btnRecueilPermanent.isSelected())
{
loadForm(0);
}
else
{
loadForm((Integer) spinner.getValue());
}
}
});
panelSelecteur.add(btnRecueilPermanent);
final JPanel formPanel = new JPanel();
tf = new JTextField(20);
tf.addKeyListener(new KeyListener()
{
@Override
public void keyTyped(KeyEvent e)
{
formChanged = true;
}
@Override
public void keyPressed(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
}
});
formPanel.add(tf);
panelGeneral.add(formPanel, BorderLayout.CENTER);
}
protected void loadForm(final int nbForm)
{
if(formChanged)
{
Object[] options =
{
"Continue", "Discard changes"
};
final int result = JOptionPane.showOptionDialog(this, "You have unsaved modifivations", "Beware !", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
if(result == 0)
{
// HERE WE DISCARD THE FORM CHANGE, BUT THE TOGGLEBUTTON or THE JSPINNER HAVED CHANGED
return;
}
}
if(nbForm == 0)
{
btnRecueilPermanent.setText("Normal form");
}
else
{
btnRecueilPermanent.setText("Permanent form");
}
tf.setText(String.valueOf(nbForm));
spinner.setEnabled(nbForm != 0);
formChanged = false;
}
public static void main(String[] args)
{
final SwingTest jf = new SwingTest();
jf.pack();
jf.setVisible(true);
}
}