button in java

button in java

how can i compare names of two buttons after they are clicked?

View Answers

October 19, 2010 at 11:20 PM

Hi,

If you are developing a swing application then you can use the ActionListener class as shown below:

public class MyClass implements ActionListener {
    ...
    //where initialization occurs:
        button.addActionListener(this);
    ...
    public void actionPerformed(ActionEvent e) {
        System.out.println("button clicked");
    }
}

Thanks


October 20, 2010 at 11:15 AM

Hi Friend,

You can use the following code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Buttons extends JFrame{
    Buttons(){
        MyClass m=new MyClass();
    JFrame f=new JFrame();
    JButton b1=new JButton("A");
    JButton b2=new JButton("B");

    JPanel p=new JPanel();
    p.add(b1);
    p.add(b2);
    add(p);
    setVisible(true);
    pack();
    b1.addActionListener(m);
    b2.addActionListener(m);
}
class MyClass implements ActionListener {
public void actionPerformed(ActionEvent e){
     String choice = e.getActionCommand();
     JOptionPane.showMessageDialog(null, "You have clicked: "+choice);
  }
}
public static void main(String args[]){
    Buttons b=new Buttons();
}
}

Thanks


October 27, 2010 at 8:12 PM

hello, thank u for that kind reply. but I'm not using swings. i haven't learned that part yet,just a beginner.I'm using simple awt components. i want to keep the labels on the button same yet identify them individually. how can i do that?? please find time to respond. Thanks.









