swing - Java synth - How to apply solid color to button background -
i trying obtain simple flat button using synth xml in java. button should contain solid color background , text no other effects.
i have checked tutorials , implemented below solution need provide solid color background image 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 per synth documentation here should able provide background color button instead of using image. have tried below xml settings same. not applying background buttons. whereas applying provided color text.
<state> <font name="verdana" size="14"/> <color value="#ff0000" type="background"/> <color value="#000000" type="text_foreground"/> </state>
can check , me find out mistake have done or there other solution this?
i guess need use <opaque value="true" />
paint 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); }); } }
Comments
Post a Comment