Question
Excercise. Use a HashMap to create a reusable class for choosing one of the 13 predefined colors in class Color. The names of the colors
Excercise.
Use a HashMap to create a reusable class for choosing one of the 13 predefined colors in class Color. The names of the colors will be used as keys, and the predefined Color objects will be used as values. Place this class in a package that can be imported into any Java program. Use your new class in a GUI application that allows the user to select one of the colors from a JList. The JList should receive its values from the HashMap programmatically. When the user clicks a button, the application must change the background color of a JPanel to the currently selected color in the JList.
My codes are working only needs some adjustment.
The color application should have changed color only after clicking on the button, not after clicking on the color in the Jlist. Please assist.
Colorstore.java
package colorstore;
import java.awt.*;
import java.util.HashMap;
/**
*/
public class ColorStore {
private static HashMap
public ColorStore(){
colorHashMap.put("MAGENTA",Color.MAGENTA);
colorHashMap.put("YELLOW",Color.YELLOW);
colorHashMap.put("CYAN",Color.CYAN);
colorHashMap.put("DARK_GRAY",Color.DARK_GRAY);
colorHashMap.put("ORANGE",Color.ORANGE);
colorHashMap.put("GRAY",Color.GRAY);
colorHashMap.put("WHITE",Color.WHITE);
colorHashMap.put("BLUE",Color.BLUE);
colorHashMap.put("GREEN",Color.GREEN);
colorHashMap.put("RED",Color.RED);
colorHashMap.put("PINK",Color.PINK);
colorHashMap.put("LIGHT_GRAY",Color.LIGHT_GRAY);
colorHashMap.put("BLACK",Color.BLACK);
}
private String getName(Color color) {
return color.getClass().getName();
}
public HashMap
return colorHashMap;
}
}
coloDemo
package ColorDemo;
import colorstore.ColorStore;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
*
* @author
*/
public class ColorDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JPanel controlPanel;
public ColorDemo(){
prepareGUI();
}
public static void main(String[] args){
ColorDemo colorDemo = new ColorDemo();
colorDemo.showListDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Select a color to change backgroud");
mainFrame.setSize(600,600);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.setVisible(true);
}
private void showListDemo(){
headerLabel.setText("Select a color to change bg");
final HashMap
final JList jlist = new JList(new Vector(colorMap.keySet()));
jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jlist.setSelectedIndex(0);
jlist.setVisibleRowCount(5);
JScrollPane listScrollPane = new JScrollPane(jlist);
jlist.addListSelectionListener((ListSelectionEvent listSelectionEvent) -> {
String selected = (String)jlist.getSelectedValue();
controlPanel.setBackground(colorMap.get(selected));
});
controlPanel.add(listScrollPane);
mainFrame.setVisible(true);
}
}
Output
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started