Java and Windows Look & feel
Asked Answered
N

1

8

I made a swing application that scans Images; and each Image represented by a leaf I a tree

the problem that I faced is that it throws this exception

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.sun.java.swing.plaf.windows.XPStyle$Skin.getWidth(XPStyle.java:513)
    at com.sun.java.swing.plaf.windows.XPStyle$Skin.getWidth(XPStyle.java:517)
    at com.sun.java.swing.plaf.windows.WindowsTreeUI$ExpandedIcon.getIconWidth(WindowsTreeUI.java:138)
    at javax.swing.plaf.basic.BasicTreeUI.drawCentered(BasicTreeUI.java:1580)
    at javax.swing.plaf.basic.BasicTreeUI.paintExpandControl(BasicTreeUI.java:1464)
    at javax.swing.plaf.basic.BasicTreeUI.paint(BasicTreeUI.java:1206)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
    at javax.swing.JComponent.paintComponent(JComponent.java:763)
    at javax.swing.JComponent.paint(JComponent.java:1027)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5122)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:285)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1128)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5070)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4880)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:723)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:679)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:659)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

and I don't know why this appears Note: I am using Windows Look and feel

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

and java 1.6_22 under windows-7

Nuncupative answered 1/3, 2011 at 17:15 Comment(9)
Can you post your code please? It's hard to tell what's going on without the code.Knowles
can you post the source code?Autogenous
I do not believe the problem is with the library itself. Do you mind sharing how you are scanning for images in directory. I sense problem with input. ( list of images in dir)Columbian
Actually, I could well believe this is a problem with the Windows PL&F. A minimal example of code that triggers the problem would be good. Possibly it is quite machine specific.Dialectician
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); // Bad, Bad, Bad UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // good, good, good - Your version will not work on anything but Windows, not that Mac and Unix users would want to see Windows PLAF on their desk-top in any case. As to the suggestion to 'post code' - I 'downvote' that. Instead post an SSCCE (pscode.org/sscce.html). Also, my WAG is that the code is doing something off the EDT, that should be done on the EDT.Aeneas
Just use SWT... the Swing implementation on Windows 7 sucks anyway.Weltanschauung
Did you set the look and feel before or after the component's frame was visible? I vaguely remember getting this nullpoiner when changing the look and feel to WindowsLookAndFeel if a frame was already visible.Immunotherapy
@Lucass PLAF can be changed when a JFrame (or JApplet) is already visible. It is just necessary to call SwingUtilities.updateComponentTreeUI(Component) to ensure the tree of components is adjusted.Aeneas
I used UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName() first but still problem found. and also it is the first line in my main()methodNuncupative
T
35

Try this:

//Set the look and feel to users OS LaF.
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }
Teletype answered 15/3, 2011 at 8:33 Comment(5)
"//Set the look and feel to windows." No it does not do that. At least, not on a Mac. or *nix OS.Aeneas
Oh my god I love it. On windows this looks AWESOME.Indiscriminate
Please consider voting the answer up if it helped you.Teletype
Why not catch (Exception e)?Brassard
WChargin: the try catch blocks were probably generated individually by the IDE (eclipse behaves like this).Stylistic

© 2022 - 2024 — McMap. All rights reserved.