Results 1 to 5 of 5

Thread: How to use Spinners in Java?

  1. #1
    Join Date
    Jul 2006
    Posts
    191

    How to use Spinners in Java?

    Hi friends,
    I am new to the Java programming language. Before I have used the combo boxes but I want something that can scroll up also. This can be done by using the Spinners. But I don't know the coding for that and also I don't know anything about the Spinners.!! So please tell me how to use Spinners in Java.?? please help me as soon as possible.!!
    ASUS P5VD1-X
    Intel Pentium Dual Core 3.00GHz
    Maxtor 160GB
    Corsair 1.5GB PC3200 RAM
    Nvidia Geforce 6800 GT 256mb
    Phillips DVD RW
    Magna 500W PSU
    XION II Steel Black Gaming Case

  2. #2
    Join Date
    Mar 2008
    Posts
    672

    Re: How to use Spinners in Java?

    If you have observed carefully then you should know that spinners are similar to combo boxes and lists. the similar things are that in both user can choose option from a range of values. Spinners allow the user to type in a value, just like you did in editable combo boxes.!! The difference between the two is that the spinners do not have a drop-down list that can cover up other components like the combo boxes. Only the current value is visible in spinner since they do not display possible values. When the set of possible values is extremely large, spinners are oftenly used instead of combo boxes or lists.

  3. #3
    Join Date
    Mar 2008
    Posts
    349

    Re: How to use Spinners in Java?

    The following code would help you for creating the spinner :
    Code:
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.Container;
    import java.util.Calendar;
    import java.util.Date;
    
    public class SpinnerDemo extends JPanel {
        public SpinnerDemo(boolean cycleMonths) {
            super(new SpringLayout());
    
            String[] labels = {"Month: ", "Year: ", "Another Date: "};
            int numPairs = labels.length;
            Calendar calendar = Calendar.getInstance();
            JFormattedTextField ftf = null;
    
            String[] monthStrings = getMonthStrings(); //get month names
            SpinnerListModel monthModel = null;
            if (cycleMonths) { //use custom model
                monthModel = new CyclingSpinnerListModel(monthStrings);
            } else { //use standard model
                monthModel = new SpinnerListModel(monthStrings);
            }
            JSpinner spinner = addLabeledSpinner(this,
                                                 labels[0],
                                                 monthModel);
            ftf = getTextField(spinner);
            if (ftf != null ) {
                ftf.setColumns(8); //specify more width than we need
                ftf.setHorizontalAlignment(JTextField.RIGHT);
            }
    
            int currentYear = calendar.get(Calendar.YEAR);
            SpinnerModel yearModel = new SpinnerNumberModel(currentYear, 
                                           currentYear - 100, 
                                           currentYear + 100, 
                                           1);                
    
            if (monthModel instanceof CyclingSpinnerListModel) {
                ((CyclingSpinnerListModel)monthModel).setLinkedModel(yearModel);
            }
            spinner = addLabeledSpinner(this, labels[1], yearModel);
            spinner.setEditor(new JSpinner.NumberEditor(spinner, "#"));
    
            Date initDate = calendar.getTime();
            calendar.add(Calendar.YEAR, -100);
            Date earliestDate = calendar.getTime();
            calendar.add(Calendar.YEAR, 200);
            Date latestDate = calendar.getTime();
            SpinnerModel dateModel = new SpinnerDateModel(initDate,
                                         earliestDate,
                                         latestDate,
                                         Calendar.YEAR);//ignored for user input
            spinner = addLabeledSpinner(this, labels[2], dateModel);
            spinner.setEditor(new JSpinner.DateEditor(spinner, "MM/yyyy"));
    
            SpringUtilities.makeCompactGrid(this,
                                            numPairs, 2, 
                                            10, 10,        
                                            6, 10);
        }

  4. #4
    Join Date
    Jul 2006
    Posts
    289

    Re: How to use Spinners in Java?

    You will have to return the formatted text field used by the editor then only the code of spinner will work properly. If the editor doesn't descend from JSpinner.DefaultEditor, then you will have to return Null. The following is the code for the same :
    Code:
        public JFormattedTextField getTextField(JSpinner spinner) {
            JComponent editor = spinner.getEditor();
            if (editor instanceof JSpinner.DefaultEditor) {
                return ((JSpinner.DefaultEditor)editor).getTextField();
            } else {
                System.err.println("Unexpected editor type: "
                                   + spinner.getEditor().getClass()
                                   + " isn't a descendant of DefaultEditor");
                return null;
            }
        }
    Signatures reduce available bandwidth

  5. #5
    Join Date
    Jul 2006
    Posts
    442

    Re: How to use Spinners in Java?

    I think that you should be pretty clear with using the Spinners in Java.
    Many times you need to create the GUI and show it. If you invoke this method from the event dispatch thread then your thread would be more safe. The following coding explains the same :
    Code:
    private static void createAndShowGUI() {
            JFrame frame = new JFrame("SpinnerDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.add(new SpinnerDemo(false));
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
    	        UIManager.put("swing.boldMetal", Boolean.FALSE);
    		createAndShowGUI();
                }
            });
        }
    }
    "When they give you ruled paper, write the other way..." J.R.J.

Similar Threads

  1. Replies: 4
    Last Post: 04-09-2013, 11:04 PM
  2. Setting Of Java to The Point At Manual Java
    By winni in forum Software Development
    Replies: 4
    Last Post: 10-01-2011, 10:05 PM
  3. How big is an Object in Java? Why Java neglects sizeof?
    By KALINDA in forum Software Development
    Replies: 4
    Last Post: 10-11-2009, 03:19 AM
  4. Link List Example in Java Sample program in Java
    By trickson in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 08:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,912,893.94011 seconds with 16 queries