Add to Google Reader or Homepage

JButton - An Example

Creating a Button in JFrame


Buttons are one of the most commonly used components in any UI based application.In this post we are going to see how to create a button and how to assign a event handler to that button.


Creating a button in java is very simple and it involves four steps,

1.Creating the button

2.adding the button to the container(frame or panel)

3.creating the Listeners for buttons

4.associating those Listeners to the buttons

First Step,creating the button is as easy as follows

Jpanel p=new JPanel();
JButton button=new JButton("NEXT");
p.add(button);

Here you have to note one thing,simply creating a button will not register itself with the Event Listener and its your job to register a button with the Event Handler and to define actions that you desire when you click the button,

I have included a simple program that explains how to create buttons and registering with a Event handler

Program


Flowlayoutmain

import javax.swing.*;
class Flowlayoutmain
{
public static void main(String args[])
{
Flowlayoutdemo f1=new Flowlayoutdemo();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
}
}


Flowlayoutdemo

import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;


class Flowlayoutdemo extends JFrame
{
public Flowlayoutdemo()
{
setTitle("FlowLayoutDemo");
setLocationByPlatform(true);
setSize(400,400);
BorderLayout b=new BorderLayout();
setLayout(b);

JPanel p=new JPanel();
JTextArea ta=new JTextArea(12,35);
p.add(ta);
Border bo=BorderFactory.createEtchedBorder();
p.setBorder(bo);
add(p,BorderLayout.SOUTH);


JPanel jp=new JPanel();

//creating the buttons
JButton button1=new JButton("NEXT");
JButton button2=new JButton("CANCEL");

//adding the buttons to the panel
jp.add(button1);
jp.add(button2);
add(jp,BorderLayout.CENTER);

//creating the Listeners for buttons

BListener b1=new BListener("You have pressed NEXT Button",ta);
BListener b2=new BListener("You have pressed CANCEL Button",ta);

//associating those Listeners to the buttons
button1.addActionListener(b1);
button2.addActionListener(b2);

}
}

class BListener implements ActionListener
{
public BListener(String text,JTextArea a)
{
 txt=text;
 a1=a;
}
public void actionPerformed(ActionEvent e)
{
a1.setText(txt);
}
private String txt;
private JTextArea a1;
}

Output:



The first two steps seems to be very easy and so let's go to the third step

Creating the listeners

 What do you mean by listeners?,In general a listener is someone who listens to you when you do something(speaking,dancing..)

                   In this case,a listener is an object that listens to a button click.

Now,we need to create the Listeners(listener objects) for each buttons along with the actions that you desire.To create a Listener object,the class of the object must implement the ActionListener interface. 

So I have created objects for the class BListener which implements ActionListener interface and specified the actions that I would like to do.


BListener b1=new BListener("You have pressed NEXT Button",ta);
BListener b2=new BListener("You have pressed CANCEL Button",ta);

 Associating the listeners to  the buttons


After creating the listeners you have to associate them with the buttons using the addActionListener() method as follows,

button1.addActionListener(b1);
button2.addActionListener(b2);



 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top