java - JOptionPane no button text with custom LNF -
i tried styling custom-made dialog
allows user create new folder. until loaded custom , feel, looked fine:
now tried setting , feel , texts of buttons disappeared.
i set sscce demonstrates behaviour. please note lnf class nothing except loading values uimanager.getdefaults()
. if helps, i'm using debian 8 (jessie) gnome 3.18.1 , global dark theme toggled on.
why happen , how can fix disappearing of button texts?
another common way use uidefaults#addresourcebundle(...) method:
//@see javax/swing/plaf/basic/basiclookandfeel.java @override public uidefaults getdefaults() { uidefaults defaults = new uidefaults(); defaults.putall(uimanager.getdefaults()); //defaults.addresourcebundle("com.example.swing.plaf.light.resources.light"); defaults.addresourcebundle("com.sun.swing.internal.plaf.basic.resources.basic"); return defaults; }
test2.java
import java.awt.*; import java.io.file; import java.util.*; import javax.swing.*; import javax.swing.grouplayout.alignment; import javax.swing.grouplayout.sequentialgroup; public class test2 { public static void main(string[] args) { try { uimanager.setlookandfeel(new lightlookandfeel()); dialogfactory.showcreatefolderdialog(null, new file(".")); } catch (unsupportedlookandfeelexception e) { e.printstacktrace(); } } } class dialogfactory { private dialogfactory() {} public static void showcreatefolderdialog(component component, file parent) { jpanel panel = new jpanel(); jlabel message = new jlabel("please enter folder name."); jlabel label = new jlabel("folder name:"); final jlabel errormessage = new jlabel(); jtextfield foldername = new jtextfield(); grouplayout layout = new grouplayout(panel); layout.sethonorsvisibility(false); layout.setautocreatecontainergaps(true); layout.setautocreategaps(true); panel.setlayout(layout); errormessage.setvisible(false); sequentialgroup hgroup = layout.createsequentialgroup(); sequentialgroup input = layout.createsequentialgroup().addcomponent(label).addcomponent(foldername); hgroup.addgroup(layout.createparallelgroup().addcomponent(message).addgroup(input).addcomponent(errormessage)); layout.sethorizontalgroup(hgroup); sequentialgroup vgroup = layout.createsequentialgroup(); vgroup.addgroup(layout.createparallelgroup(alignment.baseline).addcomponent(message)); vgroup.addgroup(layout.createparallelgroup(alignment.baseline).addcomponent(label).addcomponent(foldername)); vgroup.addgroup(layout.createparallelgroup(alignment.baseline).addcomponent(errormessage)); layout.setverticalgroup(vgroup); final joptionpane optionpane = new joptionpane(panel, joptionpane.plain_message, joptionpane.ok_cancel_option); final jdialog dialog = new jdialog(joptionpane.getframeforcomponent(component), "create new folder", true); dialog.setlocationrelativeto(component); dialog.setcontentpane(optionpane); dialog.setdefaultcloseoperation(windowconstants.do_nothing_on_close); optionpane.addpropertychangelistener(event -> { string property = event.getpropertyname(); if (dialog.isvisible() && event.getsource() == optionpane && property.equals(joptionpane.value_property)) { if (foldername.gettext().isempty() && (int) event.getnewvalue() != joptionpane.cancel_option) { errormessage.settext("<html><body><b style='color: red'>please enter valid folder name!</b></body></html>"); errormessage.setvisible(true); dialog.pack(); } else { dialog.setvisible(false); } } }); dialog.pack(); dialog.setvisible(true); int response = ((integer) optionpane.getvalue()).intvalue(); if (response != joptionpane.ok_option) return; file newfolder = new file(parent, foldername.gettext()); newfolder.mkdirs(); } } class lightlookandfeel extends lookandfeel { private final uidefaults defaults = new uidefaults(); public lightlookandfeel() { defaults.putall(uimanager.getdefaults()); } @override public void initialize() { } @override public string getname() { return "light"; } @override public string getid() { return getclass().getname(); } @override public string getdescription() { return "light , feel"; } @override public boolean isnativelookandfeel() { return false; } @override public boolean issupportedlookandfeel() { return true; } @override public uidefaults getdefaults() { //defaults.addresourcebundle("com.example.swing.plaf.light.resources.light"); defaults.addresourcebundle("com.sun.swing.internal.plaf.basic.resources.basic"); return defaults; } } // package com.example.swing.plaf.light.resources; // import java.util.*; // // public class light extends listresourcebundle { // @override protected final object[][] getcontents() { // return new object[][] { // //... // { "optionpane.cancelbuttonmnemonic", "0" }, // { "optionpane.cancelbuttontext", "cancel" }, // { "optionpane.inputdialogtitle", "input" }, // { "optionpane.messagedialogtitle", "message" }, // { "optionpane.nobuttonmnemonic", "78" }, // { "optionpane.nobuttontext", "no" }, // { "optionpane.okbuttonmnemonic", "0" }, // { "optionpane.okbuttontext", "okkkk" }, // { "optionpane.titletext", "select option" }, // { "optionpane.yesbuttonmnemonic", "89" }, // { "optionpane.yesbuttontext", "yessss" }, // //... // }; // } // } // public class light_de extends listresourcebundle { //... // public class light_es extends listresourcebundle { //... // public class light_fr extends listresourcebundle { //... // public class light_it extends listresourcebundle { //... // public class light_ja extends listresourcebundle { //... // public class light_ko extends listresourcebundle { //... // public class light_pt_br extends listresourcebundle { //... // public class light_sv extends listresourcebundle { //... // public class light_zh_cn extends listresourcebundle { //... // public class light_zh_hk extends listresourcebundle { //... // public class light_zh_tw extends listresourcebundle { //...
Comments
Post a Comment