Notes on Java Swing Classes

Information on the Java Swing classes can be found at http://java.sun.com/products/jdk/1.2/docs/api/index.html

There are many objects useful for creating Graphical User Interfaces.  Some of the more useful objects are shown in the following hierarchy.

java.lang.Object

  |

  +--java.awt.Component

        |

        +--java.awt.Container

             |

              +--java.awt.Panel

              |     |

              |     +--java.applet.Applet

              |           |

              |           +--javax.swing.JApplet

              |

              +--java.awt.Window

              |     |

              |     +--java.awt.Frame

              |           |

              |           +--javax.swing.JFrame

              |

              +--javax.swing.JComponent

                    |

                    +--javax.swing.JLabel

                    |

                    +--javax.swing.text.JTextComponent

                    |     |

                    |     +--javax.swing.JTextField

                    |

                    +--javax.swing.AbstractButton

                          |

                          +--javax.swing.JButton

                          |

                          +--javax.swing.JtoggleButton

                               |

                               +--javax.swing.JRadioButton

 

Applets can extend the class JApplet, which functions similar to Applet.

A JFrame can be created by an application to hold an applet.

public JLabel(String text)

Creates a JLabel instance with the specified text.

public JTextField(String text)

Constructs a new TextField initialized with the specified text.  If text is null, the TextField is initially blank.

public JButton(String text)

Creates a button with text.

public JRadioButton(String text,

                    boolean selected)

Creates a radio button with the specified text displayed on the radio button. If selected is true, the button is initially selected; otherwise, the button is initially unselected


Methods

JComponent

public void setEnabled(boolean enabled)

Sets whether or not this component is enabled. A component which is enabled may respond to user input, while a component which is not enabled cannot respond to user input. Some components may alter their visual representation when they are disabled in order to provide feedback to the user that they cannot take input.

public void setVisible(boolean aFlag)

Makes the component visible or invisible

JTextComponent & JLabel

public void setText(String t)

Sets the text of this TextComponent to the specified text. If the text is null or empty, has the effect of simply deleting the old text.

public String getText()

Returns the text contained in this TextComponent.

JFrame & JApplet

public Container getContentPane()

Returns the contentPane object for this frame.

Container

public Component add(Component comp)

Adds the specified component to the end of this container.

Component

public void setBounds(int x,

                      int y,

                      int width,

                      int height)

Moves and resizes this component. The new location of the top-left corner is specified by x and y, and the new size is specified by width and height.

public void setSize(int width,

                    int height)

Resizes this component so that it has width width and height.

AbstractButton & JTextfield

public void addActionListener(ActionListener thing)

adds an ActionListener to the button