how can I make JSpinner value remain unchange

Hi,

I've posted this message on Programming forum before, and many friends recommend me better to post in Swing forum.

I have a question on how to make the value remain unchange after some confirmations? In my program, I have to ask whether user want to change the value,

if yes, something other settings will be reset.

if no, I hope the vaule in Jspinner remains unchange.

I tried to use the state stateChanged event to detect any value change. and set back the previousValue, however, it comes into the event many times...

pbrockway2 has suggested me to add a flag inside stateChanged to prevent it comes into the event, however, this method not so work all times. And the value also will keep being increased or decreased.

Can some of you give a hints,the code below :

jSpinner_row.addChangeListener(new ChangeListener()

{

publicvoid stateChanged(ChangeEvent event)

{

if(CheckFlag)

{

int result = JOptionPane.showConfirmDialog(ParentFrame,

"Are you sure you want to row setting ?",

"Setup", JOptionPane.YES_NO_OPTION,

JOptionPane.QUESTION_MESSAGE);

if (result == JOptionPane.NO_OPTION)

{

CheckFlag =false;

jSpinner_row.setValue(jSpinner_row.getPreviousValue());

CheckFlag =true;

return;

}

else

{

// do reset of controls and re - construct panel

CheckFlag =true;

}

}

}

});

[2184 byte] By [mcstevenjpa] at [2007-11-27 5:10:59]
# 1
too bad...I found a problem as well, I can't only use "jSpinner_row.getPreviousValue()" .getPreviousValue(), always get the "value-1" if current Value is 4, Then I typed 9 , the message prompted, I answer no, the value will set to 8.... >_<
mcstevenjpa at 2007-7-12 10:31:16 > top of Java-index,Desktop,Core GUI APIs...
# 2

this might (?) be one way, but, as is, only works with SpinnerNumberModel

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

JSpinner spinner;

final int MIN = 0, MAX = 100;

public void buildGUI()

{

JPanel p = new JPanel();

spinner = new JSpinner(new SpinnerNumberModel(MIN,MIN,MAX,1));

((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().setEditable(false);

spinner.setUI(new ChangeVerifierUI());

p.add(spinner);

JFrame f = new JFrame();

f.getContentPane().add(p);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

class ChangeVerifierUI extends javax.swing.plaf.basic.BasicSpinnerUI

{

protected Component createNextButton()

{

JButton nextButton = (JButton)super.createNextButton();

nextButton.removeMouseListener(nextButton.getMouseListeners()[0]);

nextButton.addMouseListener(new MouseAdapter(){

public void mousePressed(MouseEvent me){

if(((Integer)spinner.getValue()).intValue() < MAX) checkChange(+1);

}

});

return nextButton;

}

protected Component createPreviousButton()

{

JButton previousButton = (JButton)super.createPreviousButton();

previousButton.removeMouseListener(previousButton.getMouseListeners()[0]);

previousButton.addMouseListener(new MouseAdapter(){

public void mousePressed(MouseEvent me){

if(((Integer)spinner.getValue()).intValue() > MIN) checkChange(-1);

}

});

return previousButton;

}

}

public void checkChange(int direction)

{

int result = JOptionPane.showConfirmDialog(null,"Are you sure you want to row setting ?",

"Setup", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);

if (result == JOptionPane.YES_OPTION) spinner.setValue(

((Integer)spinner.getValue()).intValue()+

(((Number)((SpinnerNumberModel)spinner.getModel()).getStepSize()).intValue())*direction);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-12 10:31:16 > top of Java-index,Desktop,Core GUI APIs...