Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

JSpinner does not display data

Thread view: 
yingjian.ma1955@gmail.com - 13 Jul 2006 18:49 GMT
Hi,

I have a program using a JSpinner.  But JSpinner does not display data.
Could you tell me why?  Please be detail.  Thanks a lot.

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class Spinner1 extends JFrame implements ChangeListener{
   private JSpinner m_Spinner = new JSpinner();;
   private SpinnerNumberModel m_Model;
   private JLabel m_l= new JLabel();
   public Spinner1(){
       super("JSpinner");
       Container pane = getContentPane();
       FlowLayout flo = new FlowLayout();
       pane.setLayout(flo);
       pane.add(m_Spinner);
       pane.add(m_l);
   }
   private void createNumberSpinner(){
       int loan = 300000;
       int min = 100000;
       int max = 600000;
       int step = 50000;
       m_Model = new SpinnerNumberModel(loan, min, max, step);
       m_Spinner = new JSpinner(m_Model);
       m_Spinner.addChangeListener( this );
       m_Spinner.setPreferredSize(new Dimension(100,20));
}
   public static void main(String argv[]){
       Spinner1 spinnerFrame = new Spinner1();
       spinnerFrame.createNumberSpinner();
       spinnerFrame.setSize(200, 180);
       spinnerFrame.setLocation(200, 180);
       spinnerFrame.setVisible(true);

   }
   public void stateChanged(ChangeEvent e){
        Object i = m_Spinner.getValue();
       m_l.setText("The loan Amount is " + i.toString() + ".");
    }
}
Andrew T. - 13 Jul 2006 19:05 GMT
> Hi,
>
> I have a program using a JSpinner.  But JSpinner does not display data.
>  Could you tell me why?

Some hints are in the comments.

<sscce>
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class Spinner1 extends JFrame
    implements ChangeListener{

   // do not instantiate this yet!
   // we do not need a 'default' spinner..
   private JSpinner m_Spinner;

   private SpinnerNumberModel m_Model;
   private JLabel m_l= new JLabel();
   public Spinner1(){
       super("JSpinner");
       Container pane = getContentPane();
       FlowLayout flo = new FlowLayout();
       pane.setLayout(flo);
       // call createNumberSpinner here, or else
       // hit a NullPointerException
       createNumberSpinner();
       pane.add(m_Spinner);
       pane.add(m_l);
   }
   private void createNumberSpinner(){
       int loan = 300000;
       int min = 100000;
       int max = 600000;
       int step = 50000;
       m_Model = new SpinnerNumberModel(loan, min, max, step);
       m_Spinner = new JSpinner(m_Model);
       m_Spinner.addChangeListener( this );
       m_Spinner.setPreferredSize(new Dimension(100,20));

}

   public static void main(String argv[]){
       Spinner1 spinnerFrame = new Spinner1();
       // your frame is already on-screen with the 'default'
       // spinner - it is too late to call this ..
       //createNumberSpinner();
       spinnerFrame.pack();
       spinnerFrame.setSize(200, 180);
       spinnerFrame.setLocation(200, 180);
       spinnerFrame.setVisible(true);

   }
   public void stateChanged(ChangeEvent e){
               Object i = m_Spinner.getValue();
           m_l.setText("The loan Amount is " + i.toString() + ".");
       }
}
</sscce>

HTH

Andrew T.
Oliver Wong - 13 Jul 2006 19:16 GMT
> Hi,
>
> I have a program using a JSpinner.  But JSpinner does not display data.
> Could you tell me why?  Please be detail.  Thanks a lot.

I'm gonna number the lines, and snip a lot of code.

[...]
 > public class Spinner1 extends JFrame implements ChangeListener{
1 >    private JSpinner m_Spinner = new JSpinner();;
[...]
 >    public Spinner1(){
[...]
2 >        pane.add(m_Spinner);
[...]
 >    }
 >    private void createNumberSpinner(){
[...]
3 >        m_Spinner = new JSpinner(m_Model);
[...]
 > }
 >    public static void main(String argv[]){
4 >        Spinner1 spinnerFrame = new Spinner1();
5 >        spinnerFrame.createNumberSpinner();
[...]
 >    }
[...]
 > }

   The order of execution is:

04 Spinner constructor is invoked.
01 an empty spinner object (with no model) created, and stored in m_spinner.
02 the spinner object in m_spinner is added to the content pane.
05 createNumberSpinner() is invoked.
03 a new spinner object (with a model) is created and stored in m_spinner.

Thus the spinner object which actually contains the model data is never
added to the content pane. It's a different spinner that's being added.

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.