Go Back   Java Programming Forums > Learning Java > Java Code Snippets and Tutorials

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-03-2009, 01:27 PM
JavaPF's Avatar
mmm.. coffee
 
5 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,211
Thanks: 61
Thanked 66 Times in 64 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Inspired
Post How to Add ActionListener to a JButton. Java Swing

This code is an example of how to add ActionListener to a JButton.

When the JButton is clicked, the desired action is performed. In this case it prints a message to the console.

Java Code
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ButtonAction {

    private static void createAndShowGUI()  {

        JFrame frame1 = new JFrame("JAVA");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton(" >> JavaProgrammingForums.com <<");
        //Add action listener to button
        button.addActionListener(new ActionListener() {
               
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked the button");
            }
        });      

        frame1.getContentPane().add(button);
        frame1.pack();
        frame1.setVisible(true);
    }
     

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}



__________________
Don't forget to add code tags around your code:

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote Share this thread on Facebook
Sponsored Links
  #2 (permalink)  
Old 07-07-2009, 04:54 PM
Junior Member
 

Join Date: Jul 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
IDK12 is on a distinguished road
Default Re: How to Add ActionListener to a JButton. Java Swing

Thank you for helping. I think this book is moving a bit too quick for me. My friend says he learned nothing about Gui until his second year. I have started reading a new book, and I will post another question soon. Thanks again.

IDK12
Reply With Quote
  #3 (permalink)  
Old 20-10-2009, 07:13 AM
Junior Member
 

Join Date: Oct 2009
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
chinni is on a distinguished road
Default Re: How to Add ActionListener to a JButton. Java Swing

inside the post method u can call get method. if u r sending post request.
if u r sending get request u can call post method inside the Get method
try it once.
Reply With Quote
  #4 (permalink)  
Old 20-10-2009, 11:15 AM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 861
Thanks: 31
Thanked 112 Times in 108 Posts
Json will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
Default Re: How to Add ActionListener to a JButton. Java Swing

Are you on about servlets chinni?

// Json
Reply With Quote
Reply

Tags
actionlistener, java swing, jbutton

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Add a JMenu toolbar to a Java Swing application JavaPF Java Code Snippets and Tutorials 2 04-05-2009 02:56 PM
JAVA ImageIcon and JButton resizing problem -- please go easy on me. :)? antitru5t AWT / Java Swing 1 13-03-2009 09:39 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java convert double to int java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java jbutton java list to map java mouse position java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java jtextarea font size programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 05:29 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.