File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes observable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "observable" Watch "observable" New topic
Author

observable

pam coke
Greenhorn

Joined: Nov 20, 2000
Posts: 6
I have this Counter class that extends observable, and I have a RadioTextPanel class that extends JPanel implements Observer. Inside the counter class there is a userClickedbutton method that accepts a parameter clickstring. The clickstring is in the RadioTextPanel class. How will the userclickbutton method get the string. In other words, how do I get RadioTextPanel to call userclickedbutton?
Below is the code I have for Counter Class:import java.util.*;
public class Counter extends Observable implements Assignment3 {
HashMap myCounters;
public String who() { return "Thompson, Felicia";}
public void userClickedButton(String clickString){
int val = 0;
val = ((Integer)myCounters.get(clickString)).intValue();
val++;
Integer newVal = new Integer(val);
myCounters.put(clickString, newVal);
setChanged();
notifyObservers(newVal);
}
public HashMap getHashMap() {
return myCounters;
/* return (containsKey);*/
}
}

Below is the code for RadioTextPanel:
import java.util.Observable;
import java.lang.Object;
public class RadioTextPanel extends JPanel implements Observer{
static JFrame frame;
static String birdString = "Bird";
static String catString = "Cat";
static String dogString = "Dog";
static String rabbitString = "Rabbit";
static String pigString = "Pig";
JLabel picture;
JTextField msgbox;
JPanel lower;
public RadioTextPanel(Counter myC) {
Counter myCounters = myC;

// Create the radio buttons.
JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setMnemonic(KeyEvent.VK_B);
birdButton.setActionCommand(birdString);
birdButton.setSelected(true);
JRadioButton catButton = new JRadioButton(catString);
catButton.setMnemonic(KeyEvent.VK_C);
catButton.setActionCommand(catString);
JRadioButton dogButton = new JRadioButton(dogString);
dogButton.setMnemonic(KeyEvent.VK_D);
dogButton.setActionCommand(dogString);
JRadioButton rabbitButton = new JRadioButton(rabbitString);
rabbitButton.setMnemonic(KeyEvent.VK_R);
rabbitButton.setActionCommand(rabbitString);
JRadioButton pigButton = new JRadioButton(pigString);
pigButton.setMnemonic(KeyEvent.VK_P);
pigButton.setActionCommand(pigString);
// Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);
group.add(dogButton);
group.add(rabbitButton);
group.add(pigButton);
// Register a listener for the radio buttons.
RadioListener myListener = new RadioListener();
birdButton.addActionListener(myListener);
catButton.addActionListener(myListener);
dogButton.addActionListener(myListener);
rabbitButton.addActionListener(myListener);
pigButton.addActionListener(myListener);
// Set up the picture label
picture = new JLabel(new ImageIcon(
birdString
+ ".gif"));
// The preferred size is hard-coded to be the width of the
// widest image and the height of the tallest image.
// A real program would compute this.
picture.setPreferredSize(new Dimension(177, 122));
// Set up the message box
msgbox = new JTextField();
// Put the radio buttons in a column in a panel
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(0, 1));
radioPanel.add(birdButton);
radioPanel.add(catButton);
radioPanel.add(dogButton);
radioPanel.add(rabbitButton);
radioPanel.add(pigButton);
setLayout(new BorderLayout());
add(radioPanel, BorderLayout.WEST);
add(picture, BorderLayout.CENTER);
add(msgbox, BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
/** Listens to the radio buttons. */
class RadioListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
picture.setIcon(new ImageIcon(
e.getActionCommand()
+ ".gif"));
Counter click = myC.userClickedButton(e.getActionCommand());
}
}
public void update(Observable myC, Object arg) {
System.out.println("update received");
}

public static void main(String s[]) {
frame = new JFrame("CSE 7345 Assignment3");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
// Create new counter object
Counter myC = new Counter();
//Create new JPanel
RadioTextPanel rta = new RadioTextPanel(myC);
// register JPanel with the Counter Object
myC.addObserver(rta);
frame.getContentPane().add(rta, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
Can you please help, I have to turn this in tomorrow?
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Since this post does not appear to relate to applets, I am moving it to the beginner forum.
Stephen Peterson
Ranch Hand

Joined: Dec 25, 2000
Posts: 33
Hi Pam,
you have a pretty long listing here. Any way you can prune away most of the lines which might not directly bear on your question? Often when I build a concise program and run it, to illustrate my question to put in a newsgroup post, it helps me find the answer to my original question.
------------------
United Health Group
Hartford, CT, USA


United Health Group<BR>Hartford, CT, USA
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: observable