• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# [java] change the look of a JOptionPane?

## Recommended Posts

i''d like to customize the look of my JOptionPane. if i can get the components it''s made up of (like a JButton, a JLabel and a JPanel) i can easily change the look of them manually with setBackground, setForeground, setBorder, etc. any ideas? the code below simply displays a standard boring gray JOptionPane, as in the setXXXX are ignored.
if (optionsScreen.nameField.getText().trim().length() == 0) {
JOptionPane message = new JOptionPane();
message.setBackground(Color.BLACK);
message.setForeground(Color.WHITE);
message.setBorder(BorderFactory.createLineBorder(Color.WHITE));
}

_______________________ http://mill.3dfxsweden.net

##### Share on other sites
I think I have managed to throw in my own components in a JOptionPane once, but I have never tried to change the colors of the current components. Here are a few solutions that might work:

1) The showMessageDialog method takes a Component object as its first argument. Set the background and foreground colors of that component to the colors that you want the pane to have, since that is probably where the pane gets them from.

2) Cycle through each of the pane''s components using the getComponent method (that the pane has because it extends the java.awt.Container class) and set the colors to each component.

##### Share on other sites
i did a quick test and i couldn''t get it to work.

1. tried that, no diff. i also tried creating a dummy oject and then set the colours, but still no diff.

2. it seems that when showMessageDialog() is called it attaches the Components to the parent, so calling getComponent() before it''s shown is (probably) pointless. when the dialog is shown all other threads are paused so i can''t do anything when the dialog is displayed (as in get the components and change the colour).
calling getComponentCount() returns 2 (both of them are JPanels)

i know i could write my own PLAF, but i really just need this JOptionPane changed so it seems a bit overkill. also, i''m lazy

thanks for the suggestions though. i''ll experiment some more (might have missed something basic)

_______________________
http://mill.3dfxsweden.net

##### Share on other sites
You''re problem is the call to showMessageDialog. That''s a static method, that completely ignores the instance variables you''ve been setting. Take a look at the javadoc for how to use the instance directly.

I don''t know if this''ll solve your problem... But at least you''ll have a shot at it.

##### Share on other sites
quote:
it seems that when showMessageDialog() is called it attaches the Components to the parent, so calling getComponent() before it''s shown is (probably) pointless.

Have you tried calling the JOptionPane.createDialog method to get the dialog? That should allow you to display the dialog yourself using the setVisible method, instead of calling the showMessageDialog on the JOptionPane.

##### Share on other sites
ah! i missed the static statement!

i also tried the createDialog with the same results. i can get the components but setBackground etc are ignored just as before.

doing a getBackground will indeed return black, but it''s just not drawn in black. crap.

more experimenting!

_______________________
http://mill.3dfxsweden.net

##### Share on other sites
I think you have to create your own look and feel to overide the colouring. With basic components, you can just subclass them and change the values that way. I don''t think subclassing would work in this case though.

---
Make it work.
Make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

##### Share on other sites
yah i guess.. i got sick of not getting it to work so i use a simple g.drawString() instead

works almost as good and is a LOT easier hehe. thanks for your comments though.

_______________________
http://mill.3dfxsweden.net

##### Share on other sites
could always do something basically like this:
class MyJOptionPane extends JFrame{    public MyJOptionPane( String title, String message, JButton[] buttons ){        super( title );        getContentPane().setLayoutManager( new BorderLayout() );        getContentPane().add( new JLabel( message ), BorderLayout.NORTH );        JPanel jp = new JPanel();        for( int i = 0; i < buttons.length; ++i )            jp.add( buttons );        getContentPane().add( jp, BorderLayout.SOUTH );        this.pack();        this.show();    }}

doing this off the top of my head so i''m not sure that there aren''t typos but making your own version of JOptionPane type dialogs when the prefab ones aren''t sufficient isn''t that hard. you can obviously change the constructor to do whatever you want if you want the colors and everything. Just be sure to add the actionListeners to the buttons in the case of the above. This approach is much more flexible to change later too.
Can even center the dialog using Toolkit.

• ### Forum Statistics

• Total Topics
627719
• Total Posts
2978797

• 10
• 9
• 21
• 14
• 12