Results 1 to 6 of 6

Thread: Java code for button

  1. #1
    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.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Java code for button

    Refer following java program for button:
    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");
    }

    }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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)

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    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.
    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();
    }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    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.

  6. #6
    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.
    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);
    }
    }

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,495,718.60185 seconds with 16 queries