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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:12:54+00:00 2026-05-20T21:12:54+00:00

I’m trying to pop up a dialog to allow the user to select one

  • 0

I’m trying to pop up a dialog to allow the user to select one of two colors as a background color. To make it look especially spiffy, I’d like to two choices to be displayed in the color in question, i.e.:

import java.awt.Color;
import java.awt.Label;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class JOptionPaneTest extends JFrame{

    public static void main(String[] args) {
        new JOptionPaneTest();
    }

    public JOptionPaneTest() {
        Object[] possibilities = new Object[2];
        JButton black = new JButton("Black");
        JButton white = new JButton("White");
        black.setBackground(Color.black);
        white.setBackground(Color.white);
        black.setForeground(Color.white);
        white.setForeground(Color.black);
        possibilities[0] = black;
        possibilities[1] = white;

        JButton l = (JButton)JOptionPane.showInputDialog(this, 
                "Please specify the background color", "Background check",
                JOptionPane.QUESTION_MESSAGE, null, possibilities,
                possibilities[0]);
        System.out.println("" + l);
    }
}

However, this doesn’t work – it displays the JButton.toString() return values in a drop down instead of the JButton. I also tried JLabel and Label for the heck of it. According to the API, the JButtons should be added to the dialog as is since they’re Components. If I add the JButton to the ‘message’ parameter it does display as expected.

Any idea what I’m doing wrong?

  • 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-20T21:12:55+00:00Added an answer on May 20, 2026 at 9:12 pm

    The Java API is slighly unclear about this point. At the top describes how to interpret the options, but options are the YES, NO, CANCEL… possibilities the user can choose, painted in the buttons row. You are talking about the selectionValues, and then the API (go to last method named showInputDialog) is clear:

    selectionValues – an array of Objects that gives the possible selections
    It is up to the UI to decide how best to represent the selectionValues, but usually a JComboBox, JList, or JTextField will be used.

    From my experience, the objects passed in the selectionValues are displayed using toString()and the result is shown in a JComboBox or a JList, so you can not show selection values with custom painting; you need to implement you own dialog for that.

    You can pass the message as a Component so you can provide a legend to the user about the selectionValues, where you can show labels with background colors to indicate each color available and thus provide assitance selecting a value from selectionValues.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.