javaswingsynthflatui

Java synth - How to apply solid color to button background


I am trying to obtain simple flat button using synth xml in Java. This button should contain solid color background and text with no other effects.

I have checked some tutorials and successfully implemented below solution where I will need to provide the solid color background image for the button.

    <state>
        <imagePainter method="buttonBackground" path="images/button_press.png" sourceInsets="10 10 10 10"/>
        <font name="Dialog" size="16"/>
        <color type="TEXT_FOREGROUND" value="#FFFFFF"/>
    </state>

But as per synth documentation here I should be able to provide the Background color to the button instead of using an image. I have tried below XML settings for the same. But it is not applying any background to buttons. Whereas it is applying provided color to text.

    <state>
        <font name="Verdana" size="14"/>
        <color value="#FF0000" type="BACKGROUND"/>
        <color value="#000000" type="TEXT_FOREGROUND"/>
    </state>

Can anyone check and help me find out the mistake I have done or there is any other solution for this?


Solution

  • I guess you would need to use <opaque value="true" /> to paint the JButton's background:

    button.xml

    <synth>
      <style id="default">
        <font name="Dialog" size="16" /> 
      </style>
      <bind style="default" type="region" key=".*" />
    
      <style id="ButtonTest">
        <opaque value="true" />
        <insets top="10" bottom="10" left="10" right="10" />
        <state>
          <font name="Verdana" size="14" />
          <color type="BACKGROUND" value="#FF0000" />
          <color type="TEXT_FOREGROUND" value="#000000" />
        </state>
        <state value="MOUSE_OVER">
          <color type="BACKGROUND" value="ORANGE" />
          <color type="TEXT_FOREGROUND" value="WHITE" />
        </state>
        <state value="PRESSED">
          <color type="BACKGROUND" value="GREEN" />
          <color type="TEXT_FOREGROUND" value="WHITE" />
        </state>
      </style>
      <bind style="ButtonTest" type="region" key="Button" />
    </synth>
    

    SynthButtonTest.java

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.plaf.synth.*;
    
    public class SynthButtonTest {
      public JComponent makeUI() {
        JPanel p = new JPanel();
        p.add(new JButton("JButton1"));
        p.add(new JButton("JButton2"));
        p.add(new JButton("JButton3"));
        return p;
      }
      public static void main(String... args) {
        EventQueue.invokeLater(() -> {
          try {
            Class<?> c = SynthButtonTest.class;
            SynthLookAndFeel synth = new SynthLookAndFeel();
            synth.load(c.getResourceAsStream("button.xml"), c);
            UIManager.setLookAndFeel(synth);
          } catch (Exception ex) {
            ex.printStackTrace();
          }
          JFrame f = new JFrame();
          f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          f.getContentPane().add(new SynthButtonTest().makeUI());
          f.setSize(320, 240);
          f.setLocationRelativeTo(null);
          f.setVisible(true);
        });
      }
    }