javacolorshexjcolorchooser

How to set a JColorChooser's Color with a String?


I'm making a program that reads a HEX code from a String. Then, it displays the color that it finds with the HEX code using a JColorChooser, but I can't seem to figure out how to do this. How would I go about doing this? If I can't, then what is the second best solution?

PS: If this helps, here is some of the code:

    JColorChooser colorChooser = new JColorChooser();
    textField = new JTextField();

    JButton btnOk = new JButton("OK");
    btnOk.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String str = textField.getText();
            colorChooser.setColor(str);
        }
    }); 

Solution

  • Convert the string to int using (for example):

    int color = Integer.parseInt(testField.getText(),16); //decode hex string