0

I would like to match color search results text with the checkbox text after clicking search button. See pic.

Currently I can view the Search Results text color but it doesn't match the checkbox text color after clicking search button. The code below is only for the Search Car Results text area and a class named CarBrand that matches the key of this HashMap carDetails. I am not sure how to compare and match its color using HashMap. Any suggestions would be great!

import java.awt.Color;
import java.swing.tree.DefaultTreeCellRenderer

public final class CarDetails extends DefaultTreeCellRenderer
{
    private final Color defaultColor;
    private final HashMap<String, Color> carDetails = new HashMap<>();

    public CarDetails()
    {
        int i = 0;
        defaultColor = getBackground(); //default color
        int [][] rgb = {
            { 200, 000, 200 },
            { 000, 140, 000 },
            { 000, 200, 200 }

        };
        for (CarBrand car: CarModel.getCarBrandDetails()) {
            carDetails.put(car.getCarBrand(), new Color(
                rgb[i][0], rgb[i][1], rgb[i][2]));
            i++;
            // TODO this part is what I am not sure.
            if (carDetails.containsKey(car.getCarBrand()) && carTable != null) {
                for (Component c : carTable.getComponents()) {
                    if (c.getName().equals(car.getCarBrand())) {
                       c.setForeground(carDetails.containsObject(new 
                              Color(rgb[i][0], rgb[i][1], rgb[i][2])));
                    }
                }
            }
        }
    }
}

I expect the output to match the color coding of the Search Car Results with the CheckBox text (Honda, Hundai, BMW) like the pic below.

car result output

user2537246
  • 143
  • 2
  • 10

1 Answers1

0

I agree with Andrew Thompson a lot of code is missing to be able to reproduce your problem.

You should check if your conditions are correct (if statements). The declaration of carTable is missing so we can't verify how it is build up. You use Component.getName(), are you sure this contains the value you expect ? see what-is-java-awt-component-getname-and-setname-used-for It is not filled by default so if you fill it before with the expected values, than it's fine to use.

Assuming c.getName() returns the name of the carBrand, you can do the following:

    for (CarBrand car: CarModel.getCarBrandDetails()) {
        carDetails.put(car.getCarBrand(), new Color(
            rgb[i][0], rgb[i][1], rgb[i][2]));
        i++;
    }
    if (carTable != null) {
        for (Component c : carTable.getComponents()) {
            if(carDetails.containsKey(c.getName()) {
                c.setForeground(carDetails.get(c.getName())));
            }
        }
    }

In your code you are not using the values from carDetails map you filled before. By calling ´carDetails.get()` method you reuse the created Color object.

Conffusion
  • 4,335
  • 2
  • 16
  • 28
  • carTable is just a JTable and only wanted some suggestion on how to set those JTable content textarea which are in that checkbok to match that hasmap colors. Adding the entire program will be too much since I only asking for help about that hashmap. – user2537246 May 27 '19 at 12:58
  • Thank you so much Conffusion! Thats all I needed! I am playing around with the gui now and will see how it works, but your input helps a lot! – user2537246 May 28 '19 at 02:00