0

I have problem with JSpinner over to show Month in JSpinner and i have follow code bellow.I use swing control with jframe form to use spinner control. When I run project it always set default value 0. How to solve this error?

 static protected String[] getMonthStrings(){
        String[] months=new DateFormatSymbols().getMonths();
        int lastIndex=months.length-1;
        if(months[lastIndex]==null || months[lastIndex].length()<=0){
            String[] mS=new String[lastIndex];
            System.arraycopy(months,0,mS, lastIndex,0);
            return mS;
        }
        else{
            return months;
        }
    }
    public spinner(boolean CycleMonths) {

        initComponents();
        JTextField tf=null;
        String[] monthStrings = getMonthStrings();
         SpinnerListModel monthModel=null;
         if(CycleMonths){
             monthModel=new CycleSpinnerList(monthStrings);
         }
         else {
             monthModel=new SpinnerListModel(monthStrings);
         }
         spMonth=new JSpinner(monthModel);
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
nara son
  • 39
  • 5
  • "*When i run project it always set default value 0*" - I don't understand what you mean by that. Do you mean that 1) the JSpinner displays "January" (index 0) and you don't want that? Or do you mean 2) that the JSpinner just displays a zero? Or maybe 3) that the JSpinner displays nothing? Please post a [Short, Self Contained, Correct (Compilable), Example](http://sscce.org/), so the problem becomes clearer and is actually runnable for us. (also see [mcve]) – Lukas Rotter Sep 08 '15 at 17:37
  • Oh!yes the jspinner just only displays a zero. – nara son Sep 09 '15 at 01:55

1 Answers1

0

In your constructor code, you are calling initComponents, then creating your SpinnerListModel, creating a new JSpinner, but never adding it anywhere... So it looks like the problem is that you're just not adding the JSpinner anywhere

    public spinner(boolean CycleMonths) {

        initComponents();
        JTextField tf=null;
        String[] monthStrings = getMonthStrings();
         SpinnerListModel monthModel=null;
         if(CycleMonths){
             monthModel=new CycleSpinnerList(monthStrings);
         }
         else {
             monthModel=new SpinnerListModel(monthStrings);
         }
         spMonth=new JSpinner(monthModel);

    }
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80