Link to home
Start Free TrialLog in
Avatar of ladykh
ladykh

asked on

Put text area contents into another text area depending on which item selected from combo box.

Please can anybody help?

I select items from a combobox menu which are written to a JTextArea (on customer order page).  After selecting, I select from a further combox box of table names (array 0-5), and depending on which one selected, I need the information to go to one of 6 text areas (array0-5) on the table page.

I am totally stuck.

My code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;


public class rms222 extends JApplet implements ActionListener{
      
            
//*********************************
//MAIN PANEL VARIABLES

            //Tabbed Pane
            JTabbedPane tabs;

            //Panels
            JPanel pnlMain = new JPanel();
            JPanel pnlOne  = new JPanel(); //Welcome
            JPanel pnlTwo  = new JPanel(); //Customer Order
            JPanel pnlThree = new JPanel(); //Tables
            JPanel pnlFour = new JPanel(); //Manager
      



//*********************************
//PANEL 1 VARIABLES (WELCOME)








//*********************************
//PANEL 2 VARIABLES (CUSTOMER ORDER)

                  //5 Labels
      
            JLabel lblTitle = new JLabel("Customer Orders");

            JLabel lblStarter = new JLabel("Starter");
            JLabel lblMain = new JLabel("Main Course");
            JLabel lblDes = new JLabel("Dessert");
            JLabel lblDrink = new JLabel("Drinks");

            //4 Combo Boxes
            JComboBox startCombo = new JComboBox();
            JComboBox mainCombo = new JComboBox();
            JComboBox desCombo = new JComboBox();
            JComboBox drinkCombo = new JComboBox();
            JComboBox tableCombo = new JComboBox();


            //Panels for Customer Order Tab
            JPanel pnlMenuTop = new JPanel();
            JPanel pnlMenuBot = new JPanel();
            JPanel pnlMenuLeft = new JPanel();
            JPanel pnlMenuRight = new JPanel();


            //Buttons Customer Order
            Button btnClear = new Button("Reset Order");
            Button btnRemove = new Button ("Remove Item");
            Button btnEnter = new Button ("Enter");

            Label spacer= new Label();




            //Basket myBasket = new Basket();
            CustomerOrder myCustomerOrder = new CustomerOrder();

            //Area where customer order displayed
            TextArea txtareaOrder = new TextArea(10,30);
            
            //Arrays
            String start_descriptions[]= new String[6];
            double start_prices[]= new double[6];

            String main_descriptions[]= new String[6];
            double main_prices[]= new double[6];

            String des_descriptions[]= new String[6];
            double des_prices[]= new double[6];

            String drink_descriptions[]= new String[6];
            double drink_prices[]= new double[6];

            String table_name[]= new String[6];
            int tableindex;
            boolean remv=false;

//**********************************


//*********************************
//PANEL 3 VARIABLES (TABLES)

      JTextArea txtareaOrders[] = new JTextArea[6];




//**********************************


