Setting the background color of the text?

How to set background color of background in JOptionPane ?

Picture

enter image description here

 UIManager UI = new UIManager(); UI.put("OptionPane.background", Color.white); UIManager.put("Button.background", Color.white); UI.put("Panel.background", Color.white); UI.put("OptionPane.foreground", Color.white); UI.put("OptionPane.messagebackground", Color.white); UI.put("OptionPane.textbackground", Color.white); UI.put("OptionPane.warningDialog.titlePane.shadow", Color.white); UI.put("OptionPane.warningDialog.border.background", Color.white); UI.put("OptionPane.warningDialog.titlePane.background", Color.white); UI.put("OptionPane.warningDialog.titlePane.foreground", Color.white); UI.put("OptionPane.questionDialog.border.background", Color.white); UI.put("OptionPane.questionDialog.titlePane.background", Color.white); UI.put("OptionPane.questionDialog.titlePane.foreground", Color.white); UI.put("OptionPane.questionDialog.titlePane.shadow", Color.white); UI.put("OptionPane.messageForeground", Color.white); UI.put("OptionPane.foreground", Color.white); UI.put("OptionPane.errorDialog.border.background", Color.white); UI.put("OptionPane.errorDialog.titlePane.background", Color.white); UI.put("OptionPane.errorDialog.titlePane.foreground", Color.white); UI.put("OptionPane.errorDialog.titlePane.shadow", Color.white); JOptionPane.showMessageDialog(null, "Hello world", "HELLO WORLD", JOptionPane.INFORMATION_MESSAGE); 
+7
java swing look-and-feel jtextfield nimbus
source share
3 answers

Why don't you just add a custom JPanel in this place, as shown below:

 JOptionPane.showMessageDialog(frame, getLabelPanel(), "Hello World!", JOptionPane.INFORMATION_MESSAGE); 

You can get JPanel from a method, for example:

 private JPanel getLabelPanel() { JPanel panel = new JPanel(); panel.setOpaque(true); panel.setBackground(Color.BLUE); JLabel helloLabel = new JLabel("Hello World!", JLabel.CENTER); helloLabel.setForeground(Color.WHITE); panel.add(helloLabel); return panel; } 

OUTPUT:

PANEIMAGE


UPDATE 1:

Otherwise, you can try this to change everything,

 uimanager.put("OptionPane.background", Color.BLUE); uimanager.put("OptionPane.messagebackground", Color.BLUE); uimanager.put("Panel.background", Color.BLUE); 

UPDATED EXIT:

OPTIONPANEIMAGE

+9
source share

to try

 UIManager UI=new UIManager(); UI.put("OptionPane.background",new ColorUIResource(255,255,0)); UI.put("Panel.background",new ColorUIResource(255,255,0)); 
+2
source share

The cleanest one will use JDialog to create your own JOptionPane replacement, for example, the proposed nIcE cOw. JOptionPane packs a lot of garbage into its frame, and some of the components do not check JOptionPane specific properties at all.

If you still insist on using JOptionPane and want to change the background only for them, and not for the entire application, I would say that if you want to set the background for the entire dialog with all its subcomponents, it is best to setBackground() over the entire contents of the window and call setBackground() for everyone. This can be done in your user interface class or separately for dialogs obtained using JOptionPane.createDialog() . More or less like this:

 void colorComponentTree(Component component, Color color) { if (component instanceof Container) { if (component instanceof JComponent) { ((JComponent) component).setBackground(color); } for (Component child : ((Container) component).getComponents()) { colorComponentTree(child, color); } } } 

This, however, is very ugly, and I seriously recommend using a user dialog.

+2
source share

All Articles