Show Menu

Java Swing/GUI Cheat Sheet by

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 ==:
aMenu.ge­tSe­lec­ted­Ite­m().equals ("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 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:
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:
Actio­nLi­stener
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:
ItemListener
ActionListener
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

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 JLabe­l.R­IGHT, JLabe­l.LEFT or JLabe­l.C­ENTER.

GUI Components - JSlider

Constr­uctor:
new JSlider (int orient­ation, int minimum,
  int maximum, int initia­lVa­lue)
General Methods:
void setValue (int newVal)
int getValue ( )
Listener Interface:
Chang­eLi­stener
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

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

Download the Java Swing/GUI Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/neonknightoa_java-swing-gui.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

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