pam coke

Greenhorn
+ Follow
since Nov 20, 2000
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by pam coke

How do class not in the same program communicate with each other. I have a class Counter that has a method userClickedButton(clickedstring). I have another class RadioTextButton that need to call userClickedButton how do I do this. If it is helpful, Counter class extends observable and RadioTextButton implements observer.
Thanks
24 years ago
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?
24 years ago
Well if you are looking for something you can take at home, then I suggest a videotape class. I am currently taking one through Southern Methodist University in Dallas, TX, but NTU also offers the same course, so that may be something you can look into. I must warn you, you have to be committed to learn a programming language by video. I was not very committed due to things that have happen in my life this semester and I have fallen behind and have not learned very much, but for a committed person it should work. Good luck.
24 years ago
I need help quickly. I am still having problems reading from a file one character at a time. Nothing seems to be working. I must read a file and store each character into an array. The problem I am having is getting it to read an character. I can read the bytes, but not characters. Here is some sample code. Please be very specific cause I am completely dumb when it comes to java!
FileInputStream fis = new FileInputStream(fileName);
FileOutputStream fos = new FileOutputStream("outfile.txt");
int c;
int i =0;
InputStreamReader isr = new InputStreamReader(fis);
while (( c = isr.read(someChars)) != -1) {
fos.write(c);
System.out.println(c);
i++;
This is what someone suggestted I use, but now the characters are not being written to the outfile. Thanks for any help you can give.
24 years ago
Thanks for the help. I am still having problems with reading the file into an char array. This statement does not seem to be working
while (( c = fis.read(someChars[i] )) != -1) {
fos.write(c);
any suggestion?
Thanks
Pam
24 years ago
I am to complete a program that reads from a file. I am a beginner and the textbook that I have is very confusing. Below is the code that I am to finish. Can you explain to me what I am supposed to be doing to read from a text file.
class Complete8_1 {
char[] someChars;
public void initialize( String fileName,
int maxCharCount ) {
// Your code to initialize the
// array goes here.
}
public String toString() {
return new String( someChars );
}
public static void main( String[] args ) {
if ( args.length >= 1 ) {
Complete8_1 a = new Complete8_1();
a.initialize( args[0], 100 );
System.out.println( a );
} else {
System.out.println( "A filename is "
+ "required." );
}
}
}
I have tried several things but I really don't understand what to do. The other disadvantage that I have is that this is a distance learning class so I don't have contact with any other people to ask question. Please help
24 years ago