0

//this is the array wherein colors are in dropbox type....

  private final String[] Options = new String[] {"Select Color", "RED" , "YELLOW", "BLUE" , "GREEN"};
       private final JComboBox cmb1 = new JComboBox (Options);

//this is the switch case.. if I choose red, the whole frame will be in red. enter image description here

    private class ButtonHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

        // Get the selected value on the JCOmboBox
        String SelectedValue = cmb1.getSelectedItem().toString();

        // Display
        switch (SelectedValue){

//if i choose red, the text will display in the frame below. How can I make the frame in the "red" turns red??

        case "RED" :
            txt1.setText ("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon"); break;

        case "YELLOW" :
            txt1.setText ("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron"); break;

        case "BLUE" :
            txt1.setText ("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire"); break;

        case "GREEN" :
            txt1.setText ("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive"); break;

        default:
            txt1.setText("");

            }
        }
    }

This is what I've done... want to insert colors..


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

public class ComboBoxColor extends JFrame {
    // Declare Combo Box with Values from Array
    private final String[] Options = new String[] { "Select Color", "RED",
            "YELLOW", "BLUE", "GREEN" };
    private final JComboBox cmb1 = new JComboBox(Options);

    public void itemStateChanged(java.awt.event.ItemEvent evt) {
        itemStateChanged(evt);
    }

    // Declare JTextArea and wrap it in a JScrollPane
    private final JTextArea txt1 = new JTextArea(2, 20);
    private final JScrollPane scr = new JScrollPane(txt1);
    // ActionListener
    private final ButtonHandler BH = new ButtonHandler();
    public JComboBox combo;