      public void init(){
            
            JPanel pnlMain = new JPanel(); //Main Panel
            
            tabs= new JTabbedPane();

            //Dimension mySize =new Dimension(300,200);
            Dimension mySize =new Dimension(178,100);

            //this.setBackground(Color.yellow);
      

//********************************************************************************************
      //FIRST TAB - WELCOME

            JPanel pnlOne = new JPanel();
            pnlOne.setLayout(new BorderLayout());
            pnlOne.setBackground(Color.blue);


            JLabel lblOne = new JLabel("Restaurant System", JLabel.CENTER);
            pnlOne.add(lblOne, BorderLayout.CENTER);
            lblOne.setForeground(Color.white);

            JLabel lblTwo = new JLabel("Please select the tabs at the top of the page to use the system", JLabel.CENTER);
            pnlOne.add(lblTwo, BorderLayout.SOUTH);
            lblTwo.setForeground(Color.white);


            tabs.add("Welcome", pnlOne);//Add pnlOne to First Tab
      
      

//********************************************************************************************
      //SECOND TAB - CUSTOMER ORDER

            //pnlTwo= new JPanel();
            //pnlOrderBut = new JPanel();

            //pnlOrderBut.setLayout(new GridLayout(4,1));

            pnlTwo.setLayout(new BorderLayout());
            pnlTwo.add(pnlMenuTop, BorderLayout.NORTH);
            pnlTwo.add(pnlMenuBot, BorderLayout.CENTER);
            pnlTwo.add(pnlMenuLeft, BorderLayout.EAST);
            pnlTwo.add(pnlMenuRight, BorderLayout.SOUTH);


            pnlMenuTop.setLayout(new GridLayout(0,4)); //As many rows as need and 4 coloumns

            //Add objects to customer order
            pnlMenuTop.add(lblStarter);
            pnlMenuTop.add(lblMain);
            pnlMenuTop.add(lblDes);
            pnlMenuTop.add(lblDrink);


            pnlMenuTop.add(startCombo);
            pnlMenuTop.add(mainCombo);
            pnlMenuTop.add(desCombo);
            pnlMenuTop.add(drinkCombo);


            pnlMenuBot.setBackground(Color.cyan);
            JLabel lblchoice = new JLabel("Customer Order Summary", JLabel.CENTER);

            pnlMenuBot.add(lblchoice);
            pnlMenuBot.add(txtareaOrder);
                  pnlMenuBot.add(tableCombo);


            pnlMenuRight.add(btnClear);
            pnlMenuRight.add(spacer);
            pnlMenuRight.add(btnRemove);
            pnlMenuRight.add(spacer);
            pnlMenuRight.add(btnEnter);




      //Add Action Listener to buttons and combo boxes
            startCombo.addActionListener(this);
            mainCombo.addActionListener(this);
            desCombo.addActionListener(this);
            drinkCombo.addActionListener(this);
            tableCombo.addActionListener(this);

            btnClear.addActionListener(this);
            btnRemove.addActionListener(this);
            btnEnter.addActionListener(this);


            //Starter Arrays
            for (int i=0;i<6;i++){
                  start_descriptions[i] = new String();
                  }//End for

            start_descriptions[0]= "Prawn Cocktail";
            start_descriptions[1]= "Chicken Pate";
            start_descriptions[2]= "Tomato Soup";
            start_descriptions[3]= "Melon";
            start_descriptions[4]= "Smoked Mackeral";
            start_descriptions[5]= "Garlic Bread";


            start_prices[0]= 3.75;
            start_prices[1]= 3.25;
            start_prices[2]= 2.80;
            start_prices[3]= 2.95;
            start_prices[4]= 3.50;
            start_prices[5]= 2.75;


            for (int i=0;i<6;i++){
                  startCombo.addItem(start_descriptions[i]);
                  }//End for



            //Main Course Arrays
            for (int i=0;i<6;i++){
            main_descriptions[i] = new String();
                  }//End for

            main_descriptions[0]= "Roast Chicken";
            main_descriptions[1]= "Scampi and Chips";
            main_descriptions[2]= "Lemon Sole";
            main_descriptions[3]= "Cheeseburger";
            main_descriptions[4]= "Shepherds Pie";
            main_descriptions[5]= "Gammon Steak";


            main_prices[0]= 8.99;
            main_prices[1]= 7.75;
            main_prices[2]= 9.25;
            main_prices[3]= 7.50;
            main_prices[4]= 8.25;
            main_prices[5]= 8.50;


            for (int i=0;i<6;i++){
                  mainCombo.addItem(main_descriptions[i]);
                  }//End for



            //Dessert Arrays
            for (int i=0;i<6;i++){
            des_descriptions[i] = new String();
                  }//End for

            des_descriptions[0]= "Vanilla Ice Cream";
            des_descriptions[1]= "Chocolate Fudge Cake";
            des_descriptions[2]= "Strawberry Meringue";
            des_descriptions[3]= "Fruit Salad";
            des_descriptions[4]= "Profiteroles";
            des_descriptions[5]= "Apple Pie";


            des_prices[0]= 2.99;
            des_prices[1]= 3.20;
            des_prices[2]= 3.25;
            des_prices[3]= 2.99;
            des_prices[4]= 3.20;
            des_prices[5]= 3.10;


            for (int i=0;i<6;i++){
                  desCombo.addItem(des_descriptions[i]);
                  }//End for

            //Drink Arrays
            for (int i=0;i<6;i++){
            drink_descriptions[i] = new String();
                  }//End for

            drink_descriptions[0]= "Coca-Cola";
            drink_descriptions[1]= "Mineral Water";
            drink_descriptions[2]= "Lager";
            drink_descriptions[3]= "White Wine";
            drink_descriptions[4]= "Orange Juice";
            drink_descriptions[5]= "Bacardi Breezer";


            drink_prices[0]= 1.80;
            drink_prices[1]= 1.20;
            drink_prices[2]= 3.00;
            drink_prices[3]= 3.50;
            drink_prices[4]= 2.25;
            drink_prices[5]= 2.90;


            for (int i=0;i<6;i++){
                  drinkCombo.addItem(drink_descriptions[i]);
                  }//End for

                  //Table Arrays
            for (int i=0;i<6;i++){
            table_name[i] = new String();
                  }//End for

            table_name[0]= "Table 1";
            table_name[1]= "Table 2";
            table_name[2]= "Table 3";
            table_name[3]= "Table 4";
            table_name[4]= "Table 5";
            table_name[5]= "Table 6";


            drink_prices[0]= 1.80;
            drink_prices[1]= 1.20;
            drink_prices[2]= 3.00;
            drink_prices[3]= 3.50;
            drink_prices[4]= 2.25;
            drink_prices[5]= 2.90;


            for (int i=0;i<6;i++){
                  tableCombo.addItem(table_name[i]);
                  //tableCombo[i].setActionCommand("" + i);
                  }//End for


            tabs.add("Customer Order",pnlTwo);//Add pnl2 to Second Tab



//********************************************************************************************
      //THIRD TAB - TABLES
            JPanel pnlThree = new JPanel();
            pnlThree.setLayout(new GridLayout(0,3,4,4));

            JLabel lblTblOne = new JLabel("Table One");
            pnlThree.add(lblTblOne);      
            JLabel lblTblTwo = new JLabel("Table Two");
            pnlThree.add(lblTblTwo);
                  JLabel lblTblThree = new JLabel("Table Three");
            pnlThree.add(lblTblThree);
                  JLabel lblTblFour = new JLabel("Table Four");
            pnlThree.add(lblTblFour);
                  JLabel lblTblFive = new JLabel("Table Five");
            pnlThree.add(lblTblFive);
                  JLabel lblTblSix = new JLabel("Table Six");
            pnlThree.add(lblTblSix);
                  

            JTextArea txtareaOrders[] = new JTextArea[6];

            for (int i=0;i<6;i++){
            txtareaOrders[i]=new JTextArea(6,12);
            pnlThree.add(txtareaOrders[i]);
            }
                  
            tabs.add("Tables",pnlThree);//Add pnlThree to Third Tab


//****************************************************************************************



//***************************************************************************************

      //FOURTH TAB - MANAGER

            JPanel pnlFour = new JPanel();

            JLabel lblFour = new JLabel("This is the kitchen label");
            pnlFour.add(lblFour);

            tabs.add("Manager",pnlFour);//Add pnlThree to Third Tab



//****************************************************************************************


            tabs.setBackground(Color.blue); //Set color of tabs

            lblOne.setFont(new Font("SansSerif",Font.BOLD,24)); //Set font of first label on first tab

            tabs.setPreferredSize(mySize);
            pnlMain.setLayout(new GridLayout(0,1));
            pnlMain.add(tabs); //Add tabs to panel
            tabs.setFont(new Font("SansSerif",Font.BOLD,16)); //Set font of tabs

            setContentPane(pnlMain);// Put the JPanel on the JApplet



  } // End init



public void actionPerformed(ActionEvent e){


//TAB 2 - CUSTOMER ORDER ****************************************************
            //The get selected item picks words from the relevant combo box, adds to the
            //string and puts in the text area on a new line each time.

      if(e.getSource()==startCombo){
            int i= startCombo.getSelectedIndex();
            OrderLine line = new OrderLine(i,start_descriptions[i],start_prices[i]);

            if(remv){myCustomerOrder.removeOrderLine(line);remv=false;
                  }else{
                        myCustomerOrder.addOrderLine(line);}

                        txtareaOrder.setForeground(Color.black);
                        txtareaOrder.setText(myCustomerOrder.showContents());
      
      }//End if


      if(e.getSource()==mainCombo){
            int i= mainCombo.getSelectedIndex();
            OrderLine line = new OrderLine(i,main_descriptions[i],main_prices[i]);

            if(remv){myCustomerOrder.removeOrderLine(line);remv=false;
                  }else{
                        myCustomerOrder.addOrderLine(line);}

                        txtareaOrder.setForeground(Color.black);
                        txtareaOrder.setText(myCustomerOrder.showContents());
      
      }//End if


      if(e.getSource()==desCombo){
            int i= desCombo.getSelectedIndex();
            OrderLine line = new OrderLine(i,des_descriptions[i],des_prices[i]);

            if(remv){myCustomerOrder.removeOrderLine(line);remv=false;
                  }else{
                        myCustomerOrder.addOrderLine(line);}

                        txtareaOrder.setForeground(Color.black);
                        txtareaOrder.setText(myCustomerOrder.showContents());
      
      }//End if

      if(e.getSource()==drinkCombo){
            int i= drinkCombo.getSelectedIndex();
            OrderLine line = new OrderLine(i,drink_descriptions[i],drink_prices[i]);

            if(remv){myCustomerOrder.removeOrderLine(line);remv=false;
                  }else{
                        myCustomerOrder.addOrderLine(line);}

                        txtareaOrder.setForeground(Color.black);
                        txtareaOrder.setText(myCustomerOrder.showContents());
      
      }//End if



      if(e.getSource()==btnClear){
            myCustomerOrder.clearIt();
            txtareaOrder.setForeground(Color.red);
            txtareaOrder.setText("Your basket is currently empty");
            }//End if


      if(e.getSource()==btnRemove){remv=true;}


            }//End Action Performed
//***************************************************************************


} // End rms222

