JFrame and JOptionPane

hi guy,

Thank you for taking your time to solve my problem. I doing a project, what my project does is that when a customer come to the shop to rent the movie. If the movie found and the customer want to rent the movie so it have to key in the customer detail using GUI(using JFrame) and it ask customer how many day do they want to rent (using JOptionPane)and then it display the movie, cost and customer after the display the movie status change from "available" to "rented out".

Here is my problem:

1) When customer want to rent the movie it load GUI to enter the customer detail then follow by JOptionPane to ask the customer how many day to rent. But it load both the GUI and JOptionPane together on the screen. How do I load the GUI and enter the customer detail, when the user click "OK" and close then it load the JOptionPane.

here is the driver file(DvdUI.java):

import javax.swing.JOptionPane;

import javax.swing.JFrame;

class DvdUI

{

public static void main(String args[])

{

String movieTitle, output="", name, address, phone;

final String title = "Odyssey Film Rental Pte Ltd";

Movie movie;

Customer customer;

int pressed;

DvdAdmin admin = new DvdAdmin();

JFrame GUI = new GuiTesting();

do

{

do

{

movieTitle = JOptionPane.showInputDialog(null,"Enter the movie title: ", title

, JOptionPane.PLAIN_MESSAGE);

movie = admin.findMovieTitle(movieTitle);

}while(movie==null);

output = "" + admin.showMovieDetails(movieTitle);

pressed = JOptionPane.showConfirmDialog(null, output +"\n\nDo you want to rental this movie? ",

title,JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

}while(pressed == JOptionPane.NO_OPTION);

if (pressed == JOptionPane.YES_OPTION)

{

movie.setStatus("Rented out");

//JFrame GUI = new GuiTesting();

GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GUI.setVisible(true);

//GUI.setFocusable(true);

String input = JOptionPane.showInputDialog(null, "Please enter how many day you wish to rent: ");

}

}

}

here is the class file (GuiTesting.java):

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.Container;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

class GuiTesting extends JFrame

{

private static final int width = 500;

private static final int height = 175;

private JLabel nameLabel, addressLabel, phoneLabel;

private JTextField nameTextField, addressTextField, phoneTextField;

private JButton okButton, clearButton;

private OkButtonHandler obHandler;

//private ClearButtonHandler cbHandler;

public GuiTesting()

{

setTitle("Customer Detail");

setSize(width,height);

Container pane = getContentPane();

GridLayout Layout = new GridLayout(4,3);

pane.setLayout(Layout);

nameLabel = new JLabel("Name: ");

addressLabel = new JLabel("Address: ");

phoneLabel = new JLabel("Phone: ");

nameTextField = new JTextField(30);

addressTextField = new JTextField(50);

phoneTextField = new JTextField(8);

okButton = new JButton("OK");

obHandler = new OkButtonHandler();

okButton.addActionListener(obHandler);

clearButton = new JButton("CLEAR");

//add components to the pane

pane.add(nameLabel);

pane.add(nameTextField);

pane.add(addressLabel);

pane.add(addressTextField);

pane.add(phoneLabel);

pane.add(phoneTextField);

pane.add(okButton);

pane.add(clearButton);

}//end constructor

private class OkButtonHandler implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

String name;

DvdAdmin admin = new DvdAdmin();

name = nameTextField.getText();

admin.printCustomerName(name);

//String address;

//address = addressTextField.getText();

//String phone;

//phone = phoneTextField.getText();

}//method

}//class AcceptButtonHander

}//class GuiTesting

Sorry to take your time.......Thank you

Message was edited by:

Kleave

[4416 byte] By [Kleavea] at [2007-11-26 15:58:08]
# 1

Because you set your gui visible and next line you ask for a JOptionPane.

Try to ask for a JOptionPane in the actionPerformed of the Ok button.

By the way, in your button handler why do you create an instance of DVDGUI ? And why in the main method of DVDGUI you create another instance ?

ProZa at 2007-7-8 22:19:09 > top of Java-index,Desktop,Core GUI APIs...
# 2
I try the moving JOptionPane to actionPerformed the problem is still the same..........any other solution?Message was edited by: Kleave
Kleavea at 2007-7-8 22:19:09 > top of Java-index,Desktop,Core GUI APIs...
# 3

I tried your code and it works if you add this to the constructor of GuiTesting :

WindowListener l = new WindowAdapter() {

public void windowClosing(WindowEvent e) {

synchronized(e.getSource()) {

e.getSource().notifyAll();

}

}

};

addWindowListener(l);

and this after GUI.setVisible(true) in DvdUI.java :

try {

synchronized(GUI) {

System.out.println("Wait GUI");

GUI.wait();

System.out.println("GUI closed");

}

} catch (java.lang.InterruptedException ie) {

ie.printStackTrace();

} catch (Exception exp) {

exp.printStackTrace();

}

Please remove

GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

because when you will close GUI you will all your application.

ProZa at 2007-7-8 22:19:09 > top of Java-index,Desktop,Core GUI APIs...
# 4
I BIG THANK TO YOUnow i go and try
Kleavea at 2007-7-8 22:19:09 > top of Java-index,Desktop,Core GUI APIs...
# 5

Thank for your help.....it working fine.......I got one problem.........

after enter the customer detail and it print the receipt( Example of movie title is crank ). Then after the printing the receipt the movie status change to 'rented out' . When serving the next customer, when enter the customer detail in GUI and click 'ok' it reset my crank movie status to 'available', by right should be 'rented out'. How to I stop the status from be reset to 'available'. Can someone help me out. Thank.......

Here is the Movie class:

public class Movie

{

private String movieTitle, catalogue, category, status;

private double cost;

public Movie(String cata, String bMovieTitle, double bCost, String bCategory, String bStatus)

{

movieTitle = bMovieTitle;

catalogue = cata;

cost = bCost;

category = bCategory;

status = bStatus;

System.out.println(movieTitle + "\t" + status);

}

public String getMovieTitle()

{

return movieTitle;

}

public String getCategory()

{

return category;

}

public String getCatalogue()

{

return catalogue;

}

public double getCost()

{

return cost;

}

public String getStatus()

{

return status;

}

public void setStatus(String newStatus)

{

status = newStatus;

System.out.println(movieTitle + "\t" + status);

}

public String toString()

{

String msg;

msg = "DVD title: " + movieTitle +

"\nCatalogue No: " + catalogue +

"\nCategory: " + category +

"\nCost: $" + cost +

"\nStatus: " + status;

return msg;

}

}

Here is my DVDAdmin class:

import javax.swing.JOptionPane;

public class DvdAdmin

{

private Movie[] movie;

private Customer customer;

public DvdAdmin()

{

movie = new Movie[15];

movie[0] = new Movie("C0001", "The OH in Ohio", 10, "Comedy", "Available");

movie[1] = new Movie("C0002", "Bring It On", 10, "Comedy", "Available");

movie[2] = new Movie("A0001", "Shrek", 5, "Aimation", "Available");

movie[3] = new Movie("A0002", "Joseph: King of Dreams", 5, "Aimation", "Available");

movie[4] = new Movie("A0003", "Barney's Great Adventure", 5, "Aimation", "Available");

movie[5] = new Movie("A0004", "Ice Age 2", 5, "Aimation", "Available");

movie[6] = new Movie("A0005", "A Bug's Life", 8, "Aimation", "Available");

movie[7] = new Movie("A0006", "We're back", 5,"Aimation", "Available");

movie[8] = new Movie("A0007", "Flushed Away", 10, "Aimation", "Available");

movie[9] = new Movie("A0008", "Over the Hedge", 5, "Aimation", "Available");

movie[10] = new Movie("T0001", "Operation Undercover", 10, "Thriller", "Available");

movie[11] = new Movie("T0002", "Inside Man", 8, "Thriller", "Available");

movie[12] = new Movie("T0003", "Hannibal", 5, "Thriller", "Available");

movie[13] = new Movie("T0004", "Crank", 10, "Thriller", "Available");

movie[14] = new Movie("T0005", "The Guardian", 10, "Thriller", "Available");

}

public Movie findMovieTitle(String bMovie)

{

int sizeOfMovie = movie.length;

String msg;

for( int i = 0; i < sizeOfMovie; i++)

{

if (movie.getMovieTitle().equalsIgnoreCase(bMovie))

{

return movie;

}//if

}//for

JOptionPane.showMessageDialog(null, "Movie title: " + bMovie + " not found",

"Odyssey Film Rental Pte Ltd", JOptionPane.INFORMATION_MESSAGE );

return null;

}//Movie

public String showMovieDetails(String bMovie)

{

Movie movie;

movie = findMovieTitle(bMovie);

return movie.toString();

}

public void NewCustomer(String cName, String cAddress, String cPhone)

{

Customer cus = new Customer(cName, cAddress, cPhone);

//cus(cName, cAddress, cPhone);

}

public String showCustomerDetail()

{

return customer.toString();

}

}

here is my customer class:

class Customer

{

private String name, address, phone;

public Customer(String aName, String aAddress, String aPhone)

{

name = aName;

address= aAddress;

phone= aPhone;

System.out.println(name + "\t" + address + "\t" + phone);

}

public String getName()

{

return name;

}

public String getAddress()

{

return address;

}

public String getPhone()

{

return phone;

}

public String toString()

{

String output;

output = "Customer name: " + name + "\n" +

"Customer address: " + address + "\n" +

"Customer Number: " + phone;

return output;

}

}

Kleavea at 2007-7-8 22:19:09 > top of Java-index,Desktop,Core GUI APIs...
# 6

In the listener of your ok button your create a new instance of DvdAdmin.

In the DvdAdmin constructor you initialise all status to available that is why each time the status is set to available.

Try to use only one instance of DvdAdmin. Try to create a singleton of that class (there are many topics in the forum) it will help to keep in memory dvd status.

Keep in mind that if you keep dvd status only in memory, if there is a crash of your app all dvd will be set back to "available".

ProZa at 2007-7-8 22:19:09 > top of Java-index,Desktop,Core GUI APIs...
# 7
Oh and please use the tag "code" in your message, it's easier to read source code. Thanks
ProZa at 2007-7-8 22:19:09 > top of Java-index,Desktop,Core GUI APIs...