Fields JButton. Not complied with when nimbus plaf

The margin of JButton property is not respected if nimbus look and feel is set.
. I need some "small" buttons, but the halo makes the space around the button text be large, so I only get "very large" buttons.
I found in the default nimbus page that there is a property called:

 Button.contentMargins 

which is given with large values.
I tried to override it with the following code:

 UIManager.getDefaults().put("Button.contentMargins", new InsetsUIResource(0,0,0,0)); 

in main , immediately after setting the halo look.

But nothing happens, the empty space around the button text remains large. Any idea?

+3
source share
2 answers

Changing the value of JComponent.sizeVariant can also be effective, as described in Resizing a component .

+3
source

flow-based How do I change the background color for JPanels using Nimbus Look and Feel? you can change and assign one value to something from Nimbus Defaults ,

but you are sure that you need this output for the GUI, nothing nice

enter image description here

vs basic JButton with Nimbus L & F

enter image description here

from code

 import java.awt.*; import javax.swing.*; import javax.swing.plaf.InsetsUIResource; public class NimbusJPanelBackGround { public NimbusJPanelBackGround() { JButton btn = new JButton(" Whatever "); JButton btn1 = new JButton(" Whatever "); JPanel p = new JPanel(); p.add(btn); p.add(btn1); JFrame f = new JFrame(); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.add(p, BorderLayout.CENTER); f.setSize(200, 100); f.setLocation(150, 150); f.setVisible(true); } public static void main(String[] args) { try { for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(laf.getName())) { UIManager.setLookAndFeel(laf.getClassName()); UIManager.getLookAndFeelDefaults().put("Panel.background", Color.white); UIManager.getLookAndFeelDefaults().put("Button.contentMargins", new InsetsUIResource(0,0,0,0)); } } } catch (Exception e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { @Override public void run() { NimbusJPanelBackGround nimbusJPanelBackGround = new NimbusJPanelBackGround(); } }); } } 

earlier +1 for an interesting question

+2
source

Source: https://habr.com/ru/post/902493/


All Articles