Library tutorials & articles

Programming with Swing

Listening for Action

To Listen for an action like a mouse click or key press you use a Listener class. There are several different types of Listener classes and this tutorial only talks about one of them. To find out about the rest, consult a Swing Reference book like the one suggested earlier. I like to use my Listeners as internal classes, so if you don't like that, TOO BAD!

We will be using ActionListener to learn how to use Listener objects. ActionListener listens for a mouse click over a component. We will use it with a JButton to do something when the JButton is clicked.

Here's the code to get a JFrame with a JPanel and a JButton:

JFrame frame = new JFrame("ActionListener Test");
JPanel panel = new JPanel();
JButton button1 = new JButton("Click Me!");
frame.setSize(256,256);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
panel.add(button1);
frame.pack();
frame.show();

Now we want to add a new ActionListener to JButton button1 :

button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1) {
// CODE TO RUN WHEN BUTTON CLICKED GOES HERE
}});

 I realize that is probably a little simplistic, but we want to keep things simplistic, don't we? Of course we do! So all this code does is make an internal class of
an ActionListener with it's method actionPerformed(ActionEvent e1) that runs when the ActionListener detects (hears?) an action such as our button being clicked.

That's the basics of Action Listening, for more, go buy a good book. Get off the computer! Go do something, ride your bicycle, or something! A book is better for your eyes anyway than the RF Radiation coming from this screen! whew, there's my rant for this section.

Conclusion

I hope you had fun, Java is a neat language. More people should use it, as it has a large and usefull API and it runs on so many platforms. The interpreter has also gotten quite fast, so speed is no longer a reason for not using it.

I recommend going to http://java.sun.com/ and using the API docs, they are essential!

Comments

  1. 14 Nov 2005 at 15:01

    Hello,
     I want to add a closing button to the internal frame. I am not able to do it. Plz help.javascript:smilie('')
    confused

  2. 28 Oct 2004 at 07:05

    Hi.


    I have an ChildFrame inside of an DesktopFrame. I want to insert a JButton into the ChildFrame. But this JButton turns out to be very  tiny in that child frame. If I maximize the child frame, the button does not change size.
    I used this a source for my codings:


    http://www.rz.fhtw-berlin.de/hjp3/k100230.html


    I only added a JButton into my chid frame, I used a GridBagLayout. button.setSize() does  not seem to work in  that child frame.


    Would be very thankful, if anybody could help.


    Burkhardt


  3. 13 Sep 2004 at 04:21

    jho....


    just call your child frames as follows


    public class MainFrame extends JFrame
    {
     ChildFrame1 child1;
    public MainFrame()
    {
     final JDesktopPane desktop = new JDesktopPane();
     getContentPane().add(desktop);
     
     JMenuBar bar = new JMenuBar();
    JMenu show = new JMenu("Show");
    JMenuItem showchild1 = new JMenuItem("Show Child1");
    //*******
    // here this represents the MainFrame which can be passes as a message to the child frame.
             child1 = new ChildFrame1(this);
    //
    *******
    desktop.add(child1);
    child1.setVisible(true);
     
    bar.add(show);
    show.add(showchild1);
    }
    }



    and in ChildFrame set the constructor as


    MainFrame  mainFrame;
    ChildFrame(MainFrame  mainFrame)
    {
    this.mainFrame = mainFrame;
    }


    actionPerformed in ChildFrame1()  {
     if(button clicked) {
        ChilFrame2 childFrame2 = new ChildFrame2();
         mainFrame.desktop.add(childFrame2);
         this.dispose(); // or this.hide();
     }
    }


    hope this code helps!


  4. 05 Aug 2004 at 01:44

    i have a JFrame named MainFrame which also uses JDesktoPane. I created 2 JInternalFrame class named ChildFrame1 and ChildFrame2. When I can click the menu on the mainframe, it shows the childframe1. I must load the Childframe2 on the desktopPane on the mainFrame by clicking a button inside the childframe1. How can I do this??? My problem is that the desktopPane becomes foreign to childFrame2 since it is another class. here is the sample prog...


    public class MainFrame extends JFrame
    {
      ChildFrame1 child1 = new ChildFrame1();
    public MainFrame()
    {
      final JDesktopPane desktop = new JDesktopPane();
      getContentPane().add(desktop);
     
      JMenuBar bar = new JMenuBar();
     JMenu show = new JMenu("Show");
     JMenuItem showchild1 = new JMenuItem("Show Child1");
     desktop.add(child1);
     child1.setVisible(true);
     
    bar.add(show);
    show.add(showchild1);



    }


    }


    i still have another JInternalFrame class named childFrame2 but it will only be visible when i click a button in childFrame1 (child1).


    how can i do this?????


    i cannot use the jDESKTOPANE "desktop" because that class does not recognize the "desktop"



    PLEASE HELP ME!!! I WILL DEFINITELY APPRECIATE YOUR HELP! THANKS A LOT!!!

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Programming with Swing.

Leave a comment

Sign in or Join us (it's free).

Michael H

Related podcasts

  • Java Posse #213 - Newscast for Oct 23rd 2008

    Newscast for Oct 23rd 2008 Fully formatted shownotes can always be found at http://javaposse.com The Android project has been released as open source, beating the rumored launch date for the source code by several months http://source.android.com/ And, Gizmodo and ZDNet both offer in-depth ...

Events coming up

  • Dec 15

    Portland Java User Group

    Portland, United States

    This month's topic: TBD----------PJUG meetings start with eat+meet+greet time (pizza and beverages are provided), followed by the featured speaker, then some time for Q&A, discussion, and sometimes a drawing to give away swag. :)It is...

We'd love to hear what you think! Submit ideas or give us feedback