Reputation: 2871
So I'm attempting to make a pretty window displaying the color selected from the combo box. But I got the window and combo box made, for some reason my color isn't displayed. Can anyone help me?
Also from what I can tell, my combo box is at the top of my window. I would like to have it shown below the color.
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
class Colors extends JFrame implements ItemListener
{
Choice chooseColor = new Choice();
Label lblQts = new Label("Choose color you like : ");
public Colors(String title)
{
super(title);
setLayout(new FlowLayout());
chooseColor.addItem("red");
chooseColor.addItem("green");
chooseColor.addItem("blue");
add(lblQts);
add(chooseColor);
chooseColor.addItemListener(this);
}
public void itemStateChanged(ItemEvent e)
{
String c;
Color color;
c = chooseColor.getSelectedItem();
color=Color.getColor(c);
setBackground(color);
}
}
public static void main(String[] args)
{
Colors objColor = new Colors("Color Chooser");
objColor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
objColor.setSize(400,400);
objColor.setVisible(true);
}
Upvotes: 2
Views: 3864
Reputation: 347184
Let's assume for a moment that you really should be using the Swing API and not the AWT API (and seen as you're only just learning I think it's a decent assumption to make).
You can do the following...
Basically. I have JComboBox
with a custom ListCellRenderer
and ActionListener
.
The ListCellRenderer
renders the items in a fashion I want and the ActionListener
listeners for changes to the combobox.
When a new item is selected, it will change the background of the combo box based on the selected item.
The concepts demonstrated here are crucial to understanding Swing (and Java in general)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxEditor;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestComboBox08 {
public static void main(String[] args) {
new TestComboBox08();
}
public TestComboBox08() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
final JComboBox cb = new JComboBox();
cb.addItem(new SelectableColor("Red", Color.RED));
cb.addItem(new SelectableColor("Green", Color.GREEN));
cb.addItem(new SelectableColor("Blue", Color.BLUE));
cb.setRenderer(new ColorCellRenderer());
add(cb);
cb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object value = cb.getSelectedItem();
if (value instanceof SelectableColor) {
cb.setBackground(((SelectableColor)value).getColor());
} else {
cb.setBackground(null);
}
}
});
cb.setSelectedItem(null);
}
}
public class SelectableColor {
private String name;
private Color color;
public SelectableColor(String name, Color color) {
this.name = name;
this.color = color;
}
public String getName() {
return name;
}
public Color getColor() {
return color;
}
}
public class ColorCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof SelectableColor) {
SelectableColor sc = (SelectableColor) value;
if (!isSelected) {
setBackground(sc.getColor());
setOpaque(true);
}
setText(sc.getName());
}
return this;
}
}
}
You really should familiarise yourself with Creating A UI with Swing. If that is going over your head, start with Trails covering the basics
Upvotes: 3
Reputation: 18569
You need to set contentPane's background, not JFrame
background.
Then, you can't use Color.getColor
to retrieve your color object for this case. See this
This is the working code:
import java.awt.Choice;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.lang.reflect.Field;
import javax.swing.JFrame;
public class Colors extends JFrame implements ItemListener
{
Choice chooseColor = new Choice();
Label lblQts = new Label("Choose color you like : ");
public Colors (String title) {
super(title);
setLayout(new FlowLayout());
chooseColor.addItem("red");
chooseColor.addItem("green");
chooseColor.addItem("blue");
add(lblQts);
add(chooseColor);
chooseColor.addItemListener(this);
}
public void itemStateChanged(ItemEvent e) {
String c;
Color color;
c = chooseColor.getSelectedItem();
try {
Field field = Class.forName("java.awt.Color").getField(c);
color = (Color)field.get(null);
} catch (Exception ex) {
color = null; // Not defined
}
getContentPane().setBackground(color);
}
public static void main(String[] args) {
Colors objColor = new Colors ("Color Chooser");
objColor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
objColor.setSize(400, 400);
objColor.setVisible(true);
}
}
Upvotes: 1