IncdecApp4: Main.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Main program for Incdec
 */
public class Main extends JFrame implements AdjustmentListener {
    public static void main (String [] args) {
	java.awt.EventQueue.invokeLater (new Runnable() {
            public void run() {
		new Main ();
            }
        });
    }

    private Incdec id1, id2;
    private JScrollBar sb;

    public Main () {
	// Window setup
	setSize (500, 300);
	setLayout (new FlowLayout());
	setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

	// Create some Incdec's for testing
	id1 = new Incdec ();
	id1.addAdjustmentListener (this); //*1 Use adjustmentListener instead of old get data button
	add (id1);

	// Demonstrate our setBorderColor
	id2 = new Incdec ();
	id2.setBorderColor (Color.GREEN);
	id2.addAdjustmentListener (this); //*1
	add (id2);

	// Try a scrollbar, 
	// to show how it shares same Adjustable interface with our Incdec
	sb = new JScrollBar ();
	sb.addAdjustmentListener (this); //*2 Same as a JScrollBar
	add (sb);

	setVisible (true);
    }

    /**
     * Catch our new callbacks from our Incdec's and our ScrollBar,
     * works the same for both, same AdjustmentEvent,
     * same e.getValue()
     */
    public void adjustmentValueChanged (AdjustmentEvent e) { //*3 Same callback from our synthesized widget OR real JScrollbar
	String source = null;
        if (e.getSource()==id1) source = "Incdec 1"; //*4 Printable name of source widget
	else if (e.getSource()==id2) source = "Incdec 2"; //*4
	else if (e.getSource()==sb) source = "Scrollbar"; //*4

	System.out.println ("Adjustment value changed: " //*3
			    + source + " => " + e.getValue()); //*3
    }
}
[download file]