Project #66001 - 3 Java Discussion Questions

Respond the 3 classmates answer to original discussion question.  

 

Provide explanation on their answer.  say you agree or disagree and elaborate on your response.  YOU ARE NOT GRADING THEIR WORK RATHER REFLECTING ON IT.  

 

ORIGINAL DISCUSSION QUESTION (DO NOT ANSWER) 

How do arrays and combo boxes work together? Write a small program in which you add the elements of an array to a combo box and post the code for your program.

 

#1 Respond to Victor’s answer to Original Discussion question in 200 words or more.  

 

Arrays and combo boxes work together in that the combo box will display the contents of an array in a drop down menu fashion, which can be convenient. When creating the combo box it's important to call on the name of the array in order to implement it. To show this, I've written a simple program that will display a JFrame with a short list of my favorite foods. I welcome any critiques and ways I can improve my code. I'm still getting used to the naming conventions, along with some of the other formatting techniques, but I feel I am leaps and bounds from when I first started. Still, I hope I can learn more from my peers and instructor. Thank you.

 

 

import java.awt.FlowLayout;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

 

/*

 * This program was written for 

 * University of Phoenix

 * By Victor M. Beas

 */

 

public class ComboBox extends JFrame {

    private JComboBox myJComboBox;

    private JLabel label;

    private static final String[] foodItems = 

    { "Thin Crust Pizza", "Tuna Salad", "Egg and Salsa Tacos", "Pita Bread Sandwiches", "And many more..." };

    

    public ComboBox() {

        super("Victor's Favorite Foods");

        setLayout(new FlowLayout());

        

        myJComboBox = new JComboBox(foodItems);

        myJComboBox.setMaximumRowCount(3);

        

 v       

        add(myJComboBox);

    }

}

 

 

 

import javax.swing.JFrame;

 

 

public class ComboBoxDemo {

    public static void main(String[] args) {

        ComboBox cBoxExample = new ComboBox();

        cBoxExample.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        cBoxExample.setSize(350,200);

        cBoxExample.setVisible(true);

    }

    

}

 

 

#2 Respond to Michelle’s answer to Original Discussion question in 200 words or more.  

 

Array's and combo boxes work together as the Array will create and hold the variables that will be available in your combo box drop down. 

 

public class ComboBoxExample{

   

   private JFrame mainFrame;

   private JLabel headerLabel;

   private JLabel statusLabel;

   private JPanel controlPanel;

 

   public ComboBoxExample (){

      prepareGUI();

   }

 

   public static void main(String[] args){

      ComboBoxExample CBexample = new ComboBoxExample ();     

      CBexample. ComboBoxExample ();

   }

 

   private void prepareGUI(){

      mainFrame = new JFrame("Combo Box Example");

      mainFrame.setSize(400,400);

      mainFrame.setLayout(new GridLayout(3, 1));

      mainFrame.addWindowListener(new WindowAdapter() {

         public void windowClosing(WindowEvent windowEvent){

            System.exit(0);

         }       

      });   

      headerLabel = new JLabel("", JLabel.CENTER);       

      statusLabel = new JLabel("",JLabel.CENTER);   

 

      statusLabel.setSize(350,100);

 

      controlPanel = new JPanel();

      controlPanel.setLayout(new FlowLayout());

 

      mainFrame.add(headerLabel);

      mainFrame.add(controlPanel);

      mainFrame.add(statusLabel);

      mainFrame.setVisible(true); 

   }

 

   private void showComboboxDemo(){                                   

      headerLabel.setText("Control in action: JComboBox");

 

      final DefaultComboBoxModel VegName = new DefaultComboBoxModel();

 

      VegName.addElement("Beets");

      VegName.addElement("Lettuce");

      VegName.addElement("Tomato");

      VegName.addElement("Potato");

 

      final JComboBox vegCombo = new JComboBox(VegName);   

      vegCombo.setSelectedIndex(0);

 

      JScrollPane vegListScrollPane = new JScrollPane(vegCombo);   

 

      JButton showButton = new JButton("Show");

 

      showButton.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {

            String data = "";

            if (vegCombo.getSelectedIndex() != -1) {                    

               data = "Vegetables Selected: "

                  + vegCombo.getItemAt

                    (vegCombo.getSelectedIndex());            

            }             

            statusLabel.setText(data);

         }

      });

      controlPanel.add(vegListScrollPane);         

      controlPanel.add(showButton);   

      mainFrame.setVisible(true);            

   }

}

 

#3 Respond to Daryn’s answer to Original Discussion question in 200 words or more. 

 

The relationship between arrays and combo boxes is pretty simple. When an array is passed to a combo box, the objects in the array populate the combo box. The following code is an example of how arrays and combo boxes can work together. I have also attached the .java file in case anyone would like to tinker with the code. 

 

package toolcbox;

 

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

 

public class ToolCBox extends JFrame

{

private JComboBox toolCBox;

private JPanel toolPnl;

private JPanel selectedToolPnl;

private JLabel toolLbl;

private JTextField toolTxt; 

private String[] tools = {"Wrench", "Hammer", "Saw", "Grinder"};

 

//Class constructor

public ToolCBox()

{

super("Tool Inventory");

 

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

setLayout(new BorderLayout());

 

buildToolPnl();

buildSelectedToolPnl();

 

add(toolPnl, BorderLayout.CENTER);

add(selectedToolPnl, BorderLayout.SOUTH);

 

pack();

setVisible(true);

}

 

//Build panel w/ combo box

private void buildToolPnl()

{

toolPnl = new JPanel();

 

toolCBox = new JComboBox(tools);

 

toolCBox.addActionListener(new ToolCBoxListener());

 

toolPnl.add(toolCBox);

}

 

//Build panel w/ selection display

private void buildSelectedToolPnl()

{

selectedToolPnl = new JPanel();

 

toolLbl = new JLabel("You selected: ");

 

toolTxt = new JTextField(10);

toolTxt.setEditable(false);

 

selectedToolPnl.add(toolLbl);

selectedToolPnl.add(toolTxt);

}

 

//Inner listener class to handle combo box selection event

private class ToolCBoxListener implements ActionListener

{

@Override

public void actionPerformed(ActionEvent e)

{

String selection = (String) toolCBox.getSelectedItem();

toolTxt.setText(selection);

}

}

 

public static void main(String[] args) 

{

new ToolCBox();

}

 

}

Subject Computer
Due By (Pacific Time) 04/10/2015 10:00 pm
Report DMCA
TutorRating
pallavi

Chat Now!

out of 1971 reviews
More..
amosmm

Chat Now!

out of 766 reviews
More..
PhyzKyd

Chat Now!

out of 1164 reviews
More..
rajdeep77

Chat Now!

out of 721 reviews
More..
sctys

Chat Now!

out of 1600 reviews
More..
sharadgreen

Chat Now!

out of 770 reviews
More..
topnotcher

Chat Now!

out of 766 reviews
More..
XXXIAO

Chat Now!

out of 680 reviews
More..
All Rights Reserved. Copyright by AceMyHW.com - Copyright Policy