Showing posts with label how to create a button in java. Show all posts
Showing posts with label how to create a button in java. Show all posts

Saturday, 16 February 2019

button in java AWT | Java AWT button - Online Help

Button control in java AWT:

Button class is used to create a labeled/push button. It has a platform independent. When a button is pressed and released, AWT sends an instance of ActionEvent to the button, by calling processEvent on the button. The button's processEvent method receives all events for the button; it passes an action event along by calling its own processActionEvent method. The latter method passes the action event on to any action listeners that have registered an interest in action events generated by this button.

The constructor in java AWT button class:

There is two type of constructors available in Button class.They are:

Button()
It is used to creat an empty button.

Button(String str)
It is used to create a button with a specified string.

Methods in java AWT button class:

The most commonly used Button() method are as follows:

void addActionListener(ActionListener a)
This method is used to add the specified action listener to receive action events from responding button.

void setActionCommand(String actionstr)
This method is used to set the string that is returned by the getActionCommand() method.where actions is the string to be return from the getActionCommand() method.

void setLabel(String str)
This method is used to set the string from the button.

void setFont(Font strFont)
This method is used to set the font for the button.

void setEnabled(Boolean state)
This setEnabled method is used to enable or disable the button.


void addNotify()
Creates the peer of the button.

AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Button.

String getActionCommand()
Returns the command name of the action event fired by this button.

ActionListener[] getActionListeners()
Returns an array of all the action listeners registered on this button.

String getLabel()
Gets the label of this button.

protected String paramString()
Returns a string representing the state of this Button.

protected void processActionEvent(ActionEvent e)
Processes action events occurring on this button by dispatching them to any registered ActionListener objects.

protected void processEvent(AWTEvent e)
Processes events on this button.

void removeActionListener(ActionListener l)
Removes the specified action listener so that it no longer receives action events from this button.

void setActionCommand(String command)
Sets the command name for the action event fired by this button.

void setLabel(String label)
Sets the button's label to be the specified string.

void setBackground(Color Bgcolor)
This method is used to set the background color of the string of the button.

void setForeground(Color strcolor)
The setForeground() method is used to set the color of the string of the button.

Sample Program:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* <applet code="buttondemo" width=500 height=500>
</applet> */
public class buttondemo extends Applet implements ActionListener
{
Button b1;
TextField t1;
public void init()
{
t1=new TextField(25);
add(t1);
b1=new Button("click me");
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String msg=new String("welocme to online help");
if(e.getSource()==b1)
{
t1.setText(msg);
}
}
}