Home arrow Java & J2EE arrow Page 3 - Java's Basic User Interface Components

The Button Class - Java

To simplify user interaction and make data entry easier, you can use java controls. Controls are components, such as buttons, labels and text boxes, that can be added to containers like frames, panels and applets. The Java.awt package provides an integrated set of classes to manage user interface components.

  1. Java's Basic User Interface Components
  2. Adding a Control to a Container
  3. The Button Class
  4. The Label Class
  5. The Checkbox Class
  6. The CheckboxGroup Class
  7. The Choice List Class
  8. The TextField and TextArea Class
  9. Setting a Password Character
  10. TextArea
By: Gayathri Gokul
Rating: starstarstarstarstar / 22
October 20, 2003

print this article


We will start with the simplest of UI components: the button. Buttons are used to trigger events in a GUI environment (we have discussed Event Handling in detail in the previous tutorials). They are easy to manage and, most importantly, they are easy to use. The Button class is used to create buttons. When you add components to the container, you don’t specify a set of coordinates that indicates where the components are to be placed. The arrangement of components is handled by a layout manager in effect for the container. The default layout for a container is flow layout. Now let us write a simple code to test our button class.
To create a button use, one of the following constructors:
  • Button() creates a button with no text label.
  • Button(String) creates a button with the given string as label.

Example 1

<Applet code= “ButtonTest.class”
Width = 500
Height = 100>

Import java.awt.*;
Public class ButtonTest extends java.applet.Applet
Button b1 = new Button (“Play”);
Button b2 = new Button (“Stop”);

Public void init(){

The following Java application illustrates addition of button controls to a panel.

Example 2

Import java.awt.*;

Public class ButttonTest2 extends Frame
Public static void main (String arg[])
Frame frm;
Panel panel,
Button b1, b2,
frm= new Frame(“Using Frame As Container For-Button Test”);
frm.setBackground (Color.green);

panel = new Panel();
b1 = new Button(“Accept”);
b2 = new Button(“Cancel”);


We can use the setLabel( ) method to change the label of the button and the getLabel( ) method to retrieve the caption.

>>> More Java & J2EE Articles          >>> More By Gayathri Gokul

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: