Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Swing issue - Using color palette
    primarykey
    data
    text
    <p>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 <em>name</em> 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:</p> <pre><code>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() &amp; 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload