Link to home
Start Free TrialLog in
Avatar of dowland
dowland

asked on

Action Listener Q

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;
     }

}

Avatar of fivesigmaevent
fivesigmaevent

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.
Avatar of Mick Barry
You just need to recreate new Image objects.
You've already created your ActionListener, you just need it call a method that creates new Image instances.
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.
> 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.
Yeah, getting tired here too. :-) Will write up something tomorrow having to do with JSlider with your name in the subject. Thanks.
Avatar of dowland

ASKER

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
Where is p defined?
It needs to be a member variable.
Avatar of dowland

ASKER

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;
     }

}
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial