HI,
I am having some trouble with my assignment. I have created the gui and i am now writing the event handling. My problem is with the arrays. I have 3 seperate arrays all the same length.1 is a string and the other two are ints. in each array the positions each hold data that relate to the same object, ie the string [1] is beans, with the int [1] is the barcode for the beans and the other array[1] holds the price for the beans. I want to be able to show all 3 pieces of info in a textfield when the item is selected from a jcombobox.
Any help would be grateful. I will post my code in case you need to see it. Peace to all.
Glenn
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Observer; //new
import java.util.Observable;//new
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class MySuperMktPro {
public MySuperMktPro() {
CheckoutView Check1 = new CheckoutView(1,0,0);
CheckoutView Check2 = new CheckoutView(2,300,0);
CheckoutView Check3 = new CheckoutView(3,600,0);
CheckoutView Check4 = new CheckoutView(4,0,350);
CheckoutView Check5 = new CheckoutView(5,600,350);
OfficeView off111 = new OfficeView();
}
public static void main(String[] args) {
System.out.println("Starting My Supermarket Project...");
MySuperMktPro startup = new MySuperMktPro();
}
}
class CheckoutView implements ItemListener , ActionListener {
private OfficeView off1;
private JFrame chk1;
private Container cont1;
private Dimension screensize,tempDim;
private JPanel pan1,pan2,pan3,pan4,pan5;
private JButton buyBut,prodCode;
private JButton purchase,cashBack,manual,remove,disCo;
private JButton but1,but2,finaLi1,but4,but5,but6;
private JTextArea tArea1,tArea2;
private JTextField tf1,tf2,tf3,list2;
private JComboBox prodList;
private JLabel lab1,lab2,lab3,lab4,lab5,lab6,lab7;
private int checkoutID; //New private int for all methods of this class
private int passedX,passedY;
private String prods[];
private JButton rb1,rb2,rb3,rb4;
private double price[]; // not used
private int code[];
public CheckoutView(int passedInteger, int passedX, int passedY) {
checkoutID = passedInteger; //Store the int passed into the method
this.passedX = passedX;
this.passedY = passedY;
drawCheckoutGui();
}
public void drawCheckoutGui() {
prods= new String[]{"Beans", "Eggs", "bread", "Jam", "Butter",
"Cream", "Sugar", "Peas", "Milk", "Bacon", "Spaghetti",
"Corn Flakes", "Carrots", "Oranges", "Bananas", "Snickers",
"Wine", "Beer", "Lager", "Cheese"};
code = new int [20];
for(int b = 0; b < code.length; b++) {
code[b] = b+1;
}
price = new double[]{0.65, 0.84, 0.98, 0.75, 0.45, 0.65,
1.78, 1.14, 0.98, 0.99, 0.98, 0.65, 1.69, 2.99,
0.99, 2.68, 0.89, 5.99, .54, 2.99};
screensize = Toolkit.getDefaultToolkit().getScreenSize();
chk1 = new JFrame();
chk1.setTitle("Checkout #" + checkoutID); //Use the new stored int
chk1.setSize(300,350);
chk1.setLocation(passedX,passedY);
chk1.setLayout(new GridLayout(5,1));
//chk1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cont1 = chk1.getContentPane();
//chk1.setLayout(new BorderLayout());
pan1 = new JPanel();
pan2 = new JPanel();
pan3 = new JPanel();
pan4 = new JPanel();
pan5 = new JPanel();
//panel 1
pan1.setLayout(new FlowLayout());
pan1.setBackground(Color.black);
prodList = new JComboBox(prods);
prodList.setMaximumRowCount(2);
prodList.addItemListener(this);
but1 = new JButton("Buy");
but1.addActionListener(this);
lab1 = new JLabel("Products");
lab1.setForeground(Color.white);
lab1.setBorder(BorderFactory.createLineBorder(Color.white));
pan1.add(lab1);
pan1.add(prodList);
pan1.add(but1);
//panel 2
pan2 = new JPanel();
pan2.setLayout(new BorderLayout());
pan2.setBackground(Color.black);
lab3 = new JLabel(" Enter Product Code");
lab3.setForeground(Color.white);
tArea2 = new JTextArea(8,10);
tArea2.setBorder(BorderFactory.createLineBorder(Color.white));
lab5 = new JLabel(" Tesco's ");
lab5.setForeground(Color.white);
lab5.setBorder(BorderFactory.createLineBorder(Color.white));
lab6 = new JLabel("Checkout");
lab6.setForeground(Color.white);
lab6.setBorder(BorderFactory.createLineBorder(Color.white));
lab7 = new JLabel("You selected ");
lab7.setForeground(Color.cyan);
//lab7.setBorder(BorderFactory.createLineBorder(Color.white));
pan2.add(lab7,"North");
pan2.add(lab6,"East");
pan2.add(tArea2,"Center");
pan2.add(lab5,"West");
pan2.add(lab3,"South");
// panel 3
pan3 = new JPanel();
pan3.setLayout(new FlowLayout());
pan3.setBackground(Color.black);
manual = new JButton("Manual");
manual.addActionListener(this);
manual.setBorder(BorderFactory.createLineBorder(Color.white));
remove = new JButton("Remove");
remove.addActionListener(this);
remove.setBorder(BorderFactory.createLineBorder(Color.white));
tf1 = new JTextField(5);
pan3.add(manual);
pan3.add(tf1);
pan3.add(remove);
// panel 4
pan4 = new JPanel();
pan4.setLayout(new FlowLayout());
pan4.setBackground(Color.black);
finaLi1 = new JButton("Finalise");
// finaLi1.addActionListener(this);
cashBack = new JButton("Cashback");
JTextField list2 = new JTextField(5);
but4 = new JButton(" Sum Total");
but4.addActionListener(this);
but4.setBorder(BorderFactory.createLineBorder(Color.white));
cashBack.setBorder(BorderFactory.createLineBorder(Color.white));
pan4.add(cashBack);
pan4.add(list2);
pan4.add(but4);
// panel 5
pan5 = new JPanel();
pan5.setLayout(new GridLayout(2,3));
pan5.setBackground(Color.black);
disCo = new JButton("Discount");
disCo.addActionListener(this);
disCo.setBorder(BorderFactory.createLineBorder(Color.black));
but6 = new JButton("Finalise");
but6.addActionListener(this);
but6.setBorder(BorderFactory.createLineBorder(Color.black));
rb1 = new JButton("Cash");
rb1.addActionListener(this);
rb1.setBorder(BorderFactory.createLineBorder(Color.black));
rb2 = new JButton("Card");
rb2.addActionListener(this);
rb2.setBorder(BorderFactory.createLineBorder(Color.black));
rb3 = new JButton("Cheque");
rb3.addActionListener(this);
rb3.setBorder(BorderFactory.createLineBorder(Color.black));
rb4 = new JButton("Voucher");
rb4.addActionListener(this);
rb4.setBorder(BorderFactory.createLineBorder(Color.black));
pan5.add(disCo);
pan5.add(but6);
pan5.add(rb1);
pan5.add(rb2);
pan5.add(rb3);
pan5.add(rb4);
//add the panels to the container
cont1.add(pan1);
cont1.add(pan4);
cont1.add(pan2);
cont1.add(pan3);
cont1.add(pan5);
chk1.setContentPane(cont1);
chk1.setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
Object item = e.getItem();
System.out.println("Selected item: "+item);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getActionCommand();
System.out.println("Action performed from: "+source);
}
}
class OfficeView {
private OfficeView off111;
private JFrame offMod1;
private JPanel pane1;
private JButton refill;
private Container cont11;
private Dimension screensize;
private JTextField tf11,tf12;
public OfficeView() {
drawOfficeGUI();
}
public void drawOfficeGUI() {
screensize = Toolkit.getDefaultToolkit().getScreenSize();
offMod1 = new JFrame("Office Control");
int frameWidth = 300;
int frameHeight = 250;
offMod1.setSize(frameWidth,frameHeight);
offMod1.setLocation(300,350);
offMod1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cont11 = offMod1.getContentPane();
pane1 = new JPanel();
pane1.setBackground(Color.cyan);
refill = new JButton("Refill");
tf11 = new JTextField(25);
tf12 = new JTextField(25);
pane1.add(tf11);
pane1.add(refill);
pane1.add(tf12);
cont11.add(pane1);
offMod1.setContentPane(cont11);
offMod1.setVisible(true);
}