class OrderLine{

      public int id;
      public String description;
      public double price;
      public int quantity;


      public OrderLine(int i,String des,double p){
            this.id = i;
            this.description = des;
            this .price =p;
            this.quantity = 1;
            }
      }//end OrderLinedef


class CustomerOrder{

            ArrayList Order=new ArrayList();

      public void clearIt(){Order.clear();}


      public void addOrderLine(OrderLine line){

      boolean there=false;

            for(int j =0;j<Order.size();j++){
                  OrderLine read =(OrderLine)Order.get(j);
                        if(read.id == line.id){there=true; //Remove this line to prevent first line problem?
                              read.quantity++;//if it's there already update its quantity
                  }
            }//end loop


            if(!there){Order.add(line);}//otherwise add
            }//end addOrderLine


      public void removeOrderLine(OrderLine line){

            for(int j =0;j<Order.size();j++){
            OrderLine read =(OrderLine)Order.get(j);

                  if(read.id == line.id)read.quantity--;

                        if(read.quantity<1){Order.remove(read);}
            }
      }//end removeOrderLine


      public String showContents(){

            String contents = new String();
            double bill =0;

            for(int j =0;j<Order.size();j++){
                  OrderLine read =(OrderLine)Order.get(j);
                  contents+=read.quantity+" "+read.description;
                  contents+="\n";
                  bill+=read.price*read.quantity;
                  }


            if(bill>0){contents+="\nTotal cost = £"+bill;
            return contents;
                  }else{
            return "Your basket is currently empty"  ;}

            }//end showContents

}//end CustomerOrder

