Mrs. Howe's GUI-Cookbook for Java

If you choose not to use a pre-set Layout and use the “null” option.  You must use methods,

.setSize( # , # ) and

.setLocation( # , # )

before you add these objects to the window (Pane.)

  JButton   (like a command button)

            Constructors: 

                             JButton( String caption”);

                             JButton(picturefilename”);

                             JButton(“String caption”, “picturefilename);

                    Methods

                               .setText (“String caption”);

                             .setIcon(“picturefilename”);

                             .requestFocus();

 

JCheckBox   (like a check box)

              Constructors: 

                             JCheckBox( String text”,true or false);

                             JCheckBox(picturefilename”,true or false);

                             JCheckBox(“String text”, “picturefilename”,true or false);

                    Methods

                               .isSelected();

                             .setSelected(true or false);

                             .setText(“String text”);

                             .setIcon(“picturefilename”);

JComboBox   (Like combo box)

Constructors: 

                             JComboBox( arrayname );

                            

                    Methods

                             .getSelectedIndex()

                               .getSelectedItem();

                             .setSelectedIndex(int);

                             .setSelectedItem(“String text”);             

JLabel    (like label, image box)

Constructors: 

                             JLabel( “String Caption” );

                             JLabel( “picturefilename” );

                    Methods

                             .setText(“string text”)

                               .setIcon(“picturefilename”);

JRadioButton  (Like option box)

Constructors: 

                             JRadioButton( String text”);

                             JRadioButton( String text”true false”);

                             JRadioButton( “picturefilename”);

                             JRadioButton( “picturefilename”,true false);

                             JRadioButton( “String text”,“picturefilename”,true false);

                    Methods

                             .isSelected();

                             .setSelected(true or false);

                             .setText(“String text”);

                             .setIcon(“picturefilename”);

JSlider   (Like scroll bar)

          import javax.swing.event.*;

 

Constructors: 

                             JSlider( SwingConstants.VERTICAL, min, max, value now);

                            

                    Methods

                             .getInverted(true false);

                               .getSelectedItem();

                             .setMajorTickSpacing(int);

                             .setMinorTickSpacing(int);

                             .getValue();

                             .set PaintLabels(true false);

                             .set PaintTicks(true false);

                             .setValue();

 

JTextArea  (Like text box)

Constructors: 

                             JTextArea( String text”);

                             JTextArea(rows, cols);

                             JText(“String text”, rows, cols);

                    Methods

                               .setEditable(true false);

                             .getCaretPosition();

                             .append( “String text”);

                     .insert( String text”,pos);

                     .setText( “String text”);

                     .setBackground( Color.color);

                             .setForeground( Color.color);

                             .setFont(“fontname”);

                     .setText( “String text”);

                     .requestFocus();

                             .getText();

                             .selectAll();

                             .setCaretPosition(pos);

                             .moveCaretPosition(pos);

JTextField  (Like text box multi line)

Constructors: 

                             JTextArea( String text”);

                             JTextArea( cols);

                             JText(“String text”,  cols);

                    Methods

                               .setEditable(true false);

                     .setText( “String text”);

                     .setBackground( Color.color);

                             .setForeground( Color.color);

                             .setFont(“fontname”);

                     .setText( “String text”);

                     .requestFocus();

                             .getText();

                             .selectAll();

                             .setCaretPosition(pos);

                             .moveCaretPosition(pos);

 

JToggleButton  (Like command button--changes appearance)                 

Constructors: 

                             JToggleButton( String text”true false”);

                             JToggleButton( “picturefilename”);

                             JToggleButton( “picturefilename”,true false);

                             JToggleButton( “String text”,“picturefilename”,true false);

                    Methods

                             .isSelected();

                             .setSelected(true or false);

                             .setText(“String text”);

                             .setIcon(picturefilename”);

************************

ACTION CODE

************************

 

For Buttons....using anonymous inner class. (Similar to click event)

import java.awt.event.*;   remember this header

 

nameofbutton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
  CODE YOU WANT TO HAVE HAPPEN ON CLICK
}
});

************************

MOUSE CODE

************************