Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Insert code that adds the String representation of the selected item to the ArrayList billItems.

  1. #1
    Member
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Insert code that adds the String representation of the selected item to the ArrayList billItems.

    I am not sure how to go about doing this, An ItemEvent.Selected constant is the hint given to me but i dont know how to start it.

    my Array is:
    private ArrayList billItems = new ArrayList();

    my method is:

    private void beverageJComboBoxItemStateChanged( ItemEvent event )

    HELP!!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    What have you tried?
    Do you have any specific questions about your problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    Yeah well i did try this

    {

    String item = (String)event.getItem();
    if (event.getStateChange() == ItemEvent.SELECTED)
    {
    billItems.append(item + " was selected\n");
    }
    else
    {
    billItems.append(item + " was deselected\n");
    }
    }
    What i need to do is this:

    Create the beverageJComboBoxItemStateChanged method (which immediately follows loadCategory) and insert code that adds the String representation of the selected item to the ArrayList billItems. [Hint: Use the ItemEvent.SELECTED constant to determine whether an item is selected.]

    I haven't a clue how to go about it properly

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    Do you have a small, complete program that compiles, executes and shows the problem? It's hard to make suggestions without code that executes for testing.

    Be sure to wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    I am sending you the entire code i have written so far

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JComponent;
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import java.util.ArrayList;
    import java.awt.Container;
    import javax.swing.BorderFactory;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.ResultSetMetaData;
    import java.sql.ResultSet;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
     
     
     
     
     
    public class RestaurantBillCalculator extends JFrame
    {
       static final String DATABASE_URL = "jdbc:mysql://localhost/restaurant";
       static final String DATABASE_USERNAME = "root";
       static final String DATABASE_PASSWORD = "password"; 
     
       // JLabel for Restaurant
       private JLabel restaurantJLabel;
     
       // JPanel to display waiter information
       private JPanel waiterJPanel;
     
       // JLabel and JTextField for table number
       private JLabel tableNumberJLabel;
       private JTextField tableNumberJTextField;
     
       // JLabel and JTextField for waiter name
       private JLabel waiterNameJLabel;
       private JTextField waiterNameJTextField;
     
       // JPanel to display menu items
       private JPanel menuItemsJPanel;
     
       // JLabel and JComboBox for beverage
       private JLabel beverageJLabel;
       private JComboBox beverageJComboBox;
     
       // JLabel and JComboBox for appetizer
       private JLabel appetizerJLabel;
       private JComboBox appetizerJComboBox;
     
       // JLabel and JComboBox for main course
       private JLabel mainCourseJLabel;
       private JComboBox mainCourseJComboBox;
     
       // JLabel and JComboBox for dessert
       private JLabel dessertJLabel;
       private JComboBox dessertJComboBox;
     
       // JButton for calculate bill
       private JButton calculateBillJButton;
     
       // JLabel and JTextField for subtotal
       private JLabel subtotalJLabel;
       private JTextField subtotalJTextField;
     
       // JLabel and JTextField for tax
       private JLabel taxJLabel;
       private JTextField taxJTextField;
     
       // JLabel and JTextField for total
       private JLabel totalJLabel;
       private JTextField totalJTextField;
     
       // constant for tax rate
       private final static double TAX_RATE = 0.05;
     
       // declare instance variables for database processing
       private Connection myConnection;
       private Statement myStatement;
       private ResultSet myResultSet;
     
       // declare instance variable ArrayList to hold bill items
       private ArrayList billItems = new ArrayList();
     
       // constructor
       public RestaurantBillCalculator( String databaseUserName, String databasePassword )
       {
         try 
         {
             myConnection = DriverManager.getConnection
            (DATABASE_URL, DATABASE_USERNAME, DATABASE_PASSWORD);
     
             myStatement = myConnection.createStatement();
     
             myResultSet = myStatement.executeQuery(DATABASE_URL);
         }
         catch ( SQLException sqlException )
          {
             sqlException.printStackTrace();
             System.exit( 1 );
          } // end catch
         finally
         {
             try
             {
                 myResultSet.close();
                 myStatement.close();
                 myConnection.close();
             } // end of try block
     
             catch ( SQLException SQLexception )
             {
                SQLexception.printStackTrace();
             } // end catch block
         } // end of finally
     
       } // end constructor
     
       // create and position GUI components; register event handlers
       private void createUserInterface()
       {
          // get content pane for attaching GUI components
          Container contentPane = getContentPane();
     
          // enable explicit positioning of GUI components 
          contentPane.setLayout( null );
     
          // **** TODO ****** set up restaurantJLabel
          JLabel restaurantJLabel = new JLabel("Restaurant");
          restaurantJLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
          restaurantJLabel.setBounds(30, 50, 100, 100);
     
          JPanel waiterJPanel = new JPanel();
          waiterJPanel.setLayout(null);
          waiterJPanel.setBounds(20, 50, 100, 100);
          waiterJPanel.setBorder(BorderFactory.createTitledBorder
          (BorderFactory.createEtchedBorder(),"Waiter Information"));
     
          JPanel menuItemsJPanel = new JPanel();
          menuItemsJPanel.setLayout(null);
          menuItemsJPanel.setBounds(20, 50, 200, 200);
          waiterJPanel.setBorder(BorderFactory.createTitledBorder
         (BorderFactory.createEtchedBorder(),"Menu Items"));
     
          setLayout(null);
          JButton calculateBillJButton = new JButton("Calculate Button");
          calculateBillJButton.setBounds(250, 250, 50, 50);
     
    //     public void actionPerformed( ActionEvent event)
    //     {
    //        calculateBillJButtonActionPerformed( event );
    //     } 
    // 
     
          JLabel subtotalJLabel = new JLabel("Sub Total");
          JTextField subtotalJTextField = new JTextField();
     
     
          JLabel totalJLabel = new JLabel("Total");
          JTextField totalJTextField = new JTextField();
     
          // **** TODO ****** set properties of application's window
     
          // **** TODO ****** ensure database connection is closed
          // **** TODO ****** when user quits application
          addWindowListener(
     
             new WindowAdapter()anonymous inner class
             {
                //  event handler called when close button is clicked
                public void windowClosing( WindowEvent event )
                {
                   frameWindowClosing( event );
                }
     
             } //  end anonymous inner class
     
          ); // end addWindowListener
     
       } // end method createUserInterface
     
       // **** TODO ****** set up waiterJPanel
       private void createWaiterJPanel()
       {
          // **** TODO ****** set up waiterJPanel
     
     
          // **** TODO ****** set up tableNumberJLabel
     
          // **** TODO ****** set up tableNumberJTextField
     
          // **** TODO ****** set up waiterNameJLabel
     
          // **** TODO ****** set up waiterNameJTextField
     
       } // end method createWaiterJPanel
     
       // **** TODO ****** create menuItemsJPanel
       private void createMenuItemsJPanel()
       {
          //  set up menuItemsJPanel
          menuItemsJPanel = new JPanel();
          menuItemsJPanel.setBounds( 20, 152, 232, 152 );
          menuItemsJPanel.setBorder( BorderFactory.createTitledBorder( 
             BorderFactory.createEtchedBorder(), "Menu Items" ) );
          menuItemsJPanel.setLayout( null );
     
          //  set up beverageJLabel
          beverageJLabel = new JLabel();
          beverageJLabel.setBounds( 8, 24, 80, 24 );
          beverageJLabel.setText( "Beverage:" );
          menuItemsJPanel.add( beverageJLabel );
     
          // set up beverageJComboBox
          beverageJComboBox = new JComboBox();
          beverageJComboBox.setBounds( 88, 24, 128, 25 );
          menuItemsJPanel.add( beverageJComboBox );
          beverageJComboBox.addItemListener(
     
             new ItemListener()  // **** TODO ****** anonymous inner class
             {
                // **** TODO ****** event handler called when item in beverageJComboBox
                // **** TODO ****** is selected
                public void itemStateChanged( ItemEvent event )
                {
                   beverageJComboBoxItemStateChanged( event );
                }
     
             } // end anonymous inner class
     
          ); // end addItemListener
     
          // **** TODO ****** add items to beverageJComboBox
     
     
          // **** TODO ****** set up appetizerJLabel
     
     
          // **** TODO ****** set up appetizerJComboBox
     
          // **** TODO ****** add items to appetizerJComboBox
     
     
          // **** TODO ****** set up mainCourseJLabel
     
          // **** TODO ****** set up mainCourseJComboBox
          mainCourseJComboBox = new JComboBox();
     
          // **** TODO ****** add items to mainCourseJComboBox
     
     
          // **** TODO ****** set up dessertJLabel
     
     
       } // end method createMenuItemsJPanel
     
       // **** TODO ****** add items to JComboBox
       private void loadCategory(
          String category, JComboBox categoryJComboBox )
       {
          loadCategory("beverage", beverageJComboBox);
          loadCategory("appetizer", appetizerJComboBox);
          loadCategory("main course", mainCourseJComboBox);
          loadCategory("dessert", dessertJComboBox);
     
          try
          {
             myResultSet = myStatement.executeQuery("SELECT name FROM" + 
             "Menu WHERE category = "" + category + ");
     
             while(myResultSet.next())
             {
                 categoryJComboBox.addItem(myResultSet.getString("name"));
             }
             myResultSet.close();
          } // end try block
          catch ( SQLException SQLexception )
            {
                SQLexception.printStackTrace();
            } // end catch block         
     
       } // end method loadCategory
     
       // **** TODO ****** user select beverage
       private void beverageJComboBoxItemStateChanged( ItemEvent event )
       {
        ObJect obj = event.getSource();
        if(event.getStateChange()) == ItemEvent.SELECTED)
        {
     
        }
     
     
       } // end method beverageJComboBoxItemStateChanged
     
       // **** TODO ****** user select appetizer
       private void appetizerJComboBoxItemStateChanged( ItemEvent event )
       {
     
       } // end method appetizerJComboBoxItemStateChanged
     
       // **** TODO ****** user select main course
       private void mainCourseJComboBoxItemStateChanged( 
          ItemEvent event )
       {
     
       } // end method mainCourseJComboBoxItemStateChanged
     
       // **** TODO ****** user select dessert
       private void dessertJComboBoxItemStateChanged( ItemEvent event )
       {
     
       } // end method dessertJComboBoxItemStateChanged
     
       // **** TODO ****** user click Calculate Bill JButton
       private void calculateBillJButtonActionPerformed( 
          ActionEvent event )
       {
     
       } // end method calculateBillJButtonActionPerformed
     
       // **** TODO ****** calculate subtotal
       private double calculateSubtotal()
       {
          return 0;
     
       } // end method calculateSubtotal
     
       // **** TODO ****** user close window
       private void frameWindowClosing( WindowEvent event )
       {
     
       }  // end method frameWindowClosing
     
       // **** TODO ****** method main
       public static void main( String[] args ) 
       {
         JFrame createUserInterface = new JFrame();
         createUserInterface.setSize(500, 500);
         createUserInterface.setVisible(true);
         createUserInterface.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
         createUserInterface.setLocationRelativeTo(null);
     
     
          // **** TODO ****** check command-line arguments
          if ( args.length == 2 )
          {
             // **** TODO ****** get command-line arguments
             String databaseUserName = args[ 0 ];
             String databasePassword = args[ 1 ];
     
             // **** TODO ****** create new RestaurantBillCalculator
             RestaurantBillCalculator application = new RestaurantBillCalculator ( 
             databaseUserName, databasePassword );
     
     
          }
          else
          {
             System.out.println( "Usage: java " + 
                "RestaurantBillCalculator databaseUserName databasePassword" );
          }
     
       } // end method main
     
    } // end class RestaurantBillCalculator


    --- Update ---

    The code doesnt compile fully yet as i am going step by step.

    --- Update ---

    Just going to send it one more time, the area I am having trouble with contained code that started with Object, that was me messing around for a solution, i have deleted those lines

    // Project: RestaurantBillCalculator.java Calculates a table's bill.
     
    // Import statements for different classes
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JComponent;
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import java.util.ArrayList;
    import java.awt.Container;
    import javax.swing.BorderFactory;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.ResultSetMetaData;
    import java.sql.ResultSet;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
     
     
     
     
     
    public class RestaurantBillCalculator extends JFrame
    {
       static final String DATABASE_URL = "jdbc:mysql://localhost/restaurant";
       static final String DATABASE_USERNAME = "root";
       static final String DATABASE_PASSWORD = "password"; 
     
       // JLabel for Restaurant
       private JLabel restaurantJLabel;
     
       // JPanel to display waiter information
       private JPanel waiterJPanel;
     
       // JLabel and JTextField for table number
       private JLabel tableNumberJLabel;
       private JTextField tableNumberJTextField;
     
       // JLabel and JTextField for waiter name
       private JLabel waiterNameJLabel;
       private JTextField waiterNameJTextField;
     
       // JPanel to display menu items
       private JPanel menuItemsJPanel;
     
       // JLabel and JComboBox for beverage
       private JLabel beverageJLabel;
       private JComboBox beverageJComboBox;
     
       // JLabel and JComboBox for appetizer
       private JLabel appetizerJLabel;
       private JComboBox appetizerJComboBox;
     
       // JLabel and JComboBox for main course
       private JLabel mainCourseJLabel;
       private JComboBox mainCourseJComboBox;
     
       // JLabel and JComboBox for dessert
       private JLabel dessertJLabel;
       private JComboBox dessertJComboBox;
     
       // JButton for calculate bill
       private JButton calculateBillJButton;
     
       // JLabel and JTextField for subtotal
       private JLabel subtotalJLabel;
       private JTextField subtotalJTextField;
     
       // JLabel and JTextField for tax
       private JLabel taxJLabel;
       private JTextField taxJTextField;
     
       // JLabel and JTextField for total
       private JLabel totalJLabel;
       private JTextField totalJTextField;
     
       // constant for tax rate
       private final static double TAX_RATE = 0.05;
     
       // declare instance variables for database processing
       private Connection myConnection;
       private Statement myStatement;
       private ResultSet myResultSet;
     
       // declare instance variable ArrayList to hold bill items
       private ArrayList billItems = new ArrayList();
     
       // constructor
       public RestaurantBillCalculator( String databaseUserName, String databasePassword )
       {
         try 
         {
             myConnection = DriverManager.getConnection
            (DATABASE_URL, DATABASE_USERNAME, DATABASE_PASSWORD);
     
             myStatement = myConnection.createStatement();
     
             myResultSet = myStatement.executeQuery(DATABASE_URL);
         }
         catch ( SQLException sqlException )
          {
             sqlException.printStackTrace();
             System.exit( 1 );
          } // end catch
         finally
         {
             try
             {
                 myResultSet.close();
                 myStatement.close();
                 myConnection.close();
             } // end of try block
     
             catch ( SQLException SQLexception )
             {
                SQLexception.printStackTrace();
             } // end catch block
         } // end of finally
     
       } // end constructor
     
       // create and position GUI components; register event handlers
       private void createUserInterface()
       {
          // get content pane for attaching GUI components
          Container contentPane = getContentPane();
     
          // enable explicit positioning of GUI components 
          contentPane.setLayout( null );
     
          // **** TODO ****** set up restaurantJLabel
          JLabel restaurantJLabel = new JLabel("Restaurant");
          restaurantJLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
          restaurantJLabel.setBounds(30, 50, 100, 100);
     
          JPanel waiterJPanel = new JPanel();
          waiterJPanel.setLayout(null);
          waiterJPanel.setBounds(20, 50, 100, 100);
          waiterJPanel.setBorder(BorderFactory.createTitledBorder
          (BorderFactory.createEtchedBorder(),"Waiter Information"));
     
          JPanel menuItemsJPanel = new JPanel();
          menuItemsJPanel.setLayout(null);
          menuItemsJPanel.setBounds(20, 50, 200, 200);
          waiterJPanel.setBorder(BorderFactory.createTitledBorder
         (BorderFactory.createEtchedBorder(),"Menu Items"));
     
          setLayout(null);
          JButton calculateBillJButton = new JButton("Calculate Button");
          calculateBillJButton.setBounds(250, 250, 50, 50);
     
    //     public void actionPerformed( ActionEvent event)
    //     {
    //        calculateBillJButtonActionPerformed( event );
    //     } 
    // 
     
          JLabel subtotalJLabel = new JLabel("Sub Total");
          JTextField subtotalJTextField = new JTextField();
     
     
          JLabel totalJLabel = new JLabel("Total");
          JTextField totalJTextField = new JTextField();
     
          // **** TODO ****** set properties of application's window
     
          // **** TODO ****** ensure database connection is closed
          // **** TODO ****** when user quits application
          addWindowListener(
     
             new WindowAdapter()anonymous inner class
             {
                //  event handler called when close button is clicked
                public void windowClosing( WindowEvent event )
                {
                   frameWindowClosing( event );
                }
     
             } //  end anonymous inner class
     
          ); // end addWindowListener
     
       } // end method createUserInterface
     
       // **** TODO ****** set up waiterJPanel
       private void createWaiterJPanel()
       {
          // **** TODO ****** set up waiterJPanel
     
     
          // **** TODO ****** set up tableNumberJLabel
     
          // **** TODO ****** set up tableNumberJTextField
     
          // **** TODO ****** set up waiterNameJLabel
     
          // **** TODO ****** set up waiterNameJTextField
     
       } // end method createWaiterJPanel
     
       // **** TODO ****** create menuItemsJPanel
       private void createMenuItemsJPanel()
       {
          //  set up menuItemsJPanel
          menuItemsJPanel = new JPanel();
          menuItemsJPanel.setBounds( 20, 152, 232, 152 );
          menuItemsJPanel.setBorder( BorderFactory.createTitledBorder( 
             BorderFactory.createEtchedBorder(), "Menu Items" ) );
          menuItemsJPanel.setLayout( null );
     
          //  set up beverageJLabel
          beverageJLabel = new JLabel();
          beverageJLabel.setBounds( 8, 24, 80, 24 );
          beverageJLabel.setText( "Beverage:" );
          menuItemsJPanel.add( beverageJLabel );
     
          // set up beverageJComboBox
          beverageJComboBox = new JComboBox();
          beverageJComboBox.setBounds( 88, 24, 128, 25 );
          menuItemsJPanel.add( beverageJComboBox );
          beverageJComboBox.addItemListener(
     
             new ItemListener()  // **** TODO ****** anonymous inner class
             {
                // **** TODO ****** event handler called when item in beverageJComboBox
                // **** TODO ****** is selected
                public void itemStateChanged( ItemEvent event )
                {
                   beverageJComboBoxItemStateChanged( event );
                }
     
             } // end anonymous inner class
     
          ); // end addItemListener
     
          // **** TODO ****** add items to beverageJComboBox
     
     
          // **** TODO ****** set up appetizerJLabel
     
     
          // **** TODO ****** set up appetizerJComboBox
     
          // **** TODO ****** add items to appetizerJComboBox
     
     
          // **** TODO ****** set up mainCourseJLabel
     
          // **** TODO ****** set up mainCourseJComboBox
          mainCourseJComboBox = new JComboBox();
     
          // **** TODO ****** add items to mainCourseJComboBox
     
     
          // **** TODO ****** set up dessertJLabel
     
     
       } // end method createMenuItemsJPanel
     
       // **** TODO ****** add items to JComboBox
       private void loadCategory(
          String category, JComboBox categoryJComboBox )
       {
          loadCategory("beverage", beverageJComboBox);
          loadCategory("appetizer", appetizerJComboBox);
          loadCategory("main course", mainCourseJComboBox);
          loadCategory("dessert", dessertJComboBox);
     
          try
          {
             myResultSet = myStatement.executeQuery("SELECT name FROM" + 
             "Menu WHERE category = "" + category + ");
     
             while(myResultSet.next())
             {
                 categoryJComboBox.addItem(myResultSet.getString("name"));
             }
             myResultSet.close();
          } // end try block
          catch ( SQLException SQLexception )
            {
                SQLexception.printStackTrace();
            } // end catch block         
     
       } // end method loadCategory
     
       // **** TODO ****** user select beverage
       private void beverageJComboBoxItemStateChanged( ItemEvent event )
       {
     
     
     
       } // end method beverageJComboBoxItemStateChanged
     
       // **** TODO ****** user select appetizer
       private void appetizerJComboBoxItemStateChanged( ItemEvent event )
       {
     
       } // end method appetizerJComboBoxItemStateChanged
     
       // **** TODO ****** user select main course
       private void mainCourseJComboBoxItemStateChanged( 
          ItemEvent event )
       {
     
       } // end method mainCourseJComboBoxItemStateChanged
     
       // **** TODO ****** user select dessert
       private void dessertJComboBoxItemStateChanged( ItemEvent event )
       {
     
       } // end method dessertJComboBoxItemStateChanged
     
       // **** TODO ****** user click Calculate Bill JButton
       private void calculateBillJButtonActionPerformed( 
          ActionEvent event )
       {
     
       } // end method calculateBillJButtonActionPerformed
     
       // **** TODO ****** calculate subtotal
       private double calculateSubtotal()
       {
          return 0;
     
       } // end method calculateSubtotal
     
       // **** TODO ****** user close window
       private void frameWindowClosing( WindowEvent event )
       {
     
       }  // end method frameWindowClosing
     
       // **** TODO ****** method main
       public static void main( String[] args ) 
       {
         JFrame createUserInterface = new JFrame();
         createUserInterface.setSize(500, 500);
         createUserInterface.setVisible(true);
         createUserInterface.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
         createUserInterface.setLocationRelativeTo(null);
     
     
          // **** TODO ****** check command-line arguments
          if ( args.length == 2 )
          {
             // **** TODO ****** get command-line arguments
             String databaseUserName = args[ 0 ];
             String databasePassword = args[ 1 ];
     
             // **** TODO ****** create new RestaurantBillCalculator
             RestaurantBillCalculator application = new RestaurantBillCalculator ( 
             databaseUserName, databasePassword );
     
     
          }
          else
          {
             System.out.println( "Usage: java " + 
                "RestaurantBillCalculator databaseUserName databasePassword" );
          }
     
       } // end method main
     
    } // end class RestaurantBillCalculator

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    That's not a small program that shows the problem. It has too much stuff in it that is not part of the problem and just gets in the way of solving the problem with getting items from a list.
    For example the DB and SQL stuff. I don't have any of that.

    Can you make a small, simple program to work on this one problem?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    No because this is all one, would you like the SQL stuff?

    --- Update ---

    Im only looking how to write the code, I'm not worried about the SQL stuff, i know i have to use the ItemEvent.SELECTED to get it to work, just not sure how to start it.

    reminder:
    // below is the method
    private void beverageJComboBoxItemStateChanged( ItemEvent event )
    {
    }
     
    // below is the instance variable of ArrayList BillItems
    private ArrayList billItems = new ArrayList();

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    No. I'd like a smaller sample program that works om just the one problem:
    String representation of the selected item to the ArrayList
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    I cant give you that because it wont compile if that is what you are after?

    the question to me is this below:
    Create the beverageJComboBoxItemStateChanged method (which immediately follows loadCategory) and insert code that adds the String representation of the selected item to the ArrayList billItems. [Hint: Use the ItemEvent.SELECTED constant to determine whether an item is selected.]
    [B]

    [B]// this below is the ArrayList billItems[/B]
    private ArrayList billItems = new ArrayList();
     
     
    [B]//this below is where the method is that i need to write this code [/B]
    private void beverageJComboBoxItemStateChanged( ItemEvent event )
    {}

    I dont know how to start this inside this method.
    Last edited by SeanyC; September 3rd, 2014 at 11:54 AM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    Please make a small simple program that compiles, executes and shows the problem. Something that is easy to test.

    I dont know how to start this inside this method.
    What methods does the JComboBox have that will help you get the data you want? Call the methods, get their returned values and print them out to see what is there and what will give you what you want.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    This is the only method, the question i want answered is how do i do an itemEvent.selected inside this method??

    Okay if you want a small program, i'll try one, not sure if this will work though

    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
     
     
    public class RestaurantBillCalculator extends JFrame
    {
    private JComboBox beverageJComboBox;
     
    private ArrayList billItems = new ArrayList();
     
    private void beverageJComboBoxItemStateChanged( ItemEvent event )
    {// code for the ItemEvent.selected goes here i believe, im am stuck with this in the sense of how to do it}
     
     
     
    public static void main(String... Args)
    {
      JFrame createUserInterface = new JFrame();
         createUserInterface.setSize(500, 500);
         createUserInterface.setVisible(true);
         createUserInterface.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
         createUserInterface.setLocationRelativeTo(null);
     
    }
     
     
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    That's too small and it doesn't begin to show the problem. There needs to be a frame with a combobox with a list of items and a listener for the combobox.

    How much of the code in post#5 did you write?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    That's what i am asking for, how do i do the LISTENER inside that method? all i want is an example of how to do the ItemEvent.selected within beverageJComboBoxItemStateCHanged method. I am not not worried about it compiling just yet.
    e.g would it go like something below???
    private void beverageJComboBoxItemStateChanged( ItemEvent event )
      {              
    String item = (String)e.getItem();
                    if (e.getStateChange() == ItemEvent.SELECTED)
                    {
                        feedback.append(item + " was selected\n");
                    }
                    else
                    {
                        feedback.append(item + " was deselected\n");
                    }
                }

    I am a java beginner so don't go into series hardcore detail. All i want is to KNOW HOW to do an ItemEvent.Selected from the code above, that's all

  14. #14
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    First of all, you dont "do an ItemEvent.Selected". There is no such thing.
    What exists is ItemEvent.SELECTED. This is a public static final integer variable that is part of the ItemEvent class. You need to use this variable at some point in your program and what you have so far is already using it correctly.

    Try to use this code first:
    private void beverageJComboBoxItemStateChanged( ItemEvent event )
      {              
                    if (e.getStateChange() == ItemEvent.SELECTED)
                    {
                        System.out.println("selected something");
                    }
                    else
                    {
                        System.out.println("unselected something");
                    }
                }
    which is almost what you have got, but slightly changed. What this will do is print a message to the console when the selection of the combo box is changed.
    It will show you when something was selected or unselected.

    Once you have got this working you have to find out how to get the item that was selected.
    When you managed to do that, you have to find out how to get a textual representation for that item.

    Perhaps you read the API for ItemEvent, JComboBox and whatever class you use for your Items.

  15. #15
    Member
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    I aware ItemEvent.selected does not exist, i should have spelt it correctly sorry and also thanks, ill give it a go.

  16. #16
    Member
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Insert code that adds the String representation of the selected item to the ArrayList billItems.

    After messing around with it a lot i finally got it working, its fairly similar to what you said except i need it to be associated with the ArrayList billItems

    code is:
    private void beverageJComboBoxItemStateChanged( ItemEvent event )
      {              
           if (e.getStateChange() == ItemEvent.SELECTED)
             {
                billItems.add((String)beverageJComboBox.getSelecteditem());
             }
    } // end of beverageJComboBoxItemStateChanged

    I don't need the ELSE because i am only concerned with selecting an Item not deselecting!

Similar Threads

  1. Replies: 0
    Last Post: July 21st, 2014, 04:40 PM
  2. display item for item selected in combobox
    By cisco.nat in forum AWT / Java Swing
    Replies: 1
    Last Post: June 20th, 2013, 07:12 AM
  3. Replies: 0
    Last Post: February 12th, 2011, 07:44 PM
  4. Problem getting selected item as string from JComboBox
    By lost in forum AWT / Java Swing
    Replies: 1
    Last Post: October 26th, 2010, 03:22 AM
  5. drag item or insert item into new Jlabel in JPanel
    By qaromi in forum AWT / Java Swing
    Replies: 5
    Last Post: July 6th, 2010, 07:37 PM