Widgets

Main.java  [download]

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

public class Main extends JFrame {
    private MyComboBox c1; //*2 But save some widgets cause showData needs them
    private MyList l1; //*2
    private MyScrollBar s1; //*2

    public static void main (String [] args) {
	java.awt.EventQueue.invokeLater (new Runnable() {
            public void run() {
		new Main ();
            }
        });
    }

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

	// Make various widgets and add them
	MyButton b1 = new MyButton ("Push me"); //*1 Widgets are local variables, no need to save
	content.add (b1);

	// Some are local variables, we don't save
	MyButton b2 = new MyButton ("Push me too"); //*1
	content.add (b2);

	// Some we save in ivars for showData()
	c1 = new MyComboBox (); //*2
	content.add (c1);

	l1 = new MyList (); //*2
	content.add (l1);

	MyRadio r1 = new MyRadio (); //*1
	content.add (r1);

	s1 = new MyScrollBar (); //*2
	content.add (s1);

	MyTextField t1 = new MyTextField (); //*1
	content.add (t1);

	// This button reports the data from several other widgets
	MyGetButton b = new MyGetButton (this); //*1
	content.add (b);

	// A label widget, no interaction
	JLabel label = new JLabel ("This is a label"); //*1
	content.add (label);

	// Finally, put our window on the screen
	setVisible (true);
    }

    // MyGetButton calls back to us cause we have the objects with the data
    public void showData() { //*3 From callback from MyGetButton
	System.out.println ("Values: " + c1.getSelectedItem() + //*3
	    ", " + l1.getSelectedValue() + //*3
	    ", " + s1.getValue()); //*3
    }
}

MyButton.java  [download]

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

public class MyButton extends JButton implements ActionListener {
    public MyButton (String label) { //*1 Similar to before
	setText (label); //*2 Add some customization
	setFont (new Font("Serif", Font.ITALIC, 18)); //*2
	setForeground (Color.RED); //*2
	setBorder (new LineBorder(Color.GREEN, 2)); //*2
	addActionListener (this);
    }

    public void actionPerformed (ActionEvent e) { //*3 JButton uses ActionListener
	System.out.println (getText() + " Button was pushed"); //*4 Retrieve text from JButton
    }
}

MyComboBox.java  [download]

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

// Note: On older versions of Java, use "JComboBox" not "JComboBox<String>"
public class MyComboBox extends JComboBox<String> implements ItemListener {
    public MyComboBox () {
	addItem ("one"); //*1 Load initial values
	addItem ("two"); //*1
	addItem ("three"); //*1
	addItem ("four"); //*1
	setSelectedItem ("three"); //*2 Set initial choice
	addItemListener (this);
    }

    public void itemStateChanged (ItemEvent e) { //*3 JComboBox uses ItemListener
	if (e.getStateChange()==ItemEvent.SELECTED) { //*4 Reports every select or deselect, we filter out
	    System.out.println ("Combo: " + e.getItem()); //*4
	}
    }
}

MyList.java  [download]

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

// Note: On older versions of Java, use "JList" not "JList<String>"
public class MyList extends JList<String> implements ListSelectionListener {
    public MyList () {
	String[] items = {"un", "deux", "trois", "quatre"}; //*1 Uses array to load initial values
	setListData (items); //*1
	setSelectionMode (ListSelectionModel.SINGLE_SELECTION); //*2 Allow shift-click or not
	addListSelectionListener (this);
    }

    public void valueChanged (ListSelectionEvent e) { //*3 This one uses ListSelectionListener
	if (! e.getValueIsAdjusting()) { //*4 Reports every change
	    System.out.println ("List: " + getSelectedValue()); //*4
	}
    }
}

MyRadio.java  [download]

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

public class MyRadio extends JPanel implements ItemListener { //*1 Our own object, made of a JPanel, holding JRadioButtons and a ButtonGroup
    public MyRadio () {
	// Border around our JPanel
	setBorder (new LineBorder(Color.BLUE, 1));

	ButtonGroup g = new ButtonGroup (); //*2 ButtonGroup makes them behave as radio buttons

	JRadioButton rb = new JRadioButton ("I", false); //*3 Create buttons
	add (rb); //*4 Add button to our panel
	g.add (rb); //*5 Also add button to our ButtonGroup
	rb.addItemListener (this); //*6 Make entire MyRadio be listener for all buttons, use getItem() to disambiguate

	rb = new JRadioButton ("II", true); //*3
	add (rb); //*4
	g.add (rb); //*5
	rb.addItemListener (this); //*6

	rb = new JRadioButton ("III", false); //*3
	add (rb); //*4
	g.add (rb); //*5
	rb.addItemListener (this); //*6

	rb = new JRadioButton ("IV", false); //*3
	add (rb); //*4
	g.add (rb); //*5
	rb.addItemListener (this); //*6
    }
    
    public void itemStateChanged (ItemEvent e) {
	// Reports every select or deselect, we filter out
	if (e.getStateChange()==ItemEvent.SELECTED) {
	    System.out.println ("Radio: " + ((JRadioButton)e.getItem()).getText()); //*6
	}
    }
}

MyScrollBar.java  [download]

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

public class MyScrollBar extends JScrollBar implements AdjustmentListener {
    public MyScrollBar () { //*1 Same as before
	setOrientation (HORIZONTAL); //*2 Customize
	addAdjustmentListener (this);
    }

    public void adjustmentValueChanged (AdjustmentEvent event) { //*3 JScrollBar uses AdjustmentListener
        System.out.println ("Scrollbar: " + getValue()); //*4 JScrollBar remembers current value, retrieve from there
    }
}

MyTextField.java  [download]

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

public class MyTextField extends JTextField implements ActionListener {
    public MyTextField () {
	super ("Initial value", 20); //*1 Pass initial values to JTextField constructor
	addActionListener (this);
    }

    public void actionPerformed (ActionEvent e) { //*2 Uses ActionListener for ENTER key, then also retrieve data
	System.out.println ("Text: " + e.getActionCommand()); //*2
    }
}

MyGetButton.java  [download]

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

public class MyGetButton extends JButton implements ActionListener {
    private Main parent; //*1 Save pointer to parent

    public MyGetButton (Main parent) { //*1
	this.parent = parent; //*1

	setText ("Get Data");
	addActionListener (this);
    }

    public void actionPerformed (ActionEvent e) { //*2 Delegate to parent cause it has the other widgets
	parent.showData(); //*2
    }
}