Widgets using Eclipse IDE

Main.java  [download]

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.AbstractListModel;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Main extends JFrame {
	private JPanel contentPane;
	private JTextField txtInitialValue;
	private final ButtonGroup buttonGroup = new ButtonGroup(); //*3 buttonGroup affects behavior not appearance

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Main() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 300, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		// First button
		JButton btnPushMe = new JButton("Push me"); //*1 No subclass, just set directly from UI editor input
		btnPushMe.addActionListener(new ActionListener() { //*2 Anonymous inner class for Button's listener
			public void actionPerformed(ActionEvent e) { //*2
				System.out.println ("Push me Button was pushed");			} //*2
		});
		contentPane.add(btnPushMe); //*1
		btnPushMe.setForeground(Color.RED); //*1
		btnPushMe.setFont(new Font("Serif", Font.ITALIC, 18)); //*1
		btnPushMe.setBorder(new LineBorder(new Color(0, 255, 0), 2)); // Listed under Advanced properties //*1
		
		// Second button
		JButton button = new JButton("Push me too");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println ("Push me too Button was pushed");			}
		});
		button.setForeground(Color.RED);
		button.setFont(new Font("Serif", Font.ITALIC, 18));
		button.setBorder(new LineBorder(new Color(0, 255, 0), 2));
		contentPane.add(button);
		
		// Combo box
		final JComboBox<String> comboBox = new JComboBox<String>();
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"one", "two", "three", "four"}));
		comboBox.setSelectedIndex(2);
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange()==ItemEvent.SELECTED) {
				    System.out.println ("Combo: " + e.getItem());
				}
			}
		});
		contentPane.add(comboBox);
		
		// List
		final JList<String> list = new JList<String>();
		list.setValueIsAdjusting(true);
		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				if (! e.getValueIsAdjusting()) {
				    System.out.println ("List: " + list.getSelectedValue());
				}
			}
		});
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setModel(new AbstractListModel<String>() {
			String[] values = new String[] {"un", "deux", "trois", "quatre"};
			public int getSize() {
				return values.length;
			}
			public String getElementAt(int index) {
				return values[index];
			}
		});
		contentPane.add(list);
		
		// The radio buttons
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(Color.BLUE));
		contentPane.add(panel);
		
		JRadioButton rdbtnI = new JRadioButton("I");
		rdbtnI.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				radioCallback(e);
			}
		});
		
		JLabel label = new JLabel("New label");
		panel.add(label);
		panel.add(rdbtnI);
		
		JRadioButton rdbtnIi = new JRadioButton("II");
		buttonGroup.add(rdbtnIi); //*3
		rdbtnIi.setSelected(true);
		rdbtnIi.addItemListener(new ItemListener() { //*4 Callback coded manually, not generated by IDE
			public void itemStateChanged(ItemEvent e) { //*4
				radioCallback(e); //*4
			}
		});
		panel.add(rdbtnIi);
		
		JRadioButton rdbtnIii = new JRadioButton("III");
		buttonGroup.add(rdbtnIii); //*3
		rdbtnIii.addItemListener(new ItemListener() { //*4
			public void itemStateChanged(ItemEvent e) { //*4
				radioCallback(e); //*4
			}
		});
		panel.add(rdbtnIii);
		
		JRadioButton rdbtnIv = new JRadioButton("IV");
		buttonGroup.add(rdbtnIv); //*3
		rdbtnIv.addItemListener(new ItemListener() { //*4
			public void itemStateChanged(ItemEvent e) { //*4
				radioCallback(e); //*4
			}
		});
		panel.add(rdbtnIv);
		
		// Scroll bar
		final JScrollBar scrollBar = new JScrollBar(); //*5 Inner class can access outer variable scrollBar
		scrollBar.addAdjustmentListener(new AdjustmentListener() { //*5
			public void adjustmentValueChanged(AdjustmentEvent e) { //*5
				System.out.println ("Scrollbar: " + scrollBar.getValue()); //*5
			}
		});
		scrollBar.setOrientation(JScrollBar.HORIZONTAL);
		contentPane.add(scrollBar);
		
		// Editable text field
		txtInitialValue = new JTextField();
		txtInitialValue.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println ("Text: " + e.getActionCommand());
			}
		});
		txtInitialValue.setText("Initial value");
		txtInitialValue.setColumns(20);
		contentPane.add(txtInitialValue);
		
		// This button reports the data from several other widgets
		JButton btnGetData = new JButton("Get Data");
		btnGetData.addActionListener(new ActionListener() { //*6 Inner class can access all widgets in Main
			public void actionPerformed(ActionEvent e) { //*6
				System.out.println ("Values: " + comboBox.getSelectedItem() + //*6
					", " + list.getSelectedValue() + //*6
					", " + scrollBar.getValue()); //*6
		}
		});
		contentPane.add(btnGetData);

		// A label widget, no interaction
		JLabel lblThisIsA = new JLabel("This is a label");
		contentPane.add(lblThisIsA);
	}
	
	// Shared by several radio buttons -- I coded this one manually
	private void radioCallback(ItemEvent e) { //*4
		if (e.getStateChange()==ItemEvent.SELECTED) { //*4
		    System.out.println ("Radio: " + ((JRadioButton)e.getItem()).getText()); //*4
		}
	}
}