Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Keep The TechnoWorldInc.com Community Clean: Read Guidelines Here.
Recent Updates
[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » JAVA
 Java: Button Press - Action Event
Pages: [1]   Go Down
  Print  
Author Topic: Java: Button Press - Action Event  (Read 3048 times)
Khushi
Global Moderator
Adv. Member
*****



Karma: 4
Offline Offline

Posts: 329

Hello!!


View Profile
Java: Button Press - Action Event
« Posted: January 03, 2007, 12:06:37 AM »


How to use the ActionEvent for buttons, list selections, etc

The ActionEvent is a class called when an action is performed on items in your GUI. As a simple example we will handle the event on a button press, the code will only output text, but you can add any code you want.

CODE:

Code:
import java.io.*; 
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*; 
import javax.swing.event.*;
public class AE extends JFrame{
   static final int    WIDTH  = 500;
    static final int    HEIGHT = 100;
   
   JButton b;
   B button;
   
   public class display extends JPanel{
      public display(){
         Container container;
         GridBagLayout layout = new GridBagLayout();
         GridBagConstraints layoutConstraints = new GridBagConstraints();
         getContentPane().setLayout(layout);
         container = getContentPane();
         
         b = new JButton("Press!");
          layoutConstraints.gridx    = 0; layoutConstraints.gridy = 0;
         layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
         layoutConstraints.fill       = GridBagConstraints.BOTH;
         layoutConstraints.insets    = new Insets(1,1,1,1);
         layoutConstraints.anchor    = GridBagConstraints.CENTER;
         layoutConstraints.weightx    = 1.0; layoutConstraints.weighty = 1.0;
         layout.setConstraints(b, layoutConstraints);
         container.add(b);
      }
   }
   
   public AE(){
      JFrame j = new JFrame();
      j.getContentPane().add(new display());
      button = new B();
      b.addActionListener(button);
       
   }
   
   private class B implements ActionListener{
      public void actionPerformed(ActionEvent e){
         System.out.println("Button Pressed");
      }
   }
   
    public static void main(String args[]){
       AE a = new AE();
       a.setSize(WIDTH,HEIGHT);
       a.setVisible(true);
    }
}

To reduce the creation of multiple instances of the press method, we create a class for it and assign it to button, thus this assignment is done once and then the single copy is called each time the button is pressed.

We assign the action code with b.addActionListener(button);

the class may have any name you like but the method name public void actionPerformed(ActionEvent e) is non-negotiable.
All code within this method will be run when this particular action occurs. Ther are many action events for each GUI object, such as document listeners for TextAreas etc.
Check the java documentation for a full list, i will post examples of a few more common ones as well.

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.161 seconds with 24 queries.