We help IT Professionals succeed at work.

Insert the keys in a map into a combobox

galneweinhaw
galneweinhaw asked
on
255 Views
Last Modified: 2008-02-26
Using my color map below, I want to insert the keys into a combo box.... how?

            JComboBox combo = (JComboBox)panel.getComboBox("colorComboBox");
            for( int i = 0; i < COLOR_MAP.size(); i++ )
                  combo.insertItemAt(??????, i);      


    public static final Map<String, Color> COLOR_MAP = new HashMap<String, Color>();

    static {
      COLOR_MAP.put("black", Color.BLACK);
      COLOR_MAP.put("blue", Color.BLUE);
      COLOR_MAP.put("cyan", Color.CYAN);
      COLOR_MAP.put("darkgray", Color.DARK_GRAY);
      COLOR_MAP.put("gray", Color.GRAY);
      COLOR_MAP.put("green", Color.GREEN);
      COLOR_MAP.put("lightgray", Color.LIGHT_GRAY);
      COLOR_MAP.put("magenta", Color.MAGENTA);
      COLOR_MAP.put("orange", Color.ORANGE);
      COLOR_MAP.put("pink", Color.PINK);
      COLOR_MAP.put("red", Color.RED);
      COLOR_MAP.put("white", Color.WHITE);
      COLOR_MAP.put("yellow", Color.YELLOW);
    }
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
which is more efficient?

Author

Commented:
         for( String key : COLOR_MAP.keySet() )
               combo.insertItemAt(key, i);    

there is no "i" in this =)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> which is more efficient?

if u just want the combo poluated with the keyt then use the latter

> there is no "i" in this =)

 combo.addItem(key);    

Author

Commented:
last followup =)

what do you use to get the "default" os/skin button colour?
is there a "nocolor" option or something similar I can add to the map?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
UIManager.get("Button.background");

Author

Commented:
UIManager.get("Button.background");

how do I use this?

Author

Commented:
using:
this.setBackground((Color)UIManager.get("Button.background"));

it sets all the buttons to white instead of the default cream color
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Color defaultColour = (Color) UIManager.get("Button.background");

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> it sets all the buttons to white instead of the default cream color

thats probably the look and feel you're using overriding things

Author

Commented:
ok, so how do I get the default

Author

Commented:
there is no look and feel, it's just all defaults untill I changed the colors...now I can't get them back =(
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
theres always a look and feel :)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
this will tell u the default color being used :)

Color bg = new JButton().getBackgroundColor();

Author

Commented:
setBackground(new JButton().getBackground());

= all white buttons......NOOOOOOoooooooooo!!!!!!!!!.........
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
what is it u are trying to achieve??

Author

Commented:
I just want to set a button to the default color scheme so it goes along with everything else (which for me is a creamy gray)

right now I have some white buttons beside all sorts of other default coloured controls, and I can't get the white buttons to go back =(

Author

Commented:
white or red or whatever I set them too.

but if I comment out the setbackground line....they still show up white or red ro whatever, like they've saved their color
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
if you don't set the button colour then they will be the default colour :)
the only way for the colour to change is if you are changing it.

Author

Commented:
is there any other way to change a buttons colour than SetBackground? cus I searched the prject for that.... =P
CERTIFIED EXPERT
Top Expert 2016

Commented:
combo.setModel(new Vector<String>(COLOR_MAP.keySet()));
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> is there any other way to change a buttons colour than SetBackground? cus I searched the prject for that.... =P

nope

Author

Commented:
Thanks guys, my new problem is another topic =)
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)

Author

Commented:
btw, I found out how to get the default color back:

setBackground(null);

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.