Related Tutorials/Questions & Answers:
button in java
button in java  how can i compare names of two buttons after...(ActionEvent e) { System.out.println("button clicked"); } } Thanks... a beginner.I'm using simple awt components. i want to keep the labels on the button same
java radio button and exception
java radio button and exception  Dear Sir/Madam, I have problem with my addBagelRadioButton and addCheeseRadioButton. There should be only 1 radio button can be selected, but somehow 2 can be selected the same time. Also, I
Advertisements
java button - Java Beginners
java button  i want to make form with checkboxes, after this i need button thich will open new pop-up page, where will be shown only this checkboxes..."); private JCheckBox cb4 = new JCheckBox("Check Box 4"); private JButton button
Button
Button  How to use image for button in JSF
button with rounded corner in java
button with rounded corner in java  Hi, I want to create a JButton with rounded corners in swings. I went through many programs but did not got the solutions. Thanks and regards, Vivek BirdiADS_TO_REPLACE_1
java swing button click event
java swing button click event  java swing button click event   public void doClick()   Java Swing Tutorials
creating browse button - Java Beginners
creating browse button  how can we create a browse button along with a textfield in java swings
creating browse button - Java Beginners
creating browse button  how can we create a browse button along with a textfield in java swings
creating browse button - Java Beginners
creating browse button  how can we create a browse button along with a textfield in java swings
creating browse button - Java Beginners
creating browse button  how can we create a browse button along with a textfield in java swings
creating browse button - Java Beginners
creating browse button  how can we create a browse button along with a textfield in java swings
creating browse button - Java Beginners
creating browse button  how can we create a browse button along with a textfield in java swings
creating browse button - Java Beginners
creating browse button  how can we create a browse button along with a textfield in java swings
creating browse button - Java Beginners
creating browse button  how can we create a browse button along with a textfield in java swings
creating browse button - Java Beginners
creating browse button  how can we create a browse button along with a textfield in java swings
creating browse button - Java Beginners
creating browse button  how can we create a browse button along with a textfield in java swings
creating browse button - Java Beginners
creating browse button  how can we create a browse button along with a textfield in java swings
button
button  can i give multiple commands on click of a button? if yes how can i do that?? multiple commands can we retriving dat from database, capturing data, moving to next page.. etc
button
button  can i give multiple commands on click of a button? if yes how can i do that?? multiple commands can be retriving dat from database, capturing data, moving to next page.. etc
JRadio Button - Java Beginners
JFrame("Frame in Java Swing"); f.getContentPane().setLayout(null); JLabel lbl1
How to call java method on Button click in jsp?
How to call java method on Button click in jsp?  How to call java method on Button click in jsp
creating browse button - Java Beginners
creating browse button  how can we create a browse button along with a textfield in java swings.  Hi friend, import... ActionListener { JButton button; JTextField field; public BrowsePath
Draw a line in a JPanel with button click in Java
Draw a line in a JPanel with button click in Java  Draw a line in a JPanel with button click in Java
back button issue - Java Beginners
back button issue  Hello, I have a site that asks for user and password to connect it. When the user pushs de LogOut button and later another user pushs the back button of the explorator he can entry in the page of first user
Setting icon on the button in Java
Setting icon on the button in Java   ... on the button in Java Swing. This program sets the icon on the button in Java... on the button. ImagIcon(String image_name): Above code creates an object of image
Java Button issue - Java Beginners
Java Button issue  I have this project and I have numerous steps to it. I am trying to figure out what I have to do to make my frame panel do what... is clicking the round or oval button and than perform the proper mathmathical
disable back button - Java Beginners
disable back button  I wanted the code for disabling back button in my application(The scenario is like when im in my register user page and try... displayed but..when i click back button i go back to the register page which should
Server running Button - Java Beginners
. The problem is with the display , when I run the GUI and click on the play button
Java Applet - Adding a Button to Decrease a Total
Java Applet - Adding a Button to Decrease a Total  Hello everyone... { public int testone = 0; public void init() { Button one = new Button..., it will display a button in a frame, that when clicked on will increase a total. Basically
Open website on Button Click - Java Beginners
Open website on Button Click  Hello sir I want to open website on button click using java swing plz help me sir. in my swing application one "VISIT US BUTTON" i want to open my website on Button CLick  Hi Friend
How to Hide Button using Java Swing
How to Hide Button using Java Swing  Hi, I just begin to learn java programming. How to hide the button in Java programming. Please anyone suggest or provide online example reference. Regards,   hi, In java
List the names of classes used to create button and text box in Java.
List the names of classes used to create button and text box in Java.  List the names of classes used to create button and text box in Java
wnidows button
wnidows button   How to handle windows back and forward button in struts applications, without using java script
Radio Button In Java
Radio Button In Java       Introduction In this section, you will learn how to create Radio Button on the frame. The java AWT , top-level window, are represent by the CheckBoxGroup
How to solve button problem - Java Beginners
How to solve button problem  I have created button (Open File Dialog) , List, JTabbedpane and MenuBar . By clicking btnBrowse button I am getting... equal space So button size also increasing. Could you please guide me how
Radio Button in Java
Radio Button in Java       Radio Button is a circular button on web page that can be selected by the user. The AWT  Widget used to create a radio button is Checkbox
Setting the close button operation for the frame in Java
Setting the close button operation for the frame in Java... will learn how to implement the close button of any frame or window in java application. By default the close button of the frame is not in working state. If you
How to add icons on a button or any component in java - Java Beginners
How to add icons on a button or any component in java  How to add icons on a button or any component in java?  Hi, import... main(String[] args){ JFrame frame = new JFrame("Icon on button
Java radio buttons and button groups two values
Java radio buttons and button groups two values  hi i have a problem. i have a system to input data from a jform to a mysql database. i have made... is clicked depending on which button was clicked male or female be sent
Java radio buttons and button groups two values
Java radio buttons and button groups two values  hi i have a problem. i have a system to input data from a jform to a mysql database. i have made... is clicked depending on which button was clicked male or female be sent
Hide Close Button in Java Applet - Java Server Faces Questions
Hide Close Button in Java Applet  I have an applet while running...... button or close button. Java Applet Code - Show and Hide Buttons on Applet  Java Applet code to display minimize and maximize button on Applet and code
Browser Back Button Disable/Hide/Invisible using Java Script
Browser Back Button Disable/Hide/Invisible using Java Script  Dear Sir, Please Help..., Q: How to Write Code For Browser Back Button Disable/Hide/Invisible using Java Script without Page Reload/Refreshing. anyone
Create Round Button in Java swing
Create Round Button in Java swing In this tutorial, we are going to create a button of round shape. Java2D APIs and Java Swing make it easy to implement...); } public static void main(String[] args) { JButton button = new
radio button
radio button  On selecting a radio button, it should open a aspx page
html button
html button  when i click the submit button the entered data will be displayed below the submit button please help me
JSF Command button problem - Java Server Faces Questions
JSF Command button problem  Hi Frnds... I have retrieved the records from the mdb file in backing bean..if i click the submit button those... in the form of table when i click the button for the second or third time... why cant
Swing Button Example
Swing Button Example  Hi, How to create an example of Swing button in Java? Thanks   Hi, Check the example at How to Create Button on Frame?. Thanks
Style in button
Style in button  Hi.... How can I set the button width with fx:style...;Ans: The width of the button will not set by the style because it is the property of the button not the style property of the button.ADS_TO_REPLACE_2 Thanks
Maven dependency for org.webjars.bowergithub.vaadin - vaadin-button version 2.0.0 is released. Learn to use vaadin-button version 2.0.0 in Maven based Java projects
( org.webjars.bowergithub.vaadin - vaadin-button version 2.0.0 ) in their Java project...; org.webjars.bowergithub.vaadin - vaadin-button version 2.0.0 in Java projects. Follow the step...; org.webjars.bowergithub.vaadin - vaadin-button version 2.0.0 java library in your project. ADS
Maven dependency for org.webjars.bowergithub.vaadin - vaadin-button version 2.0.0-beta3 is released. Learn to use vaadin-button version 2.0.0-beta3 in Maven based Java projects
to use  org.webjars.bowergithub.vaadin - vaadin-button version 2.0.0-beta3 in Java... 2.0.0-beta3 of vaadin-button released The developers of   org.webjars.bowergithub.vaadin - vaadin-button project have released the latest version

Ads