Here's my working code: (skip to end for question)
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TicketInformation extends JFrame implements ActionListener
{
//Panels that includes name,departure,return, and validate button
private JPanel generalInformation , validatePanel;
//Panels for name, departure , return
private JPanel namePanel , depDatePanel , returnDatePanel;
//Panels that include airline and destination
private JPanel airlines , destinations, airlineAndDestination;
//JtextFields
private JTextField nameText , depDateText , returnDateText;
//validate button
private JButton validate;
//Radio buttons for airlines and destinations
private JRadioButton american , united , continental , midwest , delta , southwest;
private JRadioButton losangeles , denver , miami , orlando , clevland;
//Button group for airline and destination
private ButtonGroup airlineBG , destinationBG;
//Create arrays to store city names and prices
private String [] cityNames = {"Los Angeles" , "Denver" , "Miami" , "Orlando" , "Cleveland"};
private double [] ticketPrices = {399.00 , 265.50 , 220.00 , 185.00 , 145.00};
//JOptionPane
JOptionPane jopPrice = new JOptionPane();
/**
*Default Constructor
*Creates a frame with information to buy a ticket
*/
public TicketInformation()
{
//Create panels
generalInformation = new JPanel();//includes namePanel, depDatePanel, and returnDatePanel
airlineAndDestination = new JPanel(new GridLayout(2,1)); //includes airlines and destinations (JPanels)
validatePanel = new JPanel();//includes validate button
airlines = new JPanel();//includes airline radio buttons and JLabel
destinations = new JPanel();//includes destination radio buttons and JLabel
namePanel = new JPanel();//includes JTextArea and JLabel of name
depDatePanel = new JPanel();//includes " of destination
returnDatePanel = new JPanel();//includes " of return
//Create text fields and labels
JLabel nameLabel = new JLabel("Name:");
JLabel depDateLabel = new JLabel("Departure Date:");
JLabel returnDateLabel = new JLabel("Return date:");
nameText = new JTextField(10);//name of client
nameText.requestFocusInWindow();
depDateText = new JTextField(8);//departure date
returnDateText = new JTextField(8);//return date
JLabel airlineLabel = new JLabel("Choose an airline");//radio button labels
JLabel destinationLabel = new JLabel("Choose a destination");
//Create text labels for airline radio buttons
american = new JRadioButton("American");
united = new JRadioButton("United");
continental = new JRadioButton("Continental");
midwest = new JRadioButton("Midwest");
delta = new JRadioButton("Delta");
southwest = new JRadioButton("Southwest");
//Create text labels for destination radio buttons
losangeles = new JRadioButton("Los Angeles");
denver = new JRadioButton("Denver");
miami = new JRadioButton("Miami");
orlando = new JRadioButton("Orlando");
clevland = new JRadioButton("Clevland");
//Create validate button and add actionListener
validate = new JButton("Validate");
validate.addActionListener(this);
//add name, return, departure components to appropriate frames
namePanel.add(nameLabel);
namePanel.add(nameText);
depDatePanel.add(depDateLabel);
depDatePanel.add(depDateText);
returnDatePanel.add(returnDateLabel);
returnDatePanel.add(returnDateText);
//add above panels to generalInformation
generalInformation.add(namePanel , BorderLayout.WEST);
generalInformation.add(depDatePanel , BorderLayout.CENTER);
generalInformation.add(returnDatePanel , BorderLayout.EAST);
//add generalInformation to frame
add(generalInformation , BorderLayout.NORTH);
//add airline and destination labels
airlines.add(airlineLabel);
destinations.add(destinationLabel);
//add airline radio buttons
airlines.add(american);
airlines.add(united);
airlines.add(continental);
airlines.add(midwest);
airlines.add(delta);
airlines.add(southwest);
//add destination radio buttons
destinations.add(losangeles);
destinations.add(denver);
destinations.add(miami);
destinations.add(orlando);
destinations.add(clevland);
//set action commands
american.setActionCommand("American");
united.setActionCommand("United");
continental.setActionCommand("Continental");
midwest.setActionCommand("Midwest");
delta.setActionCommand("Delta");
southwest.setActionCommand("Southwest");
losangeles.setActionCommand("Los Angeles");
denver.setActionCommand("Denver");
miami.setActionCommand("Miami");
orlando.setActionCommand("Orlando");
clevland.setActionCommand("Clevland");
//create ButtonGroups for radio buttons
airlineBG = new ButtonGroup();
destinationBG = new ButtonGroup();
//add airline radio buttons to button group
airlineBG.add(american);
airlineBG.add(united);
airlineBG.add(continental);
airlineBG.add(midwest);
airlineBG.add(delta);
airlineBG.add(southwest);
//add destination radion buttons to button group
destinationBG.add(losangeles);
destinationBG.add(denver);
destinationBG.add(miami);
destinationBG.add(orlando);
destinationBG.add(clevland);
//add buttons to panels
airlineAndDestination.add(airlines);
airlineAndDestination.add(destinations);
//add to frame
add(airlineAndDestination , BorderLayout.CENTER);
//add validate button to south part of frame
add(validate , BorderLayout.SOUTH);
}
//Handle the validate button and radio buttons
public void actionPerformed(ActionEvent ae)
{
String customerName;//name
String _returnDate;//return date
String departureDate;//departure date
String letter;//temp String to check letters of name
StringBuffer objectString = new StringBuffer("");//contain selected information
int number;//used to see if there are any numbers in name
double price = 0;//price of ticket
if(ae.getSource() == validate)
{
//get the text in the text fields
customerName = nameText.getText();
_returnDate = returnDateText.getText();
departureDate = depDateText.getText();
for(int i = 0; i < customerName.length(); i++)
{
letter = customerName.substring(i , i+1);//take each character in the string
try//show error message and break if there is a number in the name
{
number = Integer.parseInt(letter);//try to parse a number out of the name
//if there is a letter, will skip these two steps
JOptionPane.showMessageDialog(null, "Error. Name cannot have numbers.");
break;
}
catch(NumberFormatException nfe){}
//no need to print anything because a nfe should be thrown
}
//validates data for each component or display error message
if(customerName.length() <= 1)
{
JOptionPane.showMessageDialog(null, "Error. Name must be more than one character");
}
else if(checkValidDate(_returnDate) == false)
{
JOptionPane.showMessageDialog(null, "Error. Dates must be formatted as: mm/dd/yy");
}
else if(checkValidDate(departureDate) == false)
{
JOptionPane.showMessageDialog(null, "Error. Dates must be formatted as: mm/dd/yy");
}
else//when validated, enter information into StringBuffer and validate airline and destination radio buttons
{
objectString = new StringBuffer(("Customer:\t\t" + customerName + "\n"
+ "Departure Date:\t" + departureDate + "\n"
+ "Return Date:\t" + _returnDate));
if(airlineBG.getSelection() == null)
{
JOptionPane.showMessageDialog(null, "Error. Choose an airline");
}
else if(destinationBG.getSelection() == null)
{
JOptionPane.showMessageDialog(null, "Error. Choose a destination");
}
else
{
objectString.append("\nAirline:\t\t" + airlineBG.getSelection().getActionCommand());
objectString.append("\nDestination:\t" + destinationBG.getSelection().getActionCommand());
for(int i = 0; i < cityNames.length; i++)
{
if(cityNames.equals(destinationBG.getSelection().getActionCommand()))
{
price = ticketPrices[i];
break;
}
}
int answer = jopPrice.showConfirmDialog(null , "Price: " + price + "\nWould you like to buy this ticket?" , "Price" , JOptionPane.YES_NO_OPTION);
if(answer == JOptionPane.YES_OPTION)
{
objectString.append("\nPrice:\t\t" + price);
objectString.append("\nTicket Bought. Have a safe flight!");
System.out.print(objectString.toString());//print string buffer
this.dispose();//exits frame
}
else if(answer == JOptionPane.NO_OPTION)
{
this.getContentPane().removeAll();
this.validate();
this.repaint();
}
}
}
}
}
/**
* Checks whether the date entered is in the format of
* mm/dd/yy
*<BR>
* @param dates: a date entered by the user
* @return boolean: returns true if the characters at 2 and 5 are slashes
* and every other character is a digit
*/
private static boolean checkValidDate(String dates)
{
boolean result = false;
if(dates.length() == 8)
{
int slashOne = dates.indexOf("/");
int slashTwo = dates.indexOf("/" , 3);
char datePosZero = dates.charAt(0);
char datePosOne = dates.charAt(1);
char datePosThree = dates.charAt(3);
char datePosFour = dates.charAt(4);
char datePosSix = dates.charAt(6);
char datePosSeven = dates.charAt(7);
if( (Character.isDigit(datePosZero)) && (Character.isDigit(datePosOne)) && (Character.isDigit(datePosThree)) && (Character.isDigit(datePosFour)) && (Character.isDigit(datePosSix)) && (Character.isDigit(datePosSeven)) )
{
if((slashOne == 2) && (slashTwo == 5))
{
result = true;
}
}
}
return result;
}
public static void main(String [] args)
{
TicketInformation myFrame = new TicketInformation();
//set JFrame properties
myFrame.setTitle("Buy A Ticket");
myFrame.setVisible(true);
myFrame.setLocation(300,300);
myFrame.setSize(100,100);
myFrame.setResizable(false);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
}
}When you run this, it creates a GUI to buy a ticket with JTextFields for name, departure date, and return date. It also contains JRadioButtons for airlines and destinations (in separate ButtonGroups). Lastly it has a validate button.
When the user enters, correctly, the information a JOptionPane comes up that asks the use to buy the ticket. The options are YES_OPTION and NO_OPTION. YES_OPTION prints the ticket information. NO_OPTION is supposed to +clear+ the contents of the JTextField as well as unselect the RadioButtons. The code to do this is
else if(answer == JOptionPane.NO_OPTION)
{
this.getContentPane().removeAll();
this.validate();
this.repaint();
}The only thing that happens is it comes up with a blank JFrame with title "Buy A Ticket." I've tried creating a new TicketInformation object, but this does not work at all. help!!