Incdec2

Incdec.java  [download]

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

/**
 * Abstract base class for all IncDec's
 * with specialized stuff factored into separate overloadable methods
 */
abstract public class Incdec extends JPanel implements ActionListener {
    /**
     * Our internal components that we need to remember
     */
    protected JButton upButton, downButton;
    protected JLabel label;

    /**
     * Our data, initialized inline here 
     */
    protected int value = 0;
    protected int min = 0;
    protected int max = 100;
    protected int incr = 1;

    /**
     * Only those aspects of initialization that everbody will want
     */
    protected Incdec () {
	// Put a border on our JPanel
	// default color, user can modify later
	setBorder (new LineBorder(Color.RED, 1));
    }

    /**
     * Pieces of old constructor, factored out here for subclasses to use
     */
    protected void makeUp () { //*1 Chop up old constructor for use by subclasses
	upButton = new JButton ("+"); //*1
	upButton.addActionListener (this); //*1
	add (upButton); //*1
     }

    protected void makeDown () { //*2 makeDown, similarly
	downButton = new JButton ("-"); //*2
	downButton.addActionListener (this); //*2
	add (downButton); //*2
    }
    
    protected void makeLabel () { //*3 makeLabel, similarly
	label = new JLabel (); //*3
	label.setHorizontalAlignment (JLabel.CENTER); //*3
	refreshLabel (); //*3
	add (label); //*3
    }

    /**
     * Common callback for both buttons,
     * we use ivar to disambiguate.
     */
    public void actionPerformed (ActionEvent e) {
        if (e.getSource()==upButton) value += incr;
	else if (e.getSource()==downButton) value -= incr;

	// Clamp at bounds
	if (value > max) value = max;
	if (value < min) value = min;

	// Update our widget to match
	refreshLabel ();
    }

    /**
     * Common code fragment, extracted to here
     */
    protected void refreshLabel() {
	label.setText (String.valueOf (value));
	repaint ();
    }

    /**
     * Just as an example, we let user customize border color here
     */
    public void setBorderColor (Color color) {
	setBorder (new LineBorder(color, 1));
	repaint ();
    }

    public int getValue() { return value; }
}

IncdecVert.java  [download]

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

/**
 * Code for subclass Incdecvert
 */
public class IncdecVert extends Incdec {
    /**
     * Superclass constructor does most of the work, we finish the job here,
     * just choose the layout and the order of the 3 widgets
     */
    public IncdecVert () {
	setLayout (new GridLayout (3, 1));
	makeUp (); //*1 Taken from superclass, but we determine the order
	makeLabel (); //*1
	makeDown (); //*1
    }
}

IncdecHor.java  [download]

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

/**
 * Code for subclass IncDecHor, just like IncdecVert
 */
public class IncdecHor extends Incdec {
    /**
     * Superclass constructor does most of the work, we finish the job here,
     * just choose the layout and the order of the 3 widgets
     */
    public IncdecHor () {
	setLayout (new GridLayout (1, 3)); //*2 We have different layout
	makeDown (); //*1 We have different order
	makeLabel (); //*1
	makeUp (); //*1
    }
}

Main.java  [download]

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

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

    private Incdec id1, id2, id3, id4; //*2 Use polymorphism

    public Main () {
	// Window setup
	setSize (700, 300);
	Container content = getContentPane();
	content.setLayout (new FlowLayout());
	setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

	// Create some new Incdec's
	id1 = new IncdecVert ();
	content.add (id1); //*1 Add() both kinds just like a standard widget

	id2 = new IncdecHor ();
	content.add (id2); //*1

	id3 = new IncdecVert ();
	content.add (id3); //*1

	id4 = new IncdecHor ();
	content.add (id4); //*1

	// And a button to report the data from the Incdec's, for testing
	JButton b = new JButton ("Get data");
	b.addActionListener (this);
	content.add (b);

	setVisible (true);
    }

    /**
     * Callback from the test button
     */
    public void actionPerformed (ActionEvent e) {
	System.out.println ("Value 1 = " + id1.getValue() + 
		", Value 2 = " +  id2.getValue() +
		", Value 3 = " +  id3.getValue() +
		", Value 4 = " +  id4.getValue());
    }
}