How do I append data all into one dialog box?
I have a problem with my address book program. I view all the entries from my address book, but they are displayed in different dialog 开发者_JAVA百科boxes. The first entry will be displayed in the first dialog box and then if i click "OK" the next entry will be shown in another dialog box. I want all entries to be shown in one dialog box.
here's my getter method:
public String getInfo(){
String Info = "NAME\tADDRESS\tPHONE NO.\tE-MAIL ADD\n" +
name +"\t " + add +"\t "+ phoneNo +"\t "+ email +"\n";
return Info;
}
here's how i display all information:
public void viewAll() {
for (int i = 0; i < counter; i++) {
JOptionPane.showMessageDialog(null, new JTextArea(entry[i].getInfo()));
}
}
hope you can help me... thanks in advance :)
In order to view all the entries inside one dialog box, you can try creating a string that will add up all the entries. See my code below:
public String getInfo() {
String content = "\t" + name + "\t"+ address + "\t\t" + telNo + "\t" + email;
return content;
}
public class AddressBook
public void viewAllEntry() {
String addText = "NO\tNAME\tADDRESS\t\tTEL.NO\tEMAIL\t\n"; /<------HERE
for (int i = 0; i < addressBookEntryCounter; i++) {
addText = addText+(i+1)+ entry[i].getInfo()+ "\n"; /<------HERE
}
JOptionPane.showMessageDialog(null, new JTextArea(addText));
}
}
You could also try ninesided's solution
I'll take a wild stab in the dark here - you want ALL the contact information to be displayed in the same JOptionPane:
public void viewAll() {
StringBuffer contactList = new StringBuffer();
for (int i = 0; i < counter; i++) {
contactList.append(entry[i].getInfo());
contactList.append("\n");
}
JOptionPane.showMessageDialog(null, new JTextArea(contactList));
}
精彩评论