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));
message.showMessageDialog(this, "Please enter your name.", "No name?", JOptionPane.PLAIN_MESSAGE);
}
_______________________
http://mill.3dfxsweden.net
[java] change the look of a JOptionPane?
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.
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.
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.
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
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
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.
I don''t know if this''ll solve your problem... But at least you''ll have a shot at it.
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.
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
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
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
---
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
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
works almost as good and is a LOT easier hehe. thanks for your comments though.
_______________________
http://mill.3dfxsweden.net
could always do something basically like this:
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement