JSpinner with ChangeListener

A jspinner generates an event of type ChangeEvent when an item is selected that is sent to listeners of type ChangeListener. Both types are defined in the javax.swing.event package.

To obtain the current value of the jspinner, call getValue( ). It returns the current setting.

Program

fsdfsdf

 

 

 

Progarm Source

import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.SpinnerListModel;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.FlowLayout;
import java.awt.Dimension;

class JSpinnerTest extends JFrame implements ChangeListener{
    
    private JButton jb ;
    private JSpinner js;
    private String[] data ={"iPhone-5C-Blue","iPhone-5S-lock-screen","iPhone-5S-Silver"};
        
    JSpinnerTest()
    {
        setLayout(new FlowLayout());
        setTitle("JSpinner with ChangeListener");
        setJSpinner();
        setAction();
        setJButton();
        setSize(700,320);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    void setJSpinner()
    {
        SpinnerListModel mod = new SpinnerListModel(data);
        js = new JSpinner(mod);
        js.setPreferredSize(new Dimension(150,20));
        add(js);
    }
    
    void setJButton()
    {
        jb = new JButton(new ImageIcon("iPhone-5C-Blue-256.png")); 
        add(jb);
    }
    
    void setAction()
    {
        js.addChangeListener(this);
    }
    
    public void stateChanged(ChangeEvent eve)
    {
        String str = (String)js.getValue();
        
        if(str.equals(data[0]))
            jb.setIcon(new ImageIcon("iPhone-5C-Blue-256.png"));
        
        else if(str.equals(data[1]))
            jb.setIcon(new ImageIcon("iPhone-5S-lock-screen-256.png"));
        
        else if(str.equals(data[2]))
            jb.setIcon(new ImageIcon("iPhone-5S-Silver-256.png"));
    }  
}

public class Javaapp {
    
    public static void main(String[] args) {
        
        JSpinnerTest js = new JSpinnerTest();
    }
}

Leave a Comment