Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I get the value of a the selected item in a JSpinner?

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()); }


}
like image 350
hanem Avatar asked Feb 21 '12 15:02

hanem


People also ask

Which function is used to get the selected item from the spinner?

getItemAtPosition(i).

What is JSpinner in Java?

A JSpinner has a single child component that's responsible for displaying and potentially changing the current element or value of the model, which is called the editor . The editor is created by the JSpinner 's constructor and can be changed with the editor property.


1 Answers

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();
like image 112
Tony Avatar answered Sep 20 '22 15:09

Tony