I am working on a project that requires me to read colors and Hex codes for the colors into a map from a text file. I have a TreeMap created and it stores and prints properly on the screen, but I can not figure out how to get it to send the data to a combo box. Here is my code thus far…
public class Project extends JFrame{
JComboBox CBColor = new JComboBox(new String[]
{"", "AQUA", "BLACK", "BLUE", "BROWN", "FUCHSIA", "GRAY",
"GREEN", "INDIGO", "LIME", "MAROON", "NAVY", "ORANGE",
"PINK", "PURPLE", "RED", "SIENNA", "TAN", "TEAL", "WHITE", "YELLOW"});
JTextArea TAText = new JTextArea(5, 25);
JButton BApply = new JButton("Apply");
JButton BExit = new JButton("Exit");
public Project() {
JPanel SelectionPanel = new JPanel(new BorderLayout());
SelectionPanel.add(CBColor, BorderLayout.NORTH);
SelectionPanel.add(TAText, BorderLayout.CENTER);
JPanel ApplyPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
ApplyPanel.add(BApply);
ApplyPanel.add(BExit);
add(SelectionPanel, BorderLayout.NORTH);
add(ApplyPanel, BorderLayout.SOUTH);
BApply.addActionListener(new ButtonListener());
BExit.addActionListener(new ExitButtonListener());
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String BGColor = "";
if (CBColor.getSelectedItem() == "AQUA")
BGColor = "#00FFFF";
else if (CBColor.getSelectedItem() == "BLACK")
BGColor = "#000000";
else if (CBColor.getSelectedItem() == "BLUE")
BGColor = "#0000FF";
else if (CBColor.getSelectedItem() == "BROWN")
BGColor = "#A52A2A";
else if (CBColor.getSelectedItem() == "FUCHSIA")
BGColor = "#FF00FF";
else if (CBColor.getSelectedItem() == "GRAY")
BGColor = "#BEBEBE";
else if (CBColor.getSelectedItem() == "GREEN")
BGColor = "#008000";
else if (CBColor.getSelectedItem() == "INDIGO")
BGColor = "#4B0082";
else if (CBColor.getSelectedItem() == "LIME")
BGColor = "#00FF00";
else if (CBColor.getSelectedItem() == "MAROON")
BGColor = "#800000";
else if (CBColor.getSelectedItem() == "NAVY")
BGColor = "#000080";
else if (CBColor.getSelectedItem() == "ORANGE")
BGColor = "#FFA500";
else if (CBColor.getSelectedItem() == "PINK")
BGColor = "#FFC0CB";
else if (CBColor.getSelectedItem() == "PURPLE")
BGColor = "#800080";
else if (CBColor.getSelectedItem() == "RED")
BGColor = "#FF0000";
else if (CBColor.getSelectedItem() == "SIENNA")
BGColor = "#A0522D";
else if (CBColor.getSelectedItem() == "TAN")
BGColor = "#D2B48C";
else if (CBColor.getSelectedItem() == "TEAL")
BGColor = "#008080";
else if (CBColor.getSelectedItem() == "WHITE")
BGColor = "#FFFFFF";
else if (CBColor.getSelectedItem() == "YELLOW")
BGColor = "#FFFF00";
TAText.setBackground(Color.decode(BGColor));
}
}
private class ExitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String[] args) throws Exception {
Map<String, String> ColorsHex = new TreeMap<String, String>();
BufferedReader input = new BufferedReader(new FileReader("colors.txt"));
String line = "";
while ((line = input.readLine()) != null) {
String parts[] = line.split(", ");
ColorsHex.put(parts[0], parts[1]);
}
input.close();
System.out.println(ColorsHex);
System.out.print(ColorsHex.keySet());
System.out.print(ColorsHex.get("RED"));
Project frame = new Project();
frame.setTitle("Colors");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
This compiles and runs, but i am trying to get the combo box to read the values imported instead of entering them, and the listener to read the elements instead of entering them.
JComboBoxdoes not have a constructor that takes aSet. You will have to use one of the available constructors.Update:
One solution is to use the default constructor
JComboBox()initially, call the load to load the colors from your file, and then set the model for theJComboBox:Also, if you make your
TreeMapa class member variable, you will be able to simplify your hex color selection down to 2 lines: