PleaseNoMoreJava
November 22nd, 2004, 10:21 AM
Hi all,
Im trying to do a simple value passing operation i cant seem to get it working correctly. I have two classes one which produces a value and the other that needs that value to do something with it. I have written the code, although i do not get the right result, when i click the confirm button and enter how many tickets i would like, the showID integer to be passed over to Cost class using method at the bottom of panel class. Heres my code:
Panel Class:
import javax.swing.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.*;
public class Panel extends JFrame{
public JComboBox days, months, years;
private JLabel dateresult;
public String date, day, month, year;
private JTextArea area;
public int num;
public static void main(String[] args)
{
Panel frame = new Panel();
}
public Panel()
{
String[] Month = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
String[] Year = {"2004"};
String [] Day = new String[32];
for(int i = 1; i < 32; i++) {
Day[i] = Integer.toString(i);
}
JButton okbutton;
JButton confirmbutton;
JFrame frame = new JFrame();
frame.setLocation(100,100);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JPanel primary = new JPanel(new FlowLayout(FlowLayout.CENTER,30000,5));
primary.setPreferredSize(new Dimension(500,500));
JPanel datecombos = new JPanel(new FlowLayout(FlowLayout.CENTER,10,10));
days = new JComboBox(Day);
days.addItemListener(new SelectButton(datecombos));
days.setSelectedIndex(1);
months = new JComboBox(Month);
months.addItemListener(new SelectButton(datecombos));
years = new JComboBox(Year);
years.addItemListener(new SelectButton(datecombos));
datecombos.add (days);
datecombos.add (months);
datecombos.add (years);
Cost cost;
cost = new Cost();
cost.BookingType();
JPanel showinfo = new JPanel();
primary.add (datecombos);
primary.add (showinfo);
dateresult = new JLabel();
okbutton = new JButton("SELECT");
okbutton.addActionListener(new SelectButton(datecombos));
datecombos.add (okbutton);
area = new JTextArea();
primary.add (cost);
showinfo.add (area);
frame.getContentPane().add(primary, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
public class SelectButton implements ItemListener, ActionListener {
JPanel parent;
public SelectButton(JPanel p){parent = p;}
public void actionPerformed(ActionEvent e)
{
String showID;
day = (String) days.getSelectedItem();
month = (String) months.getSelectedItem();
year = (String) years.getSelectedItem();
if (day != "1" || month != "Apr" || year != "2004")
{
JOptionPane.showMessageDialog(parent, "There are no showings Available for this Date. " +
"Please Select Again","Error",
JOptionPane.ERROR_MESSAGE);
}
else
{
dateresult.setText("Date Selected : " +day+" "+month+" "+year);
try //select 1, 2 or 3
{
showID = JOptionPane.showInputDialog(parent, "Enter A Show ID Please.");
num = Integer.parseInt(showID);
if (num == 1)
{
JOptionPane.showMessageDialog(parent, " bla bla bla");
area.setText(" ");
}
else if (num == 2)
{
JOptionPane.showMessageDialog(parent,"bla bla bla");
area.setText("");
}
else if (num == 3)
{
JOptionPane.showMessageDialog(parent,"bla bla bla");
area.setText("");
}
else
{
JOptionPane.showMessageDialog(parent,"You have not picked correctly" +
" Please Select Again","Error",
JOptionPane.ERROR_MESSAGE);
}
}
catch ( NumberFormatException ex )
{
JOptionPane.showMessageDialog(parent, "Non Numerical Data Exception.");
System.exit( 0 ); // Exits Program
}
}
}
public void itemStateChanged(ItemEvent e) { }
}
public void getnum(int num1)
{
num = num1;
}
}
Cost Class
import javax.swing.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.*;
import java.awt.*;
import javax.swing.text.*;
import javax.swing.event.*;
public class Cost extends JPanel implements ItemListener, ActionListener {
public String[] type = {"Select", "Adult", "Child", "Student"};
public String typechosen, ticketno;
public JComboBox typecombo, numbercombo;
public int ticketcost, number, totalcost;
private JLabel typeresult;
public int num1;
public int screen1 = 50;
public int screen2 = 75;
public int screen3 = 100;
public JTextField tickettext;
public JButton ticketbutton, confirmbutton;
/** Creates a new instance of BookingInfo */
public void BookingType() {
JPanel typeinfo = new JPanel(new FlowLayout(FlowLayout.CENTER,30000,5));
typeinfo.setBorder(BorderFactory.createLoweredBevelBorder());
typeinfo.setBackground (Color.yellow);
typeinfo.setPreferredSize(new Dimension(200,100));
JLabel costlabel1 = new JLabel ("Select Ticket Type");
costlabel1.setFont(new Font("Serif", Font.BOLD, 14));
costlabel1.setForeground(Color.black);
typecombo = new JComboBox(type);
typecombo.addItemListener(this);
typeinfo.add(costlabel1);
typeinfo.add(typecombo);
typeresult = new JLabel();
typeinfo.setVisible(true);
add(typeinfo);
JPanel okPanel = new JPanel();
okPanel.setBorder(BorderFactory.createLoweredBevelBorder());
okPanel.setBackground (Color.red);
okPanel.setPreferredSize(new Dimension(500,60));
confirmbutton = new JButton("CONFIRM BOOKING");
confirmbutton.addActionListener(this);
typeinfo.add(confirmbutton);
}
public void itemStateChanged( ItemEvent event )
{
typechosen = (String) typecombo.getSelectedItem();
if ( typechosen.equals("Adult"))
{
ticketcost = 5;
}
else if ( typechosen.equals("Child"))
{
ticketcost = 3;
}
else if ( typechosen.equals("Student"))
{
ticketcost = 4;
}
}
public void actionPerformed(ActionEvent e)
{
try
{
ticketno = JOptionPane.showInputDialog(Cost.this, "Please enter the number of tickets required. Thanks");
number = Integer.parseInt(ticketno);
totalcost = ticketcost * number;
}
catch ( NumberFormatException ex )
{
JOptionPane.showMessageDialog(Cost.this, "Non Numerical Data Exception.");
System.exit( 0 ); // Exits Program
}
Panel cust;
cust = new Panel();
cust.getnum(num1);
System.out.println(num1); //this should print passed value out
if (num1 > screen1)
{
JOptionPane.showMessageDialog(Cost.this, "Booking Accepted");
}
else if (num1 > screen2)
{
JOptionPane.showMessageDialog(Cost.this, "Booking Accepted");
}
else if (num1 > screen3)
{
JOptionPane.showMessageDialog(Cost.this, "Booking Accepted");
}
else
{
JOptionPane.showMessageDialog(Cost.this, "Booking Not Accepted");
}
}
}
I have highlighted areas of code that are the problem areas.
Does anyone have any ideas why im not getting the desired result ?
Thanks
Im trying to do a simple value passing operation i cant seem to get it working correctly. I have two classes one which produces a value and the other that needs that value to do something with it. I have written the code, although i do not get the right result, when i click the confirm button and enter how many tickets i would like, the showID integer to be passed over to Cost class using method at the bottom of panel class. Heres my code:
Panel Class:
import javax.swing.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.*;
public class Panel extends JFrame{
public JComboBox days, months, years;
private JLabel dateresult;
public String date, day, month, year;
private JTextArea area;
public int num;
public static void main(String[] args)
{
Panel frame = new Panel();
}
public Panel()
{
String[] Month = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
String[] Year = {"2004"};
String [] Day = new String[32];
for(int i = 1; i < 32; i++) {
Day[i] = Integer.toString(i);
}
JButton okbutton;
JButton confirmbutton;
JFrame frame = new JFrame();
frame.setLocation(100,100);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JPanel primary = new JPanel(new FlowLayout(FlowLayout.CENTER,30000,5));
primary.setPreferredSize(new Dimension(500,500));
JPanel datecombos = new JPanel(new FlowLayout(FlowLayout.CENTER,10,10));
days = new JComboBox(Day);
days.addItemListener(new SelectButton(datecombos));
days.setSelectedIndex(1);
months = new JComboBox(Month);
months.addItemListener(new SelectButton(datecombos));
years = new JComboBox(Year);
years.addItemListener(new SelectButton(datecombos));
datecombos.add (days);
datecombos.add (months);
datecombos.add (years);
Cost cost;
cost = new Cost();
cost.BookingType();
JPanel showinfo = new JPanel();
primary.add (datecombos);
primary.add (showinfo);
dateresult = new JLabel();
okbutton = new JButton("SELECT");
okbutton.addActionListener(new SelectButton(datecombos));
datecombos.add (okbutton);
area = new JTextArea();
primary.add (cost);
showinfo.add (area);
frame.getContentPane().add(primary, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
public class SelectButton implements ItemListener, ActionListener {
JPanel parent;
public SelectButton(JPanel p){parent = p;}
public void actionPerformed(ActionEvent e)
{
String showID;
day = (String) days.getSelectedItem();
month = (String) months.getSelectedItem();
year = (String) years.getSelectedItem();
if (day != "1" || month != "Apr" || year != "2004")
{
JOptionPane.showMessageDialog(parent, "There are no showings Available for this Date. " +
"Please Select Again","Error",
JOptionPane.ERROR_MESSAGE);
}
else
{
dateresult.setText("Date Selected : " +day+" "+month+" "+year);
try //select 1, 2 or 3
{
showID = JOptionPane.showInputDialog(parent, "Enter A Show ID Please.");
num = Integer.parseInt(showID);
if (num == 1)
{
JOptionPane.showMessageDialog(parent, " bla bla bla");
area.setText(" ");
}
else if (num == 2)
{
JOptionPane.showMessageDialog(parent,"bla bla bla");
area.setText("");
}
else if (num == 3)
{
JOptionPane.showMessageDialog(parent,"bla bla bla");
area.setText("");
}
else
{
JOptionPane.showMessageDialog(parent,"You have not picked correctly" +
" Please Select Again","Error",
JOptionPane.ERROR_MESSAGE);
}
}
catch ( NumberFormatException ex )
{
JOptionPane.showMessageDialog(parent, "Non Numerical Data Exception.");
System.exit( 0 ); // Exits Program
}
}
}
public void itemStateChanged(ItemEvent e) { }
}
public void getnum(int num1)
{
num = num1;
}
}
Cost Class
import javax.swing.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.*;
import java.awt.*;
import javax.swing.text.*;
import javax.swing.event.*;
public class Cost extends JPanel implements ItemListener, ActionListener {
public String[] type = {"Select", "Adult", "Child", "Student"};
public String typechosen, ticketno;
public JComboBox typecombo, numbercombo;
public int ticketcost, number, totalcost;
private JLabel typeresult;
public int num1;
public int screen1 = 50;
public int screen2 = 75;
public int screen3 = 100;
public JTextField tickettext;
public JButton ticketbutton, confirmbutton;
/** Creates a new instance of BookingInfo */
public void BookingType() {
JPanel typeinfo = new JPanel(new FlowLayout(FlowLayout.CENTER,30000,5));
typeinfo.setBorder(BorderFactory.createLoweredBevelBorder());
typeinfo.setBackground (Color.yellow);
typeinfo.setPreferredSize(new Dimension(200,100));
JLabel costlabel1 = new JLabel ("Select Ticket Type");
costlabel1.setFont(new Font("Serif", Font.BOLD, 14));
costlabel1.setForeground(Color.black);
typecombo = new JComboBox(type);
typecombo.addItemListener(this);
typeinfo.add(costlabel1);
typeinfo.add(typecombo);
typeresult = new JLabel();
typeinfo.setVisible(true);
add(typeinfo);
JPanel okPanel = new JPanel();
okPanel.setBorder(BorderFactory.createLoweredBevelBorder());
okPanel.setBackground (Color.red);
okPanel.setPreferredSize(new Dimension(500,60));
confirmbutton = new JButton("CONFIRM BOOKING");
confirmbutton.addActionListener(this);
typeinfo.add(confirmbutton);
}
public void itemStateChanged( ItemEvent event )
{
typechosen = (String) typecombo.getSelectedItem();
if ( typechosen.equals("Adult"))
{
ticketcost = 5;
}
else if ( typechosen.equals("Child"))
{
ticketcost = 3;
}
else if ( typechosen.equals("Student"))
{
ticketcost = 4;
}
}
public void actionPerformed(ActionEvent e)
{
try
{
ticketno = JOptionPane.showInputDialog(Cost.this, "Please enter the number of tickets required. Thanks");
number = Integer.parseInt(ticketno);
totalcost = ticketcost * number;
}
catch ( NumberFormatException ex )
{
JOptionPane.showMessageDialog(Cost.this, "Non Numerical Data Exception.");
System.exit( 0 ); // Exits Program
}
Panel cust;
cust = new Panel();
cust.getnum(num1);
System.out.println(num1); //this should print passed value out
if (num1 > screen1)
{
JOptionPane.showMessageDialog(Cost.this, "Booking Accepted");
}
else if (num1 > screen2)
{
JOptionPane.showMessageDialog(Cost.this, "Booking Accepted");
}
else if (num1 > screen3)
{
JOptionPane.showMessageDialog(Cost.this, "Booking Accepted");
}
else
{
JOptionPane.showMessageDialog(Cost.this, "Booking Not Accepted");
}
}
}
I have highlighted areas of code that are the problem areas.
Does anyone have any ideas why im not getting the desired result ?
Thanks