I am making a GUI were I have to make buttons and the background changes. The program is using a Java Collection Framework were I went with a Hashmap. I am really having trouble understanding and trying to do is access a txt file then read the pairs and store them in the hashmap. the txt file contains the pairs color - hexadecimal value on separate lines which will sort the pairs in the increasing order of their hexadecimal values and use iterators to display the sorted pairs to the console. I tried a BufferReader but got errors but I am guessing that is no the best way to use for a map.
//Input File.txt
Red FF0000
Blue 000084
Green 00FF00
Yellow FFFF00
Orange FF8C00
Pink FFC0CB
Grey D3D3D3
Brown 964B00
Purple 800080
Black 000000
Dark green 013220
Dark Red 8B0000
Dark blue 00008B
Dark Orange D97700
Dark grey 363737
Dark Purple 471E8A
Dark yellow 7f7f00
Light Yellow FFFFCC
Light Blue C0D9D9
Light Purple D8BFD8
Here is the code to get that I already started and finally got working for the GUI.
public class FP extends JFrame implements ActionListener {
BufferedReader reader = new BufferedReader(new FileReader(new File("Input File.txt")));
private Map<String, String> buttonColors;
// Constructor
public FP() {
super("ColorMap");
buttonColors = new HashMap<String, String>();
//test button
buttonColors.put("Red", "FF0000");
setSize(400, 400);
setLayout(new FlowLayout());
ButtonGroup buttonGroup = new ButtonGroup();
for (Map.Entry<String, String> coloringButtons : buttonColors
.entrySet()) {
JRadioButton button = new JRadioButton(coloringButtons.getKey());
button.setActionCommand(coloringButtons.getValue());
button.addActionListener(this);
// Add this new color-button to the button group
buttonGroup.add(button);
add(button);
}
}
@Override
public void actionPerformed(ActionEvent e) {
String color = e.getActionCommand();
getContentPane().setBackground(new Color(Integer.parseInt(color, 16)));
}
public static void main(String[] args) {
FP obj = new FP();
obj.setVisible(true);
}
}
here is your code modified to read the file and populate a HashMap, in this case I am using a TreeMap to sort the hex fields in order, see if this works for you,
public class FP extends JFrame implements ActionListener {
private TreeMap<String, String> buttonColors;
// Constructor
public FP() {
super("ColorMap");
buttonColors = new TreeMap<String, String>();
BufferedReader br = new BufferedReader(new FileReader("InputFile.txt");
while(true)
{
String str = br.readLine();
if(str==null)break;
if(str.length()==0)continue;
string[] st = str.split(" ");
string colorName = st[0].trim();
string colorValue = st[1].trim();
//* to have the colors sorted by the hex value
buttonColors.put(colorValue, colorName);
}
br.close();
//test button
//buttonColors.put("Red", "FF0000");
setSize(400, 400);
setLayout(new FlowLayout());
ButtonGroup buttonGroup = new ButtonGroup();
for (Map.Entry<String, String> coloringButtons : buttonColors
.entrySet()) {
JRadioButton button = new JRadioButton(coloringButtons.getValue());
button.setActionCommand(coloringButtons.getKey());
button.addActionListener(this);
// Add this new color-button to the button group
buttonGroup.add(button);
add(button);
}
}