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

这一生的挚爱 提交于 2019-12-21 02:46:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!