问题
I'm making an application that uses a JSpinner with a max number 30,I should choose a value from this JSpinner and tape a String to the JTextField and the result will appear in the Textarea,when I compile I have many problems concerning the method jSpinner1State,can any one help me because I don't know where is my problem. This is my code of the method JSpinner:
jSpinner1.addChangeListener(this);
private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling code here:
Object sp=jSpinner1.getValue();
int i =Integer.parseInt(sp.toString() );
String targetIP=jTextField1.getText();
try{
jSpinner1StateChanged(evt);
String cmd = "tracert -h "+i+ "" +targetIP;
Process p = Runtime.getRuntime().exec(cmd);
InputStream in = p.getInputStream();
StringBuilder build = new StringBuilder();
Reader reader = new InputStreamReader(in);
char[] buffer = new char[512];
int nbRead = reader.read(buffer);
while(nbRead > 0) {
build.append(buffer, 0, nbRead);
nbRead = reader.read(buffer);
}
String response = build.toString();
jTextArea1.setText(response);
}catch(Exception e){
jTextArea1.append(e.toString()); }
}
回答1:
For one, it appears you have an infinite loop in your code. Inside your jSpinner1StateChanged function, you are calling jSpinner1StateChanged(evt)
, which will cause an infinite loop.
How are you creating your JSpinner? If you're using ints, then create it by using a SpinnerNumberModel
. This will simplify your code when getting the current value out of the spinner.
jSpinner1 = new JSpinner(new SpinnerNumberModel(0, 0, 30, 1));
Integer currentValue = (Integer)jSpinner1.getValue();
来源:https://stackoverflow.com/questions/9380241/how-can-i-get-the-value-of-a-the-selected-item-in-a-jspinner