ProgramLover
ProgramLover

Reputation: 91

how to use ChangeListener for JSpinner?

private String[]fontsizelist = {"10","15","20"};
private SpinnerListModel spinmodel = new SpinnerListModel(fontsizelist);
private JSpinner fontsizeitem= new JSpinner(spinmodel);
//Constructor

some codes


         fontsizeitem.addChangeListener(new ChangeListener(){

        public void stateChanged(ChangeEvent e){
            for(int i=0;i<fontsizelist.length;i++){
                fontsizeitem [i]=new JSpinner(fontsizelist[i]);<-shows error 
            }
        }
    });

This code shows me an error message

array required, but JSpinner found

I know what is the problem but I have no idea how should I fix.

I am using a string array because an integer array did not work with

SpinnerNumberModel and SpinnerListModel.

The font size should be changed automatically when the state of JSpinner is

changed. How should I do in my case?

Upvotes: 0

Views: 3469

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

You should start with How to Use Spinners

A SpinnerNumberModel setup right (min of 10, max of 20 and a step of 5) would give you the same result as your fontsizelist

The basic idea is, the JSpinner will notify your ChangeListener when the value is changed, from that, you need to get a reference of the JSpinner and get it's current value, for example...

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            SpinnerNumberModel model = new SpinnerNumberModel(10, 10, 20, 5);
            JSpinner spinner = new JSpinner(model);
            add(spinner);

            spinner.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    JSpinner spinner = (JSpinner) e.getSource();
                    int value = (int)spinner.getValue();
                    System.out.println("Value is " + value);
                }
            });
        }

    }

}

Upvotes: 5

Related Questions