Please can anyone help?
Thanks

Avatar of Mick Barry
Mick Barry
Flag of Australia image

what exactly is your problem?
Avatar of ladykh
ladykh

ASKER



I can't get the items in JTextArea on the customer page to go into one of the 6 text areas depending on which table name was selected from the combo box.
Why dod u need to get the items out of the text area, can u instead just determine what to go into the 6 ta's according to the cb states?
Remove the redefinition of txtareaOrders, your're defining it first at the class level, the again in init(), which shadows the class level variable.

Move all addActionListener() calls to the very end of init(). One of the draw backs with many of Swing components is that action events are fired as you're populating them with data. This causes a problem in your case.

Add the following to your actionPerformed() method:
     if (e.getSource()==tableCombo) {
           txtareaOrders[tableCombo.getSelectedIndex()].setText(myCustomerOrder.showContents());
     }


ASKER CERTIFIED SOLUTION
Avatar of Javatm
Javatm
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Javatm, that won't exactly help ladykh improve programming skills.
> Javatm, that won't exactly help ladykh improve programming skills.

Good catch there my friend, But I cant just answer and give all the answer because
we are not alowed to do that, thats why I just made som very few changes =-)
Avatar of ladykh

ASKER

Thank you javatm, I do understand it much better now.
Sorry for late reply.
Thank you for your help.
http://www.freewebs.com/cube-j

Always Glad 2 Help . . .
Friend : Javatm