Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 05-01-2010
Member
 
Join Date: Dec 2009
Posts: 32
Java code for button

Hello Friends,

I studying the advanced programming on JAVA. Advanced java programming seems to be more difficult to understand than core JAVA programming. Now I want to code the java program for button, but I don't have any idea about it.
If you any java code Java code for button , then please let me know that. I would appreciate your help.
Reply With Quote
  #2  
Old 05-01-2010
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: Java code for button

Refer following java program for button:
Quote:
import java.awt.event.*;
import java.awt.*;

public class ButtonDemo {
public static void main(String args[])
{
Button b;
ActionListener a1 = new MyActionListener();

Frame f1 = new Frame("Java Applet");

f1.add(b1 = new Button("BonjourDemo"), BorderLayout.SOUTH);

b1.setActionCommand("Welcome");

b1.addActionListener(a1);

f1.add(b1 = new Button("NIce day"), BorderLayout.SOUTH);

b1.addActionListener(a1);
b1.addActionListener(a1);

f1.pack();

f1.show();
}
}

class MyActionListenerDemo implements ActionListener {

public void actionPerformed(ActionEvent ae)
{
String s1 = ae1.getActionCommand();

if (s1.equals("ButtonName")) {
System.exit(0);
}
else if (s1.equals("BonjourDemo")) {
System.out.println("Welcome");
}

}
}
Reply With Quote
  #3  
Old 05-01-2010
Member
 
Join Date: May 2008
Posts: 2,008
Re: Java code for button

If you want to include the buttons, check boxes in java the use following different java classes for that:
  • JButton(A common button)
  • JRadioButton(One of a group of radio buttons)
  • JCheckBoxMenuItem(A menu item that has a check box)
  • JToggleButton(Implements toggle functionality inherited by JCheckBox and JRadioButton)
  • JCheckBox(A check box button)
Reply With Quote
  #4  
Old 05-01-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Java code for button

Hi,

I have following java event program. In which I have used the "MyActionListener " class concept to fire the various events for buttons.
Quote:
import java.awt.event.*;

class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
if (s.equals("Hello"))
{
System.out.println("Hello everyone");

}
else {
System.exit(0);
}
}
}
public class JavaButtonDemo {
public static void main(String[] args){
Button b;
ActionListener ab = new MyActionListener();
Frame fk = new Frame("java");
fk.add(lb = new Button("Hello"), BorderLayout.NORTH);

bl.setActionCommand("Hello");
bl.addActionListener(ab);
fk.pack();
fk.show();
}
}
Reply With Quote
  #5  
Old 05-01-2010
Member
 
Join Date: May 2008
Posts: 2,293
Re: Java code for button

Hi,
To code the java program for button, you need to use the properties of the "ActionListener" class . The "ActionListener" basically comprises properties like "setActionCommand", "addActionListener", "getActionCommand". setActionCommand is used to pass the value to the getActionCommand. The you need to put the code hat you want to apply for button into the "getActionCommand". You can use nore than one events to the single button in the java. You can also use the multiple button in the single program.
Reply With Quote
  #6  
Old 06-01-2010
Member
 
Join Date: Nov 2009
Posts: 140
Re: Java code for button

To code for "actionPerformed" method use the following code for reference. I have used multiple properties of the "actionPerformed" method in this java program for button.
Quote:
public void actionPerformed(ActionEvent p)
{
if ("emp".Equals(p.getActionCommand()))
{
m1.setEnabled(true);
m2.setEnabled(false);
m3.setEnabled(false);

}
else
{
m1.setEnabled(False);
m2.setEnabled(true);
m3.setEnabled(true);
}
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Java code for button"
Thread Thread Starter Forum Replies Last Post
help me to understand java code (calendar java code) ? so0oma Software Development 2 10-01-2011 12:01 AM
Disabling the Close Button on a JFrame in Java technika Software Development 5 24-02-2010 10:51 PM
How to invoke a form from a button click using Java Swing? shelton141 Software Development 1 11-09-2009 07:54 AM
Save button code in visual basic 2008 Chrisch Software Development 4 23-08-2009 01:20 AM
How to create button on frame in java? JagdishP Software Development 4 10-08-2009 06:04 PM


All times are GMT +5.5. The time now is 10:03 PM.