ContainersTopGeneral remindersGUI Components

GUI Components

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)

Find out the font names on the computer in Eclipse as follows:

Style can be one of Font.BOLD, Font.ITALIC, Font.PLAIN, or Font.BOLD + Font.ITALIC.

The specific components we have considered:

  1. JButton
    Constructor:
    new JButton (String s)
    
    General Methods:
    String getText ( )
    void setText (String s)
    
    Listener Interface:
    ActionListener
    
    Adding the listener:
    void addActionListener (ActionListener al)
    
    Listening Method:
    void actionPerformed (ActionEvent e)
    
  2. JComboBox
    Constructor and Initialization:
    new JComboBox ( )
    void addItem (Object item)
    
    General Methods:

    To find out which item was selected, use:

    String getSelectedItem ( )
    
    To find out the index of the item that was selected, use:
    int getSelectedIndex ( )
     
    Listener Interface:
    ActionListener
    
    Adding the listener:
    void addActionListener (ItemListener il)
    
    Listening Method:
    void actionPerformed (ActionEvent e)
    
  3. JLabel
    Constructors:
    new JLabel (String s)
    
    new JLabel (String s, int align)
    
    align is one of JLabel.RIGHT, JLabel.LEFT, JLabel.CENTER
    General Methods:
    void setText (String s)
    
    String getText ( )
    
    Listener Interface:

    no listeners available for JLabels

  4. JSlider
    Constructor:
    new JSlider (int orientation, 
                  int minimum, int maximum, int initialValue)
    
    orientation is one of JSlider.HORIZONTAL or JSlider.VERTICAL
    General Methods:
    void setValue (int newVal)
    
    To find out the current value, use:
    int getValue ( )
    
    Listener Interface:
    ChangeListener
    
    Adding the Listener:
    addChangeListener (ChangeListener al)
    
    Listening Method:
    void stateChanged (ChangeEvent e)
    
  5. JTextField
    Constructors:
    new JTextField (String s)
    
    General Methods:
    void setText (String s)
    
    To find out the value typed, use:
    String getText ( )
    
    Listener Interface:
    ActionListener
    
    Adding the Listener:
    addActionListener (ActionListener al)
    
    Listening Method:
    void actionPerformed (ActionEvent e)
    

ContainersTopGeneral remindersGUI Components