title: Trying to add a JComboBox dropdown into a JTextField area
body: <p>I have spent many hours digging around the web and cannot seem to find a straightforward answer or method to add a dropdown selection into my main panel that houses the various text fields, in this case, I am trying to add a paint color selection to the panel so that color can then be used as a variable in determining cost.</p>
  <p>The closest I have come so far is having a combo box popup in a new window but then was unable to retrieve the selection.</p>
  <pre><code>package paintestimator;
  import java.lang.String;
  import java.awt.Button;
  import java.awt.Choice;
  import java.awt.Component;
  import java.awt.Frame;
  import java.awt.GridLayout;
  import java.awt.Label;
  import java.awt.PopupMenu;
  import javax.swing.JComboBox;
  import javax.swing.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  
   public class PaintEstimator extends JFrame
       {
  
  
  
  
  
  private JTextField wallHeight = new JTextField(3);
  private JTextField wallWidth = new JTextField(3);
  private JTextField wallArea = new JTextField(3);
  private JTextField gallonsNeeded = new JTextField(3);
  private JTextField cansNeeded = new JTextField(3);
  private JTextField paintTime = new JTextField(3);
  private JTextField sColor = new JTextField(3);
  private JTextField matCost = new JTextField(3);
  private JTextField laborCost = new JTextField(3);
  
  //Josh
  public PaintEstimator()
  
  {
  
      
      JButton CalcChangeBTN = new JButton(&quot;Calculate&quot;);
      JButton ClearBTN = new JButton(&quot;Clear&quot;);
      JButton ChoiceA = new JButton(&quot;Paint Color&quot;);
      //JComboBox vendorBTN = (new JComboBox());
      
      ChoiceA.addActionListener(new ChoiceAListener());
      CalcChangeBTN.addActionListener(new CalcChangeBTNListener());
      ClearBTN.addActionListener(new ClearBTNListener());
      
      wallHeight.setEditable(true);
      wallWidth.setEditable(true);
      wallArea.setEditable(false);
      gallonsNeeded.setEditable(false);
      paintTime.setEditable(false);
      cansNeeded.setEditable(false);
     sColor.setEditable(true); 
      matCost.setEditable(false);
      laborCost.setEditable(true);
  
  
  
      JPanel mainPanel = new JPanel();
      
  
      mainPanel.setLayout(new GridLayout(16, 3, 0, 0));
  
      // need cost of paint array to set equations for material cost
      // need combobox arrays for both color and cost
      // vendor selection combo box sets array field for cost/color
      
      
  
      mainPanel.add(new JLabel(&quot;Please enter wall height in feet&quot;));
  
      mainPanel.add(wallHeight);
  
      mainPanel.add(new JLabel(&quot;please enter wall width in feet&quot;));
  
      mainPanel.add(wallWidth);
  
      //box to show chosen color 
      //mainPanel.add(new JLabel(&quot;Color Chosen&quot;));
     // mainPanel.add(sColor);
  
      mainPanel.add(new JLabel(&quot;wall area&quot;));
  
      mainPanel.add(wallArea);
  
      mainPanel.add(new JLabel(&quot;Gallons Needed&quot;));
  
      mainPanel.add(gallonsNeeded);
  
      mainPanel.add(new JLabel(&quot;Number of cans Needed&quot;));
      mainPanel.add(cansNeeded);
  
      mainPanel.add(new JLabel(&quot;Time to paint in Hours&quot;));
  
      mainPanel.add(paintTime);
  
      mainPanel.add(new JLabel(&quot;Cost of Labor&quot;));
      mainPanel.add(laborCost);
  
      mainPanel.add(new JLabel(&quot;Total Cost of Material&quot;));
      mainPanel.add(matCost);
  
      mainPanel.add( new JLabel(&quot;Select a Color&quot;));
      mainPanel.add (ChoiceA);
     // mainPanel.add(sColor);
      
      
     // Select&lt;String&gt; select = new Select&lt;&gt;();
  //select.setLabel(&quot;Sort by&quot;);
  //select.setItems(&quot;Most recent first&quot;, &quot;Rating: high to low&quot;,
   // &quot;Rating: low to high&quot;, &quot;Price: high to low&quot;, &quot;Price: low to high&quot;);
  //select.setValue(&quot;Most recent first&quot;);
     // mainPanel.add(select);
      
      mainPanel.add(CalcChangeBTN);
  
      mainPanel.add(ClearBTN);
      
     // mainPanel.add(ChoiceA);
      
      setContentPane(mainPanel);
  
      pack();
  
      setTitle(&quot;Paint Estimator Tool&quot;);
  
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      setLocationRelativeTo(null);
  }
  
  
  
  
  //Josh
  class CalcChangeBTNListener implements ActionListener
  {
  
      public void actionPerformed(ActionEvent e)
      {
          try
          {
  
              final double paintCvrGal = 320.0;
  
              final double gallonsPerCan = 1.0;
  
              double h = Integer.parseInt(wallHeight.getText());
  
              double w = Integer.parseInt(wallWidth.getText());
  
              double a = h * w;
  
              double c = ((a / paintCvrGal) * gallonsPerCan);
  
              double n = (c / gallonsPerCan);
  
              double p = (int) ((a * 0.76) / 60);
  
              double l = Integer.parseInt(laborCost.getText());
  
              double labCost = l * p;
  
              // double mc = c * CostofPaint
              wallArea.setText(String.valueOf(a));
              String wallArea = String.valueOf(a);
  
              gallonsNeeded.setText(String.valueOf(c));
              String gallonsNeeded = Double.toString(c);
  
              cansNeeded.setText(String.valueOf(n));
              String cansNeeded = Double.toString(n); // still need refine decimal point to #.0 
   (one decimal place)
  
              paintTime.setText(String.valueOf(p));
              String paintTime = String.valueOf(p);
  
              laborCost.setText(String.valueOf(labCost));
              String laborCost = Double.toString(labCost);
              
              
              
  
  
          } catch (NumberFormatException f)
          {
              wallHeight.requestFocus();
              wallHeight.selectAll();
          }
      }
  
  }
  
  class ClearBTNListener implements ActionListener
  {
  
      public void actionPerformed(ActionEvent e)
      {
  
  // clear text fields and set focus
          cansNeeded.setText(&quot;&quot;);
          gallonsNeeded.setText(&quot;&quot;);
          wallArea.setText(&quot;&quot;);
          wallHeight.setText(&quot;&quot;);
          wallWidth.setText(&quot;&quot;);
          paintTime.setText(&quot;&quot;);
          sColor.setText(&quot;&quot;);
          laborCost.setText(&quot;&quot;);
          //set focus
          wallHeight.requestFocus();
      }
  }
  
  class ChoiceAListener implements ActionListener
  {
  public void actionPreformed (ActionEvent e)
  { 
      
     
  
  }
  public static void main(String[] args)
  {
     
  
  
      
      PaintEstimator window = new PaintEstimator();
      window.setVisible(true);
      
  
  }
  
  }
  </code></pre>
  <p>Below is the combo box I was able to create:</p>
  <pre><code>public static void main(String[] args)
  {
      String[] optionsToChoose =
      {
          &quot;White&quot;, &quot;Cream&quot;, &quot;Sage&quot;, &quot;Light Blue&quot;, &quot;Eggshell White&quot;
      };
  
      JFrame jFrame = new JFrame();
  
      JComboBox&lt;String&gt; jComboBox = new JComboBox&lt;&gt;(optionsToChoose);
      jComboBox.setBounds(80, 50, 140, 20);
  
      JButton jButton = new JButton(&quot;Done&quot;);
      jButton.setBounds(100, 100, 90, 20);
  
      JLabel jLabel = new JLabel();
      jLabel.setBounds(90, 100, 400, 100);
  
      jFrame.add(jButton);
      jFrame.add(jComboBox);
      jFrame.add(jLabel);
  
      jFrame.setLayout(null);
      jFrame.setSize(350, 250);
      jFrame.setVisible(true);
  
      jButton.addActionListener((ActionEvent e) -&gt;
      {
          String selectedColor = &quot;You selected &quot; + 
  jComboBox.getItemAt(jComboBox.getSelectedIndex());
          jLabel.setText(selectedColor);
      });
  
  }
  </code></pre>