|
Construct and initialize the component.
|
button = new JButton ("ButtonLabel"); |
|
Add it to the content pane of the window or to a JPanel that is added to the display.
|
getContentPane().add (button); |
|
Import javax.swing. and sometimes also java.awt. at the beginning of the class creating the components.
|
import javax.swing.; import java.awt. |
|
Declare that the class handling the event implements the appropriate listener interface.
|
implements ActionListener |
|
Define the method that the listener interface requires.
|
public void actionPerformed (ActionEvent event) |
|
Add a listener appropriate for the component to the component.
|
button.addActionListener (this); |
|
Import java.awt.event. (and occasionally javax.swing.event.) at the beginning of the class that is the listener.
|
import javax.swing.; import java.awt. |
|
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) { |
|
If a method returns a String, remember to compare the result using the equals method, not ==:
|
aMenu.getSelectedItem().equals ("A value"); |
|
JPanel constructor:
|
new JPanel () |
|
Define the type of layout:
|
void setLayout (LayoutManager lm) |
|
Add an object to a container:
(FlowLayout or GridLayout) |
void add (Component c) |
|
Add an object to a container:
(BorderLayout) |
void add (Component c, int position) |
BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.WEST, or BorderLayout.CENTER.
|
BorderLayout constructor:
|
new BorderLayout () |
|
FlowLayout constructor:
|
new FlowLayout () |
|
GridLayout constructor:
|
new GridLayout (int rows, int cols) |
|
The following methods can be applied to any Component:
|
void setFont (Font f) |
|
To construct a font use:
|
new Font (String name, int style, int size) |
|
Style can be one of the following:
|
Font.BOLD |
|
Constructor:
|
new JButton (String s) |
|
General Methods:
|
String getText ( ) |
|
Listener Interface:
|
ActionListener |
|
Adding the listener:
|
void addActionListener (ActionListener al) |
|
Listening Method:
|
void actionPerformed (ActionEvent e) |
|
Constructor and Initialization:
|
new JComboBox ( ) |
|
General Methods:
|
Object getSelectedItem ( ) |
|
Listener Interface:
|
ItemListenerActionListener |
|
Adding the listener:
|
void addItemListener (ItemListener il)void addActionListener (ActionListener al) |
|
Listening Method:
|
void itemStateChanged (ItemEvent e)void actionPerformed (ActionEvent e) |
getSelectedItem ( ) returns the selected item(String) menu.getSelectedItem ( ); is a typecast which treats the above returned value as a Stringint getSelectedIndex ( ) returns the index of the selected item.|
Constructors:
|
new JLabel (String s) |
|
General Methods:
|
void setText (String s) |
|
Listener Interface:
|
No listeners available.
|
JLabel.RIGHT, JLabel.LEFT or JLabel.CENTER.
|
Constructor:
|
new JSlider (int orientation, int minimum, |
|
General Methods:
|
void setValue (int newVal)int getValue ( ) |
|
Listener Interface:
|
ChangeListener |
|
Adding the Listener:
|
addChangeListener (ChangeListener al) |
|
Listening Method:
|
void stateChanged (ChangeEvent e) |
JSlider.HORIZONTAL or JSlider.VERTICAL.
|
Constructors:
|
new JTextField (String s) |
|
General Methods:
|
void setText (String s)String getText ( ) |
|
Listener Interface:
|
ActionListener |
|
Adding the Listener:
|
addActionListener (ActionListener al) |
|
Listening Method:
|
void actionPerformed (ActionEvent e) |
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.