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.addActionListen er(
new ActionListener() {
public void actionPerformed(ActionEven t e) {
System.exit(0);
}
});
handButton.addActionListen er(
new ActionListener() {
public void actionPerformed(ActionEven t 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\\Ca rds\\" + Hand[c] + ".gif")));
}
return p;
}
}
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.addActionListen
new ActionListener() {
public void actionPerformed(ActionEven
System.exit(0);
}
});
handButton.addActionListen
new ActionListener() {
public void actionPerformed(ActionEven
Deck deck = new Deck();
}
});
// show the dialog box
frame.getContentPane().add
frame.getContentPane().add
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\\
}
return p;
}
}
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.
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.
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.
ASKER
I tried this, it breaks.
handButton.addActionListen er(
new ActionListener() { public void actionPerformed(ActionEven t 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\\p okerg\\Car ds\\" +
Hand[c] ".gif")));
}
}
//Breaks
handButton.addActionListen
new ActionListener() { public void actionPerformed(ActionEven
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\\p
Hand[c] ".gif")));
}
}
//Breaks
Where is p defined?
It needs to be a member variable.
It needs to be a member variable.
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.addActionListen er(
new ActionListener() {
public void actionPerformed(ActionEven t e) {
System.exit(0);
}
});
ActionListener handbut =
new ActionListener() {
public void actionPerformed(ActionEven t 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\\Ca rds\\" + Hand[c] + ".gif")));
System.out.println(Hand[c] );
}
p.repaint();
System.out.println("\n");
}
};
handButton.addActionListen er(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\\Ca rds\\" + Hand[c] + ".gif")));
System.out.println(Hand[c] );
}
System.out.println("\n");
return p;
}
}
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.addActionListen
new ActionListener() {
public void actionPerformed(ActionEven
System.exit(0);
}
});
ActionListener handbut =
new ActionListener() {
public void actionPerformed(ActionEven
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\\
System.out.println(Hand[c]
}
p.repaint();
System.out.println("\n");
}
};
handButton.addActionListen
// show the dialog box
frame.getContentPane().add
frame.getContentPane().add
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\\
System.out.println(Hand[c]
}
System.out.println("\n");
return p;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.addActionListen
That should just about cover it. Lemme know how you make out.