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

  • Home
  • SEARCH
  • 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 6051127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:46:02+00:00 2026-05-23T07:46:02+00:00

I’m writing a Java Swing program that needs to enable the user to select

  • 0

I’m writing a Java Swing program that needs to enable the user to select 8 different colors from a Settings form. This is quite straightforward as far as it goes with JColorChooser, but I’m wondering is there a recommended way to lay out the form. This is what I’m currently contemplating:

  1. For each color,

  2. Display a square in the current selected color,

  3. Display to the right of that, a button that will bring up the JColorChooser to change the selected color.

However, I suspect the 8 buttons will make the form look clunky if not downright ugly (particularly if more get added later). An alternative approach might be to make the square double as the button; in that case, how to visually indicate it’s clickable?

Is there an alternative recommended method?

  • 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-23T07:46:03+00:00Added an answer on May 23, 2026 at 7:46 am

    Here is a ColorButton that I use in the pscode API.

    package org.pscode.ui.widget;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.*;
    import javax.swing.*;
    
    /** Provides a button for popping a color chooser.
    The Color can be obtained by calling ColorButton.getBackground().
    @author Andrew Thompson. */
    public class ColorButton extends JButton implements ActionListener {
    
        private JColorChooser chooseColor;
        private String htmlColor;
    
        private String text;
    
        public ColorButton(String label) {
            super(label);
            text = label;
            chooseColor = new JColorChooser();
            addActionListener( this );
        }
    
        @Override
        public void setText(String text) {
            super.setText(
                "<html><body style='text-align: left;'><center>" +
                "<p>" +
                text +
                "<br>" +
                "<span style='width: 5px; height: 5px; " +
                "border: solid 1px black; background-color: " +
                htmlColor +
                "; color: " +
                htmlColor +
                ";'>AA</span>  " +
                "</center></body></html>");
            this.text = text;
        }
    
        public void actionPerformed(ActionEvent ae) {
            Color c = chooseColor.showDialog(
                this,
                "Choose Color",
                getBackground() );
            if (c!=null) {
                setBackground(c);
            }
        }
    
        @Override
        public void setBackground(Color bg) {
            super.setBackground( new Color(bg.getRed(), bg.getGreen(), bg.getBlue()) );
            int r = bg.getRed();
            int g = bg.getGreen();
            int b = bg.getBlue();
    
            htmlColor = "rgb(" + r + "," + g + "," + b + ")";
            setText(text);
        }
    }
    

    Screenshot

    Screenshot of typical usage.

    Color scheme chooser using ColorButtons

    In Other News..

    @rwallace: how did you get them to be all the same shape (regardless of length of caption)..

    That part of the layout (for the 5 buttons and check box) is using a GridLayout. You might also alter the size of the buttons by:

    1. Setting the preferred size (dangerous to presume we can second guess it).
    2. Adding some padding in the style for the body element of the HTML.

    “..and with the slightly rounded corners and shiny effect?”

    DukeBox (from which that screenshot was obtained) uses the native PLAF – I’m running Windows 7.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I have a bunch of posts stored in text files formatted in yaml/textile (from
I need to clean up various Word 'smart' characters in user input, including but
I am writing an app with both english and french support. The app requests

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.