lundi 27 novembre 2017

Icon Radio and Checkbox not showing in custom Look and Feel

I'm creating a custom Look and Feel for a company as my final trainee ship for graduation. I've been stuck on this part for a few days now and me and my colleagues can't seem to fix it.

I'm trying to create a custom radio button an checkbox for the Look and Feel. Now when I want to display the stock ones to the screen / JFrame it won't display.

Once I activate my Look and Feel the checkboxes and radio buttons dissapear. But the label of them does display on screen. When I create the checkbox / radio outside the look and feel or give a nimbus look and feel to them, it does display. So somehow my Look and Feel is breaking things. Can anyone help?

This is my code for the radio button;

public class MyRadioBut extends BasicRadioButtonUI {


    public MyRadioBut()
    {
        super();
    }

    public static ComponentUI createUI(JComponent c) {
        return new MyRadioBut();
    }

    @Override
    public void installUI(final JComponent c) {
        super.installUI(c);
    }

    public static MyRadioBut createRadio()
    { 
        MyRadioBut radio = new MyRadioBut();

        return radio;
    }
    private boolean defaults_initialized = false;
    @Override
    public void installDefaults(AbstractButton b) {  
        super.installDefaults(b);

    }
}

Code for checkbox is the same.

Look and feel code;

package iac.lookandfeel;

import java.awt.Font;

import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicLookAndFeel;


public class MyLookAndFeel extends BasicLookAndFeel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    static MyButton btn = new MyButton();
    static MyProgressBar pb = new MyProgressBar();
    static MyLabel lbl = new MyLabel();
    static MyTab tab = new MyTab();
    static MyRadioBut radio = new MyRadioBut();
    static MyComboBox combo = new MyComboBox();

    public MyLookAndFeel() {
        super();

    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return "IAC Look and Feel";
    }

    @Override
    public String getID() {
        // TODO Auto-generated method stub
        return "IAC Look and Feel";
    }

    @Override
    public String getDescription() {
        // TODO Auto-generated method stub
        return "IAC's Look And Feel";
    }

    @Override
    public boolean isNativeLookAndFeel() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isSupportedLookAndFeel() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean getSupportsWindowDecorations()
    {
        return true;
    }
    public static void setAllFonts() {
        Font defaultFont = UIManager.getDefaults().getFont("Button.font");
        int defaultSize = defaultFont.getSize(); 
        Font font = new Font("Serif", Font.PLAIN, 50); 

        UIManager.put("Button.font", font);

        UIManager.put("RadioButton.font", font);
        UIManager.put("CheckBox.font", font);

        UIManager.put("ComboBox.font", font);
        UIManager.put("Label.font", font);
        UIManager.put("TabbedPane.font", font);
        UIManager.put("TextField.font", font);
        UIManager.put("PasswordField.font", font);
        UIManager.put("TextArea.font", font);
        UIManager.put("ProgressBar.font", font);

    }

    @Override
    protected void initClassDefaults(UIDefaults table)
    { 
        setAllFonts();
        MyButton.createButton(); 
        MyProgressBar.createBar();
        MyLabel.createLabel();
        MyTab.createTab();
        MyRadioBut.createRadio();
        MyCheckBox.createCheckBox();
        MyComboBox.createCombo();


        super.initClassDefaults(table);
        String IACPackage = "iac.lookandfeel.";
        final String basicPackageName = "javax.swing.plaf.basic.";
        Object[] uiDefaults = {
                "ButtonUI", IACPackage + "MyButton",
                "CheckBoxUI", IACPackage + "MyCheckBox",
                "ColorChooserUI", basicPackageName + "BasicColorChooserUI",
                "FormattedTextFieldUI", basicPackageName + "BasicFormattedTextFieldUI",
                "MenuBarUI", basicPackageName + "BasicMenuBarUI",
                "MenuUI", basicPackageName + "BasicMenuUI",
                "MenuItemUI", basicPackageName + "BasicMenuItemUI",
                "CheckBoxMenuItemUI", basicPackageName + "BasicCheckBoxMenuItemUI",
                "RadioButtonMenuItemUI", basicPackageName + "BasicRadioButtonMenuItemUI",
                "RadioButtonUI", IACPackage + "MyRadioBut",
                "ToggleButtonUI", basicPackageName + "BasicToggleButtonUI",
                "PopupMenuUI", basicPackageName + "BasicPopupMenuUI",
                "ProgressBarUI", IACPackage + "MyProgressBar",
                "ScrollBarUI", basicPackageName + "BasicScrollBarUI",
                "ScrollPaneUI", basicPackageName + "BasicScrollPaneUI",
                "SplitPaneUI", basicPackageName + "BasicSplitPaneUI",
                "SliderUI", basicPackageName + "BasicSliderUI",
                "SeparatorUI", basicPackageName + "BasicSeparatorUI",
                "SpinnerUI", basicPackageName + "BasicSpinnerUI",
                "ToolBarSeparatorUI", basicPackageName + "BasicToolBarSeparatorUI",
                "PopupMenuSeparatorUI", basicPackageName + "BasicPopupMenuSeparatorUI",
                "TabbedPaneUI", IACPackage + "MyTab",
                "TextAreaUI", basicPackageName + "BasicTextAreaUI",
                "TextFieldUI", basicPackageName + "BasicTextFieldUI",
                "PasswordFieldUI", basicPackageName + "BasicPasswordFieldUI",
                "TextPaneUI", basicPackageName + "BasicTextPaneUI",
                "EditorPaneUI", basicPackageName + "BasicEditorPaneUI",
                "TreeUI", basicPackageName + "BasicTreeUI",
                "LabelUI", IACPackage + "MyLabel",
                "ListUI", basicPackageName + "BasicListUI",
                "ToolBarUI", basicPackageName + "BasicToolBarUI",
                "ToolTipUI", basicPackageName + "BasicToolTipUI",
                "ComboBoxUI", IACPackage + "MyComboBox",
                "TableUI", basicPackageName + "BasicTableUI",
                "TableHeaderUI", basicPackageName + "BasicTableHeaderUI",
                "InternalFrameUI", basicPackageName + "BasicInternalFrameUI",
                "DesktopPaneUI", basicPackageName + "BasicDesktopPaneUI",
                "DesktopIconUI", basicPackageName + "BasicDesktopIconUI",
                "FileChooserUI", basicPackageName + "BasicFileChooserUI",
                "OptionPaneUI", basicPackageName + "BasicOptionPaneUI",
                "PanelUI", basicPackageName + "BasicPanelUI",
                "ViewportUI", basicPackageName + "BasicViewportUI",
                "RootPaneUI", basicPackageName + "BasicRootPaneUI",
        };
        //   UIManager.put(MyLookAndFeel.UI_CLASS_ID,  MyButton.class.getName());
        table.putDefaults(uiDefaults);
    }
}

IACPackage is my own package which I use in all components.

Hope one of you guys got an idea.

I have a test gui which I test the look and feel on: I create the checkbox and radio like this;

JCheckBox CBox = new JCheckBox("Checkbox");
    JRadioButton Radio = new JRadioButton("Radio"); 

It does display the string but not the icons.




Aucun commentaire:

Enregistrer un commentaire