PDA

View Full Version : Help with java program (random access file)


comp101
07-29-2009, 05:53 PM
I have created an Address Book using a random access file that allows me to save records and cycle between records with buttons that go to the first entry, last entry, previous entry, and next entry.

I need help in creating an update and search method. The search button is my main priority. I want the user to be able to search the file by entering a word in a textfield and having the option of whether to match it with a record name, street, city, state, or zip. I am very stuck now.

Here is the actionPerformed section of my code in the Search class so far with the only focus on the user being able to try and compare names in the record:

**When the OK button is pressed, I want the comparison to happen**


public void actionPerformed(ActionEvent e) {

if (e.getSource()==jcbsearch){
search();
}

else if (e.getSource()==jbtok){

}

else if (e.getSource() == jbtcancel){
dispose();
}
}

public void search(){
if(jcbsearch.getSelectedItem()=="Name"){

Search frame2 = new Search(1);
frame2.pack();
frame2.setTitle("Search");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setLocationRelativeTo(null);
frame2.setVisible(true);
}
}

Gox
07-30-2009, 12:47 AM
Not a lot of information to go on here. However, I notice in this line of code you appear to be trying to compare two strings:
jcbsearch.getSelectedItem()=="Name"

In Java comparing Strings in this manner should be done with the Equals method.
jcbsearch.getSelectedItem().equals("Name")

The above assumes that jcbsearch.getSelectedItem() returns a String.

comp101
07-30-2009, 01:13 AM
I am sorry for the lack of information. The whole code would have taken up plenty of pages, and scared help away, so I tried to shorten it and just post the part I needed but let me try to clarify...

my "jcbsearch" is an uneditabel JComboBox and the term "Name" is a string item inside the ComboBox. (jcbsearch.getSelectedItem()=="Name") refers to what is supposed to happen when the "Name" item in the dropdown menu is selected.

When the "Name" item in the ComboBox is selected, another frame appears which allows the user to enter text into a textfield. There are two buttons in this new frame, "ok (jbtok), and cancel(jbtcancel)." When "cancel" is pressed, the frame is disposed of. What I want to do is for my new frame, when "ok" is pressed, I want to somehow search the random access file and see if that name entered in the TextField matches anything already previously entered into the file?

(this will also be duplicated for the other string entries in the ComboBox...street, city, state, and zip)