Swing/GUI Cheat Sheet
To display a Swing component, you must:
To get events from a GUI component, you must do the following:
When the listener method is called, you can find out which component sent the event by calling getSource() on the event.
public void actionPerformed (ActionEvent event) { Object theButton = event.getSource(); if (theButton == framedCircleButton) { // Create a framed circle } }
If a method returns a String, remember to compare the result using the equals method, not ==:
aMenu.getSelectedItem ().equals ("A value")
The following methods can be applied to any Component:
void setFont (Font f) void setForeground (Color c) void setBackground (Color c)
To construct a font use:
new Font (String name, int style, int size)
Style can be one of Font.BOLD, Font.ITALIC, Font.PLAIN, or Font.BOLD + Font.ITALIC.
The specific components we have considered:
new JButton (String s)
String getText ( ) void setText (String s)
ActionListener
void addActionListener (ActionListener al)
void actionPerformed (ActionEvent e)
new JComboBox ( ) void addItem (Object item)
To find out which item was selected, use:
Object getSelectedItem ( )If you wish to treat the value returned from this method as a String, you may use either the toString() method, as in
String text = menu.getSelectedItem().toString();or a type cast, as in:
String text = (String) menu.getSelectedItem ( );
To find out the index of the item that was selected, use:
int getSelectedIndex ( )
ItemListener
void addItemListener (ItemListener il)
void itemStateChanged (ItemEvent e)
new JLabel (String s)
new JLabel (String s, int align)align is one of JLabel.RIGHT, JLabel.LEFT, JLabel.CENTER
void setText (String s)
String getText ( )
no listeners available for JLabels
new JSlider (int orientation, int minimum, int maximum, int initialValue)orientation is one of JSlider.HORIZONTAL or JSlider.VERTICAL
void setValue (int newVal)To find out the current value, use:
int getValue ( )
ChangeListener
addChangeListener (ChangeListener al)
void stateChanged (ChangeEvent e)
new JTextField (String s)
void setText (String s)To find out the value typed, use:
String getText ( )
ActionListener
addActionListener (ActionListener al)
void actionPerformed (ActionEvent e)
Both JPanel and the object obtained by sending getContentPane() to a WindowController object are containers (and have type Container). The following methods are available for all containers.
To define the type of layout, use:
void setLayout (LayoutManager lm)LayoutManager may be any of the layout managers listed below.
To add something to a container:
void add (Component c)Component may be any Component (such as JButton, JTextField, JSlider, ...) or Container (such as JPanel). Use the method above if the container has a FlowLayout or GridLayout. Use the one below if it has a BorderLayout.
void add (Component c, int position)The position may be any of BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST,
Constructing a JPanel is very straightforward:
new JPanel ()
Constructor:
new BorderLayout ()
Constructor:
new FlowLayout ()
Constructors:
new GridLayout (int rows, int cols)
new GridLayout (int rows, int cols, int colSpacing, int rowSpacing)