    // Constructor
    public ComboBoxColor() {
        // Add ActionListener
        cmb1.addActionListener(BH);
        // Set Layout
        Container pane = this.getContentPane();
        pane.setLayout(new GridLayout(2, 4));
        // Add Components
        pane.add(cmb1);
        pane.add(scr);
        // Set JFrame Properties
        this.setTitle("COMBO BOX COLOR");
        this.setSize(500, 150);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    // ActionListener Sub-Class
    private class ButtonHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Get the selected value on the JCOmboBox
            String SelectedValue = cmb1.getSelectedItem().toString();
            // Display
            switch (SelectedValue) {
            case "RED":
                txt1.setText("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon");
                break;
            case "YELLOW":
                txt1.setText("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron");
                break;
            case "BLUE":
                txt1.setText("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire");
                break;
            case "GREEN":
                txt1.setText("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive");
                break;
            default:
                txt1.setText("");
            }
        }
    }

    // Main Method
    public static void main(String[] args) {
        ComboBoxColor CC = new ComboBoxColor();
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Blahh
  • 11
  • 5

2 Answers2

0

You didn't added textfield component to pane. That was the reason it was not changing.

You must add component to the panel before making changes in it, using..

pane.add(txt1);

Here is working source code :

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

public class ComboBoxColor extends JFrame {
   // Declare Combo Box with Values from Array
   private final String[] Options = new String[] {"Select Color", "RED" ,"YELLOW", "BLUE" , "GREEN"};
   private final JComboBox<String>  cmb1 = new JComboBox<String>(Options);
   // Declare JTextArea and wrap it in a JScrollPane
   private final JTextArea txt1 = new JTextArea (2,20);
   private final JTextArea txt2 = new JTextArea (2,20);
   private final JScrollPane scr = new JScrollPane(txt1);
   // ActionListener
   private final ButtonHandler BH = new ButtonHandler();

   // Constructor
   public ComboBoxColor() {

  // Add ActionListener
  cmb1.addActionListener(BH);

  // Set Layout
  Container pane = this.getContentPane();
  pane.setLayout(new GridLayout (2,1));

  // Add Components
  pane.add(cmb1); pane.add(scr);

  //Here you didn't add textField Component to the pane
  pane.add(txt1);
  pane.add(txt2);


  // Set JFrame Properties
  this.setTitle("COMBO BOX COLOR");
  this.setSize(500,150);
  this.setLocationRelativeTo(null);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setVisible(true);

  }    

  // ActionListener Sub-Class
  private class ButtonHandler implements ActionListener {
  @Override
  public void actionPerformed(ActionEvent e) {
      // Get the selected value on the JCOmboBox
      String SelectedValue = cmb1.getSelectedItem().toString();
      // Display
      switch (SelectedValue){
          case "RED" :
              txt1.setText ("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon");
              txt2.setText ("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon");
              txt1.setBackground(Color.red);
              txt2.setForeground(Color.red);
              break;

          case "YELLOW" :
              txt1.setBackground(Color.yellow);
              txt1.setText ("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron"); 
              txt2.setForeground(Color.yellow);
              txt2.setText ("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron"); 
              break;
          case "BLUE" :
              txt1.setBackground(Color.blue);
              txt1.setText ("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire");
              txt2.setForeground(Color.blue);
              txt2.setText ("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire");
              break;

          case "GREEN" :
              txt1.setBackground(Color.green);
              txt1.setText ("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive"); 
              txt2.setForeground(Color.green);
              txt2.setText ("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive"); 
              break;

          default:
              txt1.setText("");
          }
      }
  }
 // Main Method
 public static void main (String[]args){
      ComboBoxColor CC = new ComboBoxColor();
 } 
}

Hope the question is solved now.

Reck
  • 1,388
  • 11
  • 20
  • Why should an ItemListener work better than an ActionListener? – Hovercraft Full Of Eels Oct 09 '15 at 18:28
  • @Hovercraft , Here is the stackoverflow answer for the question http://stackoverflow.com/a/3499275/5364318 – Reck Oct 09 '15 at 19:09
  • @Hovercraft, Have you down voted the answer. But the answer does solves the above question. I have checked it before using actionPerformed event but it doesnt work and later I used itemStateChanged event for the same and it worked. – Reck Oct 09 '15 at 19:17
  • @HovercraftFullOfEels , and about your question you asked in the Comments . As I was not pretty sure about the difference between both. I checked it in stackoverflow. And I got a answer for it. You should rather have a look over it. And do run the code and check whether above problem is solved or not. – Reck Oct 09 '15 at 19:20
  • @HovercraftFullOfEels , as you have already downvoted. Let me know what you got it wrong. – Reck Oct 09 '15 at 19:43
  • The question is why isn't his code working, and while yes, you've posted working code, it doesn't explain his problem at all, and I can guarantee you, at least with your code, that using an ItemListener is no better than using an ActionListener. To prove this, remove your line where you add an ItemListener to the combo box, and in its place add an ActionListener. Inside of the ActionListener's actionPerformed, simply call your `comboItemStateChanged(null);` method, but pass in a null argument, and the code will work just as well. This answer then does not answer the question. – Hovercraft Full Of Eels Oct 09 '15 at 21:22
  • What answer **will** answer the question? In its present state, no one can answer it without guessing because the original poster has refused to post his [mcve], and so none of us really knows what is wrong. – Hovercraft Full Of Eels Oct 09 '15 at 21:23
  • @Blahh: don't thank me -- improve your question please. In its present state it's unanswerable without guessing. – Hovercraft Full Of Eels Oct 10 '15 at 04:09
  • @HovercraftFullOfEels ,yeah!!you are right. It works with ActionListener and as well as Item Listener. – Reck Oct 10 '15 at 05:11
  • @HovercraftFullOfEels , I guess I got the error. Please checkout whether it is solved. – Reck Oct 10 '15 at 07:30
  • @Blahh , you were not adding textfield component in the the pane. – Reck Oct 10 '15 at 07:32
0

This is what I've done, I want to insert colors:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ComboBoxColor extends JFrame { 
    
    // Declare Combo Box with Values from Array
    private final String[] Options = new String[] {"Select Color", "RED" ,      "YELLOW", "BLUE" , "GREEN"};
        private final JComboBox cmb1 = new JComboBox (Options);
    
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            itemStateChanged(evt);
        }
    
    // Declare JTextArea and wrap it in a JScrollPane
    private final JTextArea txt1 = new JTextArea (2,20);
    private final JScrollPane scr = new JScrollPane(txt1);
    
    // ActionListener
    private final ButtonHandler BH = new ButtonHandler();
    public JComboBox combo;
    
    
    // Constructor
    public ComboBoxColor () {
        
        // Add ActionListener
        cmb1.addActionListener(BH);
        
        // Set Layout
        Container pane = this.getContentPane();
        pane.setLayout(new GridLayout (2,4));
        
        // Add Components
        pane.add(cmb1); pane.add(scr);
        
        
        // Set JFrame Properties
        this.setTitle("COMBO BOX COLOR");
        this.setSize(500,150);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        
    }
    
    // ActionListener Sub-Class
    private class ButtonHandler implements ActionListener {
        
        @Override
        public void actionPerformed(ActionEvent e) {
            
            // Get the selected value on the JCOmboBox
            String SelectedValue = cmb1.getSelectedItem().toString();
            
            // Display
            switch (SelectedValue){
            
            case "RED" :
                txt1.setText ("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon"); break;
                            
            case "YELLOW" :
                txt1.setText ("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron"); break;
                        
            case "BLUE" :
                txt1.setText ("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire"); break;
            
            case "GREEN" :
                txt1.setText ("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive"); break;
            
            default:
                txt1.setText("");
                
                }
            }

        
        }
        
    
    // Main Method
    public static void main (String[]args){
        ComboBoxColor CC = new ComboBoxColor ();
    }
}
Tenten Ponce
  • 2,436
  • 1
  • 13
  • 40
Blahh
  • 11
  • 5