Show Menu
Cheatography

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.
getCon­ten­tPa­ne(­).add (button);
Import javax.s­wing. and sometimes also java.awt. at the beginning of the class creating the compon­ents.
import javax.s­wing.; import java.awt.

Getting events from GUI component

Declare that the class handling the event implements the approp­riate listener interface.
implements 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.
button.ad­dAc­tio­nLi­stener (this);
Import java.a­wt.e­vent. (and occasi­onally javax.s­wi­ng.e­vent.) at the beginning of the class that is the listener.
import javax.s­wing.; import java.awt.

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 ==:
aMenu.g­et­Sel­ect­edI­tem­().e­quals ("A value");

Containers

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:
(BorderLayout)
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
Border­Lay­out.NORTH
,
Border­Lay­out.SOUTH
,
Border­Lay­out.EAST
,
Border­Lay­out.WEST
, or
Border­Lay­out.CENTER
.

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:
Font.BOLD
Font.ITALIC
Font.PLAIN
Font.BOLD+Font.ITALIC

GUI Components - JButton

Constr­uctor:
new JButton (String s)
General Methods:
String getText ( )
void setText (String s)
Listener Interface:
Action­Lis­tener
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:
ItemLi­stener

Action­Lis­tener
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:
getSel­ect­edItem ( )
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 listeners:
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

Constr­uctors:
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
JLabel.RIGHT
,
JLabel.LEFT
or
JLabel.CENTER
.

GUI Components - JSlider

Constr­uctor:
new JSlider (int orient­ation, int minimum,
  int maximum, int initia­lValue)
General Methods:
void setValue (int newVal)

int getValue ( )
Listener Interface:
Change­Lis­tener
Adding the Listener:
addCha­nge­Lis­tener (Chang­eLi­stener al)
Listening Method:
void stateC­hanged (Chang­eEvent e)
orient­ation can be either
JSlide­r.H­ORI­ZONTAL
or
JSlide­r.V­ERTICAL
.

GUI Components - JTextField

Constr­uctors:
new JTextField (String s)
General Methods:
void setText (String s)

String getText ( )
Listener Interface:
Action­Lis­tener
Adding the Listener:
addAct­ion­Lis­tener (Actio­nLi­stener al)
Listening Method:
void action­Per­formed (Actio­nEvent e)
 

Comments

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