wood burning stoves 2.0*
The moose likes Java in General and the fly likes Using .next() for next record button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using .next() for next record button" Watch "Using .next() for next record button" New topic
Author

Using .next() for next record button

Jay Thomas
Greenhorn

Joined: Mar 25, 2004
Posts: 3
I can't seen to get the code right. I have 3 store locations and want to go to the next record. How should I use .next(). I am expanding the program to have a previous, first, and last record buttons later. And so far I'm stuck on the next record button.
Here is my code: (Can someone help?)
// Make it easy to access packaged code.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
/**
* This class defines an applet to record price changes for a collection of
* Item objects maintained in a HashMap.
*
* Written by: Jon Huhtala
*/
public class App extends Applet implements ActionListener, MouseListener {
// These object references have broad scope so they may be used throughout
// the class.
private HashMap items; // The HashMap.
private Item current; // The current Item object being processed.
private TextField numberFld; // For entering/displaying the store number.
private TextField addressFld; // For displaying the store address.
private TextField phoneFld; // For entering/displaying the store phone number.
private Button bfirst; // The FIRST button.
private Button bnext; // The NEXT button.
private Button bprevious; // The PREVIOUS button.
private Button blast; // The LAST button.
private Label message; // The current message.
private String savedMessage; // For saving the previous message.
// This method defines initial (one-time) applet processing.
public void init() {
// Create the HashMap collection and load it with Item objects.
items = new HashMap();
current = new Item(56594, "1548 Main St. Traverse City, MI", "231-254-4852");
items.put(new Integer(current.getStoreNumber()), current);
current = new Item(56521, "4812 State Ave. Grand Rapids, MI", "616-567-1564");
items.put(new Integer(current.getStoreNumber()), current);
current = new Item(56548, "1882 Luvs St. Big Rapids, MI", "231-452-4525");
items.put(new Integer(current.getStoreNumber()), current);
// Set the size, background color, and layout manager for the applet window.
// NOTE: Because no layout manager is used, each component's size and
// location must be specifically set.
setSize(300, 300);
setBackground(Color.lightGray);
setLayout(null);
// Construct and set the main applet heading.
Label mainHdg = new Label("Store Phone Book");
mainHdg.setSize(250, 50);
mainHdg.setAlignment(Label.CENTER);
mainHdg.setFont(new Font("SansSerif", Font.BOLD, 24));
add(mainHdg);
mainHdg.setLocation(25, 25);
// Construct and set components for entering and displaying the store number.
Label numberHdg = new Label("Store #");
numberHdg.setSize(50, 25);
add(numberHdg);
numberHdg.setLocation(20, 100);
numberFld = new TextField();
numberFld.setSize(25, 25);
add(numberFld);
numberFld.setLocation(20, 125);
// Construct and set components for displaying the store address.
Label addressHdg = new Label("Item address");
addressHdg.setSize(70, 25);
add(addressHdg);
addressHdg.setLocation(70, 100);
addressFld = new TextField();
addressFld.setSize(130, 25);
addressFld.setEditable(false);
add(addressFld);
addressFld.setLocation(70, 125);
// Construct and set components for entering and displaying the store phone number.
Label phoneHdg = new Label("Phone");
phoneHdg.setSize(50, 25);
add(phoneHdg);
phoneHdg.setLocation(205, 100);
phoneFld = new TextField();
phoneFld.setSize(75, 25);
phoneFld.setEditable(false);
add(phoneFld);
phoneFld.setLocation(205, 125);
// Construct and set the FIRST button.
bfirst = new Button("First");
bfirst.setSize(60, 50);
bfirst.addActionListener(this);
add(bfirst);
bfirst.setLocation(30, 175);
bfirst.addMouseListener(this);
// Construct and set the CHANGE button.
bnext = new Button("Next");
bnext.setSize(60, 50);
bnext.setEnabled(false);
bnext.addActionListener(this);
add(bnext);
bnext.setLocation(120, 175);
bnext.addMouseListener(this);
// Construct and set the CANCEL button.
bprevious = new Button("Previous");
bprevious.setSize(60, 50);
bprevious.setEnabled(false);
bprevious.addActionListener(this);
add(bprevious);
bprevious.setLocation(210, 175);
bprevious.addMouseListener(this);
// Construct and set the LAST button.
blast = new Button("Last");
blast.setSize(60, 50);
blast.setEnabled(false);
blast.addActionListener(this);
add(blast);
blast.setLocation(210, 175);
blast.addMouseListener(this);

// Construct and set a label for displaying messages.
message = new Label("");
message.setSize(250, 25);
message.setAlignment(Label.CENTER);
add(message);
message.setLocation(25, 250);
}
// This method implements the abstract event handler method of the
// ActionListener interface to handle ActionEvents.
public void actionPerformed(ActionEvent e) {
// This block handles the processing of the NEXT button.
if (e.getSource().equals(bnext)) {
Iterator pointer = items.interator();
current = pointer.next();
addressFld.setText(current.getAddress());
phoneFld.setText(current.getPhone());
numberFld.setText(Integer.toString(current.getStoreNumber()));
bfirst.setEnabled(true);
bnext.setEnabled(true);
bprevious.setEnabled(true);
blast.setEnabled(true);
}
}
public void mouseEntered(MouseEvent e) {
savedMessage = message.getText();
if (e.getSource().equals(bnext))
message.setText("Proceed to the next record");
}
public void mouseExited(MouseEvent e) {
message.setText(savedMessage);
}
// Other required methods of the MouseListener interface. In this applet,
// they do nothing.
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
// This class encapsulates the attributes and methods of an Item. It is by no
// means complete, but ONLY used to support the testing of the applet.
class Store {
// Instance variables.
private int number;
private String address;
private String phone;
// This constructor initializes the instance variables to values received
// from the class client without editing.
public Store(int iStoreNumber, String iAddress, String iPhone) {
number = iStoreNumber;
address = iAddress;
phone = iPhone;
}
// These methods are used to set the value of their associated instance
// variable.
public void setNumber(int nStoreNumber) {
number = nStoreNumber;
}
public void setAddress(String nAddress) {
address = nAddress;
}
public void setPhone(String nPhone) {
phone = nPhone;
}
// These methods are used to get the value of their associated instance
// variable.
public int getStoreNumber() {
return number;
}
public String getAddress() {
return address;
}
public String getPhone() {
return phone;
}
}
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

So some things to mention first:
- if (intermediate) throw WhyDontYouUseCodeButtonException ("You don't know it?")
- too much code and too less explanation.
If you don't have time, to cut your code to the bare minimum, why should we spend our time (and we are probably a lot of persons, so (a lot of persons * ourTime) is spend in finding out, where your error is.
As a side effect, if you try to explain your problem accurately and clearly, you will often find the solution while doing this. This will help YOU!
And similiar effects are achieved, by cutting the code to the bare minimum.
If you post a lot of code, you can imagine that some comments aren't helpful for intermediate programmers - we know why to use 'import'.
And if your code doesn't even compile: mention it!
Show sections which are evident for the problem by remarking them in obvious way.
Compile errors:
- if your Item is a Store call it Store, but if your Store is a Item, call it Item.
- if something returns an Iterator, perhaps the method is called

Since an iterator always returns the SuperDuperClass: Object, we need a cast:

But - what a pity, a HashMap doesn't know 'iterator ()'.
So we might change HashMap to ArrayList, and instead of

Now a minor issue: why is the 'next'-button disabled in the beginning?
And why is the previous-button the last, and the next-button before?
I would expect the sorting '<-' '->'.
Have a look at your cd-player.
Think about pages in a book.
Ok.
But why doesn't it work?
Because you allways create a new Iterator in the 'actionPerformed'-Method, always forgetting the last state when the method is left.
The Iterator needs to be a member-variable, so you can increase it several times, without loosing it's state.
Initialize the Iterator in the init-Method and and let it point to either the last or the first entry, and enable the prev. or next-Button accordingly.
Of course you have to do some bounds-checking, (already at last position?), but I guess that's no problem to you.


http://home.arcor.de/hirnstrom/bewerbung
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using .next() for next record button
 
Similar Threads
Showing the first record by press a button
Help Client Server implementaion
Adding Exception error problems
HashMap Issues and Such
Client server help