Show Menu

Java Swing/GUI Cheat Sheet by

Cheat Sheet based on:

Displaying a Swing component

Construct and initialize the component.
button = new JButton ("Bu­tto­nLa­bel­");
Add it to the content pane of the window or to a JPanel that is added to the display.
getCo­nte­ntP­ane­().add (button);
Import javax.s­wing. and sometimes also java.awt. at the beginning of the class creating the compon­ents.
import javax.s­wi­ng.; import java.a­wt.

Getting events from GUI component

Declare that the class handling the event implements the approp­riate listener interface.
imple­ments Action­Lis­tener
Define the method that the listener interface requires.
public void action­Per­formed (Actio­nEvent event)
Add a listener approp­riate for the component to the component.
butto­n.a­ddA­cti­onL­istener (this);
Import java.a­wt.e­vent. (and occasi­onally javax.s­wi­ng.e­ve­nt.) at the beginning of the class that is the listener.
import javax.s­wi­ng.; import java.a­wt.

Finding out which component sent the event

When the listener method is called, you can find out which component sent the event by calling getSou­rce() on the event:
public void action­Per­formed (Actio­nEvent event) {
  Object theButton = event.getSource();
  if (theButton == framed­Cir­cle­Button) {
    // Create a framed circle
If a method returns a String, remember to compare the result using the equals method, not ==:­tSe­lec­ted­Ite­m().equals ("A value");


JPanel constr­uctor:
new JPanel ()
Define the type of layout:
void setLayout (Layou­tMa­nager lm)
Add an object to a container:
(FlowLayout or GridLa­yout)
void add (Component c)
Add an object to a container:
void add (Component c, int position)
Both JPanel and the object obtained by sending getCon­ten­tPane() to a Window­Con­troller object are containers (and have type Contai­ner). These methods are available for all containers.
For Border­Lay­outs, position may be either Borde­rLa­you­t.N­ORTH, Borde­rLa­you­t.S­OUTH, Borde­rLa­you­t.E­AST, Borde­rLa­you­t.W­EST, or Borde­rLa­you­t.C­ENTER.

Layout Managers

Border­Layout constr­uctor:
new Border­Layout ()
FlowLayout constr­uctor:
new FlowLayout ()
GridLayout constr­uctor:
new GridLayout (int rows, int cols)
new GridLayout (int rows, int cols,
  int colSpa­cing, int rowSpa­cing)
Border­Layout is the default layout for Window­Con­tro­ller, whereas FlowLayout is default for JPanel.

GUI Components - General

The following methods can be applied to any Component:
void setFont (Font f)
void setFor­eground (Color c)
void setBac­kground (Color c)
To construct a font use:
new Font (String name, int style, int size)
Style can be one of the following:

GUI Components - JButton

new JButton (String s)
General Methods:
String getText ( )
void setText (String s)
Listener Interface:
Adding the listener:
void addAct­ion­Lis­tener (Actio­nLi­stener al)
Listening Method:
void action­Per­formed (Actio­nEvent e)

GUI Components - JComboBox

Constr­uctor and Initia­liz­ation:
new JComboBox ( )
void addItem (Object item)
General Methods:
Object getSel­ect­edItem ( )
String text=(String)menu.getSelectedItem();
int getSel­ect­edIndex ( )
Listener Interface:
Adding the listener:
void addIte­mLi­stener (ItemL­istener il)
void addAct­ion­Lis­tener (Actio­nLi­stener al)
Listening Method:
void itemSt­ate­Changed (ItemEvent e)
void action­Per­formed (Actio­nEvent e)
About methods:
getSe­lec­tedItem ( ) returns the selected item
(String) menu.g­etS­ele­cte­dItem ( ); is a typecast which treats the above returned value as a String
int getSel­ect­edIndex ( ) returns the index of the selected item.
About the listen­ers:
This component can hear the user making a menu selection dependong on the chosen interface. Be consistent in your choice of listener interface, adding method, and listening method.

GUI Components - JLabel

new JLabel (String s)
new JLabel (String s, int align)
General Methods:
void setText (String s)
String getText ( )
Listener Interface:
No listeners available.
align can be either JLabe­l.R­IGHT, JLabe­l.LEFT or JLabe­l.C­ENTER.

GUI Components - JSlider

new JSlider (int orient­ation, int minimum,
  int maximum, int initia­lVa­lue)
General Methods:
void setValue (int newVal)
int getValue ( )
Listener Interface:
Adding the Listener:
addCh­ang­eLi­stener (Chang­eLi­stener al)
Listening Method:
void stateC­hanged (Chang­eEvent e)
orient­ation can be either JSlid­er.H­OR­IZO­NTAL or JSlid­er.V­ER­TICAL.

GUI Components - JTextField

new JTextField (String s)
General Methods:
void setText (String s)
String getText ( )
Listener Interface:
Adding the Listener:
addAc­tio­nLi­stener (Actio­nLi­stener al)
Listening Method:
void action­Per­formed (Actio­nEvent e)


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          More Cheat Sheets by NeonKnightOA