Skip to main content

Input Dialog Box, JOptionPane cancel button

1 reply [Last post]
ngpamela
Offline
Joined: 2006-02-17
Points: 0

Help needed urgently!

I'm doing a project for my OOP (Object Oriented Programming) Assignment. My assignment is to write a programming of simple library system.

I'm currently having trouble doing the cancel button of the input dialog box.

(a)
How do i write the code, when the user clicks on the cancel button of the input dialog box. When the user clicks on the cancel button of the input dialog box, i want it to go back to the main option dialog box which i have created at the start of the program.

(b)
How do i write the code for checking the input keyed into the input dialog box.
(i) when the user did not key in anything and click on ok button of the input dialog box.
(ii) when the user did not key in all the values required.

Note: I used StringTokenizer.

Part of my program is as shown below:

public static void main(String [] args) {
//adds a predifined list of borrowers, library items and loans at the beginning of the program
LibrarySystem lib = new LibrarySystem();
addAListOfBorrowers(lib);
addAListOfLibraryItems(lib);
addAListOfLoans(lib);

int option;

do {
// declare String array to represent the choices

//Display an option dialogue box which allows the user to choose from the nine options
//List Borrowers, List Items, Add Borrowers, Add Items, Loan, Return, List Loans, Library Fine and Quit.
String [] choice = {"List Borrowers", "List Items", "Add Borrowers", "Add Items",
"Loan", "Return", "List Loans", "Library Fine", "Quit"};

// create an option dialogue
// the showOptionDialog method returns an integer to indicate the option chosen - 0 for the first, etc.
option = JOptionPane.showOptionDialog(null, "Please choose an option", "Library System",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice,
"List Borrowers");

switch (option) {
case 0: // first button, "List Borrowers", pushed
displayBorrowers(lib);
break;
case 1: // second button, "List Items", pushed
displayItems(lib);
break;
case 2: // second button, "Add Borrowers", pushed
addBorrowers(lib);
break;
case 3: // second button, "Add Items", pushed
addItems(lib);
break;
case 4: // second button, "Loan", pushed
loanItem(lib);
break;
case 5: // second button, "Return", pushed
returnItem(lib);
break;
case 6: // second button, "List Loans", pushed
listLoans(lib);
break;
case 7: // second button, "Library Fines", pushed
libraryFines(lib);
break;
}
} while (option != 8); //continue till "Quit" pushed

System.exit(0);
}

...
...
...

public static void addBorrowers(LibrarySystem lib) {
String input = JOptionPane.showInputDialog(null, "Enter Borrower's id, name (seperated by commas)",
"Add Borrower", JOptionPane.PLAIN_MESSAGE);
StringTokenizer inputToken = new StringTokenizer(input, ", ");
int id = Integer.parseInt(inputToken.nextToken());
String name = inputToken.nextToken();

//Calls the search method, findBorrower(id) to search whether borrower exists in the library system
Borrower existingBorrower = lib.findBorrower(id);
String message;
//If a borrower object is returned, shows a message "Borrower already exists!"
if (existingBorrower != null)
message = "Borrower already exists!";
else {
//If no borrower object is returned, creates a new borrower object,
//adds the borrower object created into the library system
Borrower newBorrower = new Borrower(id, name);
lib.add(newBorrower);
message = "Borrower created.\nID: "+id+"\nName: "+name;
}
JOptionPane.showMessageDialog(null, message, "New Borrower", JOptionPane.INFORMATION_MESSAGE);

if (inputToken.hasMoreTokens() == false) {
JOptionPane.showMessageDialog(null, "No input keyed! Please enter a valid input!", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
this is jackieding
Offline
Joined: 2006-02-17
Points: 0

hi,ngpamela.
All your problems can be settled by adding button
action listener.Detailed to say:
a)
u add action listener code to your canel button.
When u click cancel button,the dialog will dispose,
and after dispose ,u can fresh your mainframe by
rebuild your frame container with the frame u wanna use.
b) add listener to your submit button.
the data check will be added to the listener code.
When the data is null or is not inputed as you wanted,
just bring up a joptionpane and tell him
error inputed.

if u wanna detailed code,
u can mail to me.
dingjq@21cn.com
good luck!