?
Solved

Action Listener Q

Posted on 2003-03-22
10
Medium Priority
?
476 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

569 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