Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java: JButton

JavaFAQ Home » Swing, AWT Go to all tips in Swing, AWT


Bookmark and Share

Description

There are a few steps in using a button: declaring it, creating it, adding it to a container (the content pane or a JPanel), and adding a listener that has code to execute when the user clicks on the button. Images can be used on buttons, including automatic rollover effects.

Constructors

Assume these declarations.
   String text;
   Icon   image;
   
JButton btn = new JButton(text);
JButton btn = new JButton(text, image);
JButton btn = new JButton(image);

Common methods

Assume these declarations:
   JButton btn;
   ActionListener listener;
   boolean b;
   
   //--- Buttons always have action listeners.
   btn.addActionListener(listener);
   btn.setEnabled(b);
   

Events

When a button is clicked, the actionPerformed() method is called for all of the button's listeners. It is passed an ActionEvent, which is generally ignored, but can be used to identify which component generated the event if several share the same listener. The example below shows the creation of a button, attaching a listener, and adding the button to a container.

Example

Typically a button is assigned to a local variable (not an instance variable) and an anonymous action listener is added to it. The listener often calls another method to do everything because separating the code to build the user interface and do the "semantics" makes a program clearer and easier to modify.
JButton mybtn = new JButton("Do Something");
mybtn.addActionListener(
    new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            doMyAction();  // code to execute when button is pressed
        }
    }
);
. . .
content.add(mybtn);  // add the button to a JPanel (eg, content).

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy