swing-gui_cheat_sheet
swing-gui_cheat_sheet
To get events from a GUI component, you must do the following: General Methods:
String getText ( )
Declare that the class handling the event implements the appropriate listener interface. void setText (String s)
Example: implements ActionListener
Define the method that the listener interface requires. Listener Interface:
Example: public void actionPerformed (ActionEvent event)
ActionListener
Add a listener appropriate for the component to the component.
Example: button.addActionListener (this); Adding the listener:
Import java.awt.event.* (and occasionally javax.swing.event.*) at the beginning of the
class that is the listener. void addActionListener (ActionListener al)
Listening Method:
When the listener method is called, you can find out which component sent the event by calling
getSource() on the event. void actionPerformed (ActionEvent e)
If a method returns a String, remember to compare the result using the equals method, not ==: General Methods:
aMenu.getSelectedItem ().equals ("A value") To find out which item was selected, use:
If you wish to treat the value returned from this method as a String, you may use a type
The following methods can be applied to any Component:
cast:
void setFont (Font f) String text = (String) menu.getSelectedItem ( );
void setForeground (Color c)
void setBackground (Color c)
To find out the index of the item that was selected, use:
To construct a font use: int getSelectedIndex ( )
new Font (String name, int style, int size)
Listener Interface:
Find out the font names on the computer in Eclipse as follows:
The JComboBox component is unusual in that it can hear about the user making a menu
Open the Window menu selection by either implementing ItemListener or ActionListener. Be sure to be consistent
in your choice of listener interface, method to add the listener, and listening method.
Adding the Listener:
ItemListener
addChangeListener (ChangeListener al)
or
Listening Method:
ActionListener
void stateChanged (ChangeEvent e)
Adding the listener:
5. JTextField
void addItemListener (ItemListener il)
Constructors:
or
new JTextField (String s)
void addActionListener (ActionListener al)
General Methods:
Listening Method:
void setText (String s)
void itemStateChanged (ItemEvent e)
To find out the value typed, use:
or
String getText ( )
void actionPerformed (ActionEvent e)
Listener Interface:
3. JLabel
ActionListener
Constructors:
Adding the Listener:
new JLabel (String s)
addActionListener (ActionListener al)
new JLabel (String s, int align)
Listening Method:
align is one of JLabel.RIGHT, JLabel.LEFT, JLabel.CENTER
void actionPerformed (ActionEvent e)
General Methods:
void setText (String s) Containers
String getText ( )
Both JPanel and the object obtained by sending getContentPane() to a WindowController object
Listener Interface: are containers (and have type Container). The following methods are available for all containers. To
define the type of layout, use:
no listeners available for JLabels
void setLayout (LayoutManager lm)
4. JSlider
LayoutManager may be any of the layout managers listed below.
Constructor:
To add something to a container:
new JSlider (int orientation,
int minimum, int maximum, int initialValue) void add (Component c)
orientation is one of JSlider.HORIZONTAL or JSlider.VERTICAL Component may be any Component (such as JButton, JTextField, JSlider, ...) or Container (such
General Methods: as JPanel). Use the method above if the container has a FlowLayout or GridLayout. Use the one
below if it has a BorderLayout.
void setValue (int newVal)
void add (Component c, int position)
To find out the current value, use:
The position may be any of BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST,
int getValue ( )
BorderLayout.WEST, or BorderLayout.CENTER.
Listener Interface:
Constructing a JPanel is very straightforward:
ChangeListener
new JPanel ()
Layout Managers
Constructor:
new BorderLayout ()
Constructor:
new FlowLayout ()
3. GridLayout
Constructors:
new GridLayout (int rows, int cols)
new GridLayout (int rows, int cols, int colSpacing, int rowSpacing)