ContainersTopGeneral remindersGUI Components

GUI Components

The following methods can be applied to any Component:

    void setFont (Font f)
    void setFont (String fs)
    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:

Be sure that the string you pass in setFont or to create a new font is spelled and capitalized exactly as shown in the list of fonts.

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<String> ( )
    void addItem (String item)
    

    The <String> must be omitted in the constructor in Java 6 (or below), but included in Java 7 or greater.

    General Methods:

    To find out which item was selected, use:

    Object getSelectedItem ( )
    

    To find out the index of the item that was selected, use:

    int getSelectedIndex ( )
     
    Listener Interface:
    ActionListener
    
    Adding the listener:
    void addActionListener (ActionListener al)
    
    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