I\'m struggling here. I want to add JOptionPane message window after clicking en
ID: 3834205 • Letter: I
Question
I'm struggling here. I want to add JOptionPane message window after clicking enter. I need the message to display how many tickets the user wants to purchase.
import java.awt.*;
import java.awt.event.*;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
public class Movie extends JFrame
{
private JLabel jlblMovie;
private JTextField jtfMovie;
private JLabel jlblPeople;
private JTextField jtfPeople;
private JLabel jlblTickets;
private JTextField jtfTickets;
private JRadioButton button1;
private JRadioButton button2;
private JRadioButton button3;
private ButtonGroup radioGroup;
private JTextField outField;
private JButton button;
private JCheckBox check;
private JTextField checkField;
private JComboBox<String> comboBox;
private JTextField inField;
private String[] choices = {"Front" , "Middle" , "Back"};
private JComboBox<String> comboBox1;
private JTextField inField1;
private String[] choices1 = {"A","B","C", "D","E"};
private JComboBox<String> comboBox2;
private JTextField inField2;
private String[] choices2 = {"S1" , "S2" , "S3", "S4" , "S5" , "S6", "S7" , "S8" , "S9", "S10" , "S11" , "S12"};
private JPanel panel = new JPanel ();
public Movie () {
super ("Majestic Movie Threater");
setLayout (new FlowLayout());
outField = new JTextField ("Morning");
outField.setEditable(false);
jlblMovie = new JLabel ("What movie would you like to watch?");
jtfMovie = new JTextField(10);
jlblPeople = new JLabel("How many tickets?");
jtfPeople = new JTextField(10);
jlblTickets = new JLabel("When will you visit?");
button1 = new JRadioButton ("Morning", true);
button2 = new JRadioButton ("Noon", false);
button3 = new JRadioButton ("Evening", false);
button = new JButton("Enter");
inField = new JTextField ("Seat Section");
inField.setEditable(false);
inField1 = new JTextField ("Row");
inField1.setEditable(false);
inField2 = new JTextField ("Seat");
inField2.setEditable(false);
comboBox = new JComboBox<String>(choices);
comboBox.setMaximumRowCount(3);
comboBox.addItemListener(new ComboListener());
comboBox1 = new JComboBox<String>(choices1);
comboBox1.setMaximumRowCount(3);
comboBox1.addItemListener(new ComboListener());
comboBox2 = new JComboBox<String>(choices2);
comboBox2.setMaximumRowCount(3);
comboBox2.addItemListener(new ComboListener());
check = new JCheckBox ("I'm not a robot");
checkField = new JTextField ("Are you?" , 10);
checkField.setEditable(false);
add(jlblMovie); add(jtfMovie); add(jlblPeople); add(jtfPeople); add(jlblTickets);
add(button1); add(button2); add(button3);
add(inField);
add(comboBox);
add(inField1);
add(comboBox1);
add(inField2);
add(comboBox2);
add(check); add(checkField);
add(button);
radioGroup = new ButtonGroup();
radioGroup.add(button1);
radioGroup.add(button2);
radioGroup.add(button3);
RadioListener handler = new RadioListener ();
button1.addItemListener(handler);
button2.addItemListener(handler);
button3.addItemListener(handler);
check.addItemListener(new CheckListener());
}
private class RadioListener implements ItemListener
{
public void itemStateChanged (ItemEvent event)
{
String outputText;
if(event.getSource() == button1)
outputText = "Morning";
else if (event.getSource() == button2)
outputText = "Noon";
else outputText = "Evening";
outField.setText(outputText);
}
}
private class CheckListener implements ItemListener
{
public void itemStateChanged (ItemEvent event)
{
String outputText ="";
if (check.isSelected())
{
outputText ="okay good...";
}
else
{
outputText = "go away stupid virus";
}
checkField.setText(outputText);
}
}
private class ComboListener implements ItemListener
{
public void itemStateChanged(ItemEvent event)
{
if(event.getStateChange() == ItemEvent.SELECTED)
{
inField.setText(choices[comboBox.getSelectedIndex()]);
}
if(event.getStateChange() == ItemEvent.SELECTED)
{
inField1.setText(choices1[comboBox1.getSelectedIndex()]);
}
if(event.getStateChange() == ItemEvent.SELECTED)
{
inField2.setText(choices2[comboBox2.getSelectedIndex()]);
}
}
}
public static void main (String args [])
{
Movie frame = new Movie ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 400);
frame.setVisible(true);
}
}
Explanation / Answer
import java.awt.*;
//import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class FullDataBaseGenerator
{
// Method that takes a String (from the film time cb)
// And returns a String of the name of the text file it belongs to
public static String returnFileName(String input)
{
String timeFileName = input;
if (input.equals("1.00 PM"))
{
timeFileName = "SEAT DATABASE 1.00 PM.txt";
}
else if (input.equals("3.00 PM"))
{
timeFileName= "SEAT DATABASE 3.00 PM.txt";
}
else if (input.equals("5.00 PM"))
{
timeFileName= "SEAT DATABASE 5.00 PM.txt";
}
else if (input.equals("7.00 PM"))
{
timeFileName = "SEAT DATABASE 7.00 PM.txt";
}
else if (input.equals("9.00 PM"))
{
timeFileName= "SEAT DATABASE 9.00 PM.txt";
}
return timeFileName;
}
//Method to Create a blank database of all the seats available
public ArrayList<Integer> seatNumberCalculate()
{
// Variables to values for each block of seats
int A = 0;
int B = 0;
int C = 0;
// ArrayList to hold the values
ArrayList<Integer> al = new ArrayList<Integer>();
// Add zero at the start of the array to act as a defauilt value for the cbs
al.add(0);
// Calculate the seatnumbers and add them into the array
for (int i=0; i < 36; i++)
{
A = 101+i;
al.add(A);
}
for (int i = 0; i < 40; i++)
{
B = 201+i;
al.add(B);
}
for (int i = 0; i < 36; i++)
{
C = 301+i;
al.add(C);
}
return al;
}
// Method that generates a fresh database
public void FullDataBaseGeneration(String file_name)
{
// Name of database (calculated by 'returnFileName' method)
String name = file_name;
// Get ArrayList cointaining values for every seat
ArrayList <Integer> input = seatNumberCalculate();
// Name of database
String selectedTime = returnFileName(name);
File selectedTimeFile = new File(selectedTime);
try{
// if the file exists, do not create a new file (leave existing file alone)
if (selectedTimeFile.exists() == true)
{
return;
}
}catch (Exception ex){
System.err.println(ex.getMessage());
}
// if the file doesnt exist..
try{
// create a new file with the correct name
selectedTimeFile.createNewFile();
// Start dependencies for file reading
FileInputStream fs = new FileInputStream(selectedTimeFile.toString());
DataInputStream in = new DataInputStream(fs);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Start dependancy for file writing
String stringLine;
BufferedWriter fw1 = new BufferedWriter(new FileWriter(selectedTime));
// Write a ; to the file (this is needed to add some content to replace)
fw1.write(";");
// Close this write dependancy
fw1.close();
// While there are Lines left to be read
while ((stringLine = br.readLine()) != null)
{
// Create dependencies for writing to same file
BufferedWriter fw = new BufferedWriter(new FileWriter(selectedTime));
int x=0;
// Iterate through the new edited array (orginal array minus selected seat)
while(x < input.size())
{
// Rewrite every line of the text file with each entry in the new array
String line = input.get(x).toString();
fw.write(line + ";");
x++;
}
//Close the file writing dependency
fw.close();
}
}catch (Exception ex){
System.err.println(ex.getMessage());}
}
// Method for returning an array of the available seats, for passing into the Main class
public static ArrayList<Integer> AvailableSeatsArrayReturn(String file_name)
{
ArrayList<Integer> temp = new ArrayList<Integer>();
String name = file_name;
String selectedTime = returnFileName(name);
File selectedTimeFile = new File(selectedTime);
if (selectedTimeFile.exists())
{
try{
FileInputStream fs = new FileInputStream(selectedTimeFile.toString());
DataInputStream in = new DataInputStream(fs);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String stringLine;
while ((stringLine = br.readLine()) != null)
{
String[] array = stringLine.split(";");
// For every object in the array, built from items in the text file
for (int i=0; i < array.length; i++)
{
// Convert the item to an integer
Integer num = Integer.parseInt(array[i]);
// Add Item to arraylist to be rerturned
temp.add(num);
}
}
in.close();
}catch (Exception ex){System.err.println(ex.getMessage());}
}
return temp;
}
}