Skip to Main Content
Java Swing, 2nd Edition
book

Java Swing, 2nd Edition

by Dave Wood, Robert Eckstein, Marc Loy, James Elliott, Brian Cole
November 2002
Intermediate to advanced content levelIntermediate to advanced
1278 pages
38h 26m
English
O'Reilly Media, Inc.
Content preview from Java Swing, 2nd Edition

Spinner Editors

You probably noticed that the JSpinner class also includes several inner classes. These inner classes provide basic editors (and renderers) for spinners for each of the major model types. While you’ll typically rely on the editor picked by your spinner when you create it, you can override that decision if you like. Here’s a simple example of a modified DateEditor. This spinner displays an mm/yy date, and the step size is one month. Figure 7-16 shows such a spinner.

A customized DateEditor used in a JSpinner

Figure 7-16. A customized DateEditor used in a JSpinner

Here’s the source code that built this editor:

// MonthSpinner.java
//
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.util.*;

public class MonthSpinner extends JFrame {

  public MonthSpinner( ) {
    super("Month Spinner");
    setSize(200,100);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane( );
    c.setLayout(new FlowLayout(FlowLayout.LEFT, 4,4));

    c.add(new JLabel("Expiration Date:"));
    Date today = new Date( );
    // Start the spinner today, but don't set a min or max date.
    // The increment should be a month.
    JSpinner s = new JSpinner(new SpinnerDateModel(today, 
                 null, null, Calendar.MONTH));
    JSpinner.DateEditor de = new JSpinner.DateEditor(s, "MM/yy");
    s.setEditor(de);
    c.add(s);

    setVisible(true);
  }

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

DefaultEditor

All of the other inner class editors ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Threads, 3rd Edition

Java Threads, 3rd Edition

Scott Oaks, Henry Wong

Publisher Resources

ISBN: 0596004087Errata PageSupplemental Content