I am trying to change color using getColor(String name) function but it seems not to work.May be I am doing some kind of mistake. Here is the code:
public class ComboBoxPractice extends JFrame{
private String names={"Color.YELLOW","Color.RED","Color.GREEN"};
public ComboBoxPractice()
{
...
box.addItemListener(
new ItemListener()
{
public void itemStateChanged(ItemEvent event)
{
if(event.getStateChange()==event.SELECTED)
{
getContentPane().setBackground(Color.getColor(names[box.getSelectedIndex()]));
}
}
}
);
}
}
I have also tried the String array containing Color names alone like YELLOW , RED, BLUE. but it wasn’t working.
I know the other way of changing color by making an array of Class Color, but I wanted to try this method.
What am I doing wrong?
Regards
Either use the Color class as :
If you use the
Colorclass, then you may update statement below as:as
or use the names as “black”, “red” etc.
If you decide to use the color name as above, then you may get the
Colorobject using reflection as below: