Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8678699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:47:21+00:00 2026-06-12T20:47:21+00:00

I am working on a project that requires me to read colors and Hex

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:47:22+00:00Added an answer on June 12, 2026 at 8:47 pm

    JComboBox does not have a constructor that takes a Set. 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 the JComboBox:

    protected void loadColors() throws IOException {
       Map<String, String> colorsHexMap = new TreeMap<String, String>();
       BufferedReader input = new BufferedReader(new FileReader("colors.txt"));
       ...
    
       Set<String> keySet = colorsHexMap.keySet();
       String[] keyArray = keySet.toArray(new String[keySet.size()]);
       ComboBoxModel<String> model = new DefaultComboBoxModel<>(keyArray);
       CBColor.setModel(model);
    }
    

    Also, if you make your TreeMap a class member variable, you will be able to simplify your hex color selection down to 2 lines:

    String bgColor = colorsHexMap.get(CBColor.getSelectedItem());
    TAText.setBackground(Color.decode(bgColor));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a project that requires to convert html email into text. Below
I'm currently working on a python project, that requires file transmission from a client
I have a project I'm working on that requires our WPF application read SMS
I'm working on a project that requires me to programmatically create MySQL users from
Am working on a project that requires uploading xml file to remote FTP site.
I have been working on a project that dynamically creates a javascript file using
I am working on a project that requires appending to a AES/CTR encrypted file.
I'm currently working on a project that requires that I have a div stacked
I am working on a project that requires me to define a DSL. Since
I'm working on a project that requires a fair bit of inserting/updating rows in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.