Skip to main content

Input Dialog Box, JOptionPane cancel button

1 reply [Last post]
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.

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.

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();

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
case 1: // second button, "List Items", pushed
case 2: // second button, "Add Borrowers", pushed
case 3: // second button, "Add Items", pushed
case 4: // second button, "Loan", pushed
case 5: // second button, "Return", pushed
case 6: // second button, "List Loans", pushed
case 7: // second button, "Library Fines", pushed
} while (option != 8); //continue till "Quit" pushed



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);
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
Joined: 2006-02-17
Points: 0

All your problems can be settled by adding button
action listener.Detailed to say:
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.
good luck!