简体   繁体   中英

Substance L&F not working

I want to use the Substance L&F library in my Java application, so I downloaded the .jar files and added them to the project classpath. Then I want to set the L&F in the application's main() function like this:

SwingUtilities.invokeAndWait(new Runnable()
{
    @Override
    public void run()
    {
        try
        {
            // Substance
            String skin = "org.pushingpixels.substance.api.skin.SubstanceGraphiteAquaLookAndFeel";
            SubstanceLookAndFeel.setSkin(skin);
            JFrame.setDefaultLookAndFeelDecorated(true);
            JDialog.setDefaultLookAndFeelDecorated(true);
        }
        catch(Exception e)
        {
            System.err.println("Can't initialize specified look&feel");
            e.printStackTrace();
        }
    }
});

That is done before the JFrame is being created. However, even though no exception is thrown, nothing happens, the GUI is rendered in default Swing L&F.

Any ideas what I am missing here?

EDIT
Instead of the SubstanceLookAndFeel.setSkin(skin); call I tried it with UIManager.setLookAndFeel(skin); instead. This still doesn't work, but at least I get an exception now:

org.pushingpixels.substance.api.UiThreadingViolationException:
State tracking must be done on Event Dispatch Thread

Isn't that solved by calling this via invokeAndWait() ?

EDIT-2
Ok, so the problem was something different. The exception was thrown while creating a JTable , not when setting the L&F. I was able to solve the issue - the L&F is now correctly rendered - by calling the JFrame constructor (which then basically runs the whole application) via EventQueue.invokeLater() . But I never did that before, is it "save" (valid in Java terms) to do it that way?

There is a small trick when setting Substance LaF . You have to call UIManager.setLookAndFeel(new SubstanceGraphiteAquaLookAndFeel()); before you call UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceGraphiteAquaLookAndFeel"); . So, set it like this:

public class App {

    public static void main(String [] args) {
        try {

            UIManager.setLookAndFeel(new SubstanceGraphiteAquaLookAndFeel());
            UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceGraphiteAquaLookAndFeel");    

        } catch (ClassNotFoundException | InstantiationException
                | IllegalAccessException | UnsupportedLookAndFeelException e1) {
            e1.printStackTrace();
        }
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                //Your GUI code goes here..
            }
        });

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM