?
Solved

Action Listener Q

Posted on 2003-03-22
10
Medium Priority
?
469 Views
Last Modified: 2013-11-23
How can I add an action listener to a button that reloads images onto a
page

I can't figure out how to use HandButton to refresh the card images of a hand
on a page.





import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 *  Description of the Class
 *
 *@author     john
 *@created    March 21, 2003
 */
class doGui {
     String[] Hand;


     /**
      *  Constructor for the doGui object
      *
      *@param  pHand  Description of the Parameter
      */
     doGui(String[] pHand) {
          Hand = pHand;
     }


     /**
      *  Description of the Method
      */
     public void doPanel() {
          JFrame frame = new JFrame("Poker Hand");
// create a JPanel using a GridLayout with 5 cells and 2 pixel spacing
          JPanel p = new JPanel(new GridLayout(1, 5, 2, 2));
          JPanel bp = new JPanel(new GridLayout(1, 4, 2, 2));

          JButton handButton = new JButton("   New Hand   ");
          JButton sortButton = new JButton("Sort Remaining Cards");
          JButton shuffleButton = new JButton("Shuffle Deck");
          JButton exitButton = new JButton("   Exit   ");

          bp.add(handButton);
          bp.add(sortButton);
          bp.add(shuffleButton);
          bp.add(exitButton);
          exitButton.addActionListener(
               new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                         System.exit(0);
                    }
               });
               
          handButton.addActionListener(
               new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                         Deck deck = new Deck();

                    }
               });              

// show the dialog box
          frame.getContentPane().add(addImages(p), BorderLayout.NORTH);
          frame.getContentPane().add(bp, BorderLayout.SOUTH);
          frame.pack();
          frame.setVisible(true);

     }


     /**
      *  Adds a feature to the Images attribute of the doGui object
      */
     public JPanel addImages(JPanel p) {

          // add 5 JLabel objects (each with its own ImageIcon) to the JPanel

          for (int c = 0; c < 5; c++) {
               p.add(new JLabel(new ImageIcon("C:\\java\\dev\\pokerg\\Cards\\" + Hand[c] + ".gif")));

          }
          return p;
     }

}

0
Comment
Question by:dowland
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 

Expert Comment

by:fivesigmaevent
ID: 8188802
1. Implement an ActionListener for one of your classes. I believe your class has to be of type component to be able to implement event handling of actions. I made your class exend Component to do this:

class doGui extends Component implements ActionListener
{


2. In that class, then implement the actionPerformed() method. Would look something like so:

   public void actionPerformed( ActionEvent ae )
   {
      if( ae.getSource() == handButton )
      { // event was generated from the hand button


3. Finally, register the object that implements the action listener from within the class like so:

  handButton.addActionListener( this );

That should just about cover it. Lemme know how you make out.
0
 
LVL 92

Expert Comment

by:objects
ID: 8188817
You just need to recreate new Image objects.
0
 
LVL 92

Expert Comment

by:objects
ID: 8188822
You've already created your ActionListener, you just need it call a method that creates new Image instances.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:fivesigmaevent
ID: 8188833
I'm not familiar with what obects is saying. But he's usually right. The method I posted has worked for me before.

OT - objects, would you be available if I had some questions about swing? I am low on points (seems to be a bug in the point allocating), like 90 pts.
0
 
LVL 92

Expert Comment

by:objects
ID: 8188857
> would you be available if I had some questions about swing?

i'm about to knock off for the day, but i'll have a look tomorrow.
0
 

Expert Comment

by:fivesigmaevent
ID: 8188888
Yeah, getting tired here too. :-) Will write up something tomorrow having to do with JSlider with your name in the subject. Thanks.
0
 

Author Comment

by:dowland
ID: 8189072
I tried this, it breaks.




handButton.addActionListener(
     new ActionListener() {                    public void actionPerformed(ActionEvent e) {
          refreshImages();
                    }
               });




public void refreshImages() {

  String[] Hand = Deck.getHand();
  System.out.println("count " + p.getComponentCount());
  //Breaks
  for (int c = 0; c < 5; c++) {
      p.add(new JLabel(new                                 ImageIcon"C:\\java\\dev\\pokerg\\Cards\\" +
                Hand[c] ".gif")));

          }
     }


  //Breaks
0
 
LVL 92

Expert Comment

by:objects
ID: 8189079
Where is p defined?
It needs to be a member variable.
0
 

Author Comment

by:dowland
ID: 8190437
OK - no syntax errors and no runtime crash, bt the new images never show.  How do I get them to show?

The new code is below:


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 *  Description of the Class
 *
 *@author     john
 *@created    March 21, 2003
 */
class doGui {
     String[] Hand;
     Deck Deck;
     GridLayout g = new GridLayout(1, 5, 2, 2);
     JPanel p = new JPanel(g);
     JFrame frame = new JFrame("Poker Hand");
     JPanel bp = new JPanel(new GridLayout(1, 4, 2, 2));


     /**
      *  Constructor for the doGui object
      *
      *@param  pHand  Description of the Parameter
      *@param  pDeck  Description of the Parameter
      */
     doGui(String[] pHand, Deck pDeck) {
          Hand = pHand;
          Deck = pDeck;
     }



     /**
      *  Description of the Method
      */

     public void refreshPanel() {

     }


     /**
      *  Description of the Method
      */
     public void doPanel() {

// create a JPanel using a GridLayout with 5 cells and 2 pixel spacing
          //JPanel p = new JPanel(new GridLayout(1, 5, 2, 2));


          JButton handButton = new JButton("   New Hand   ");
          JButton sortButton = new JButton("Sort Remaining Cards");
          JButton shuffleButton = new JButton("Shuffle Deck");
          JButton exitButton = new JButton("   Exit   ");

          bp.add(handButton);
          bp.add(sortButton);
          bp.add(shuffleButton);
          bp.add(exitButton);
          exitButton.addActionListener(
               new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                         System.exit(0);
                    }
               });
          ActionListener handbut =
               new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                         p.removeAll();
                         p.setLayout(g);
                         Hand = Deck.getHand();
                         p.repaint();
                         for (int c = 0; c < 5; c++) {
                              p.add(new JLabel(new ImageIcon("C:\\java\\dev\\pokerg\\Cards\\" + Hand[c] + ".gif")));
                              System.out.println(Hand[c]);
                         }
                         p.repaint();
                         System.out.println("\n");
                    }
               };
          handButton.addActionListener(handbut);

// show the dialog box
          frame.getContentPane().add(addImages(p), BorderLayout.NORTH);
          frame.getContentPane().add(bp, BorderLayout.SOUTH);
          frame.pack();
          frame.setVisible(true);

     }


     /**
      *  Adds a feature to the Images attribute of the doGui object
      *
      *@param  p  The feature to be added to the Images attribute
      *@return    Description of the Return Value
      */
     public JPanel addImages(JPanel p) {

          // add 5 JLabel objects (each with its own ImageIcon) to the JPanel

          for (int c = 0; c < 5; c++) {
               p.add(new JLabel(new ImageIcon("C:\\java\\dev\\pokerg\\Cards\\" + Hand[c] + ".gif")));
               System.out.println(Hand[c]);
          }
          System.out.println("\n");
          return p;
     }

}
0
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 8191917
Try:

  p.removeAll();
  Hand = Deck.getHand();
  for (int c = 0; c < 5; c++) {
    p.add(new JLabel(new ImageIcon("C:\\java\\dev\\pokerg\\Cards\\" + Hand[c] + ".gif")));
    System.out.println(Hand[c]);
  }                      
  revalidate();
  repaint();
  System.out.println("\n");
               
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question