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 7007363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:35:52+00:00 2026-05-27T21:35:52+00:00

I have a problem here – I have a hexadecimal value being stored in

  • 0

I have a problem here – I have a hexadecimal value being stored in a textfield after I have selected a color (using JColorChooser). What I would like to do is display the name of the color in another textfield right beside the one with the hexadecimal value, but I am unsure as to how to get the color name? I am including my code, maybe someone can give me some useful hints:

public class Main extends JComponent implements Accessible {
    public ColorSelectionModel selectionModel;
    public static final String SELECTION_MODEL_PROPERTY = "selectionModel";
    public JColorChooser chooser;
    public Color color;

    public void process() {
        JFrame frame;
        JButton button;
        final JTextField text1, text2;

        // Initialize variables
        chooser = new JColorChooser();
        frame = new JFrame();
        JPanel panel = new JPanel();
        button = new JButton("Show color Palette");
        text1 = new JTextField(20);
        text2 = new JTextField(20);

        // Setup UI
        frame.add(panel);
        panel.add(button);
        panel.add(text1);
        panel.add(text2);
        panel.add(chooser)
        chooser.setVisible(false);

        button.setLocation(800, 600);
        button.setActionCommand("");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                color = chooser.showDialog(chooser, "SHOW THE COLOR",
                        chooser.getColor());
                {
                    if (color != null) {
                        String hex = Integer.toHexString(color.getRGB() & 0xffffff);
                        hex = "#" + hex;
                        text1.setText(hex);
                    }
                }
            }
        });

        frame.setVisible(true);
        frame.setSize(1000, 800);
    }

    public static void main(String[] argv) {
        // Run the code
        Main m1 = new Main();
        m1.process();
    }
}
  • 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-05-27T21:35:52+00:00Added an answer on May 27, 2026 at 9:35 pm

    I achieved this by Java Reflection : (works for static final Color defined in java.awt.Color)

    Here is my code :

    public static String getNameReflection(Color colorParam) {
            try {
                //first read all fields in array
                Field[] field = Class.forName("java.awt.Color").getDeclaredFields();
                for (Field f : field) {
                    String colorName = f.getName();
                    Class<?> t = f.getType();
                    // System.out.println(f.getType());
                    // check only for constants - "public static final Color"
                    if (t == java.awt.Color.class) {
                        Color defined = (Color) f.get(null);
                        if (defined.equals(colorParam)) {
                            System.out.println(colorName);
                            return colorName.toUpperCase();
                        }
                    }
                }
            } catch (Exception e) {
                System.out.println("Error... " + e.toString());
            }
            return "NO_MATCH";
        }
    

    Source : http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-reflection-getting-name-of-color.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Rational ClearCase v. 7.0.1.1 with UCM, I have a problem here when using
i have a big problem here..i am using Microsoft charting controls in my asp.net
I have a problem here... After a button is pressed I want a loop
I have a problem here. I am using YQL & jQuery to get some
I have a problem here if you look at my codes below after I
I have a problem here using .dbf files. I can make CRUD operations but
I have a problem here. My Zend_Forms do not render in view script. Via
I have an interesting problem here I've been trying to solve for the last
I have a bit of an architecture problem here. Say I have two tables,
I have run into a bit of a problem here: I had a problem-specific

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.