Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I was wondering if anybody knew if it was possible to change the background color on the buttons inside a JOptionPane. I know how to change the entire JOptionPane background using a UIManager, but know what I want is to set the individual okButton, cancelButton, and so on within the JOptionPane to separate individual colors. If I can do this, how would I do this?

Thanks for the help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
250 views
Welcome To Ask or Share your Answers For Others

1 Answer

There is no direct way to do this.

But if you really want to give it a try, then you will need to read the JOptionPane API which gives code that shows you how to manually create and display a JOptionPane without using the showXXX methods.

Using this approach you now have access to the actuall JDialog. Then you can use Darryl's SwingUtils to access the individual buttons and then set the background.

The code would be something like:

JButton ok = SwingUtils.getDescendantOfType(JButton.class, dialog, "Text", "Ok");
ok.setBackground(...);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...