简体   繁体   中英

Java Swing with own L&F sometimes suddenly switches back to default L&F

In a Java Swing application that uses several own UI's (eg for JTabbedPane) sometimes right after startup, the Look and Feel (L&F) of the entire application changes back to the default. It quickly shows everything correct and then after a second or less the entire application changes to the ugly default.

Unfortunately, this is difficult to reproduce. It happens very rarely and so far only under Ubuntu when I start it directly from within Eclipse using java-1.6.0-openjdk-amd64. I have a newer java version installed too, but I use 1.6 to test compatibility.

Since it happens either for all or for no component, it looks like it does not matter how II derive the UI. It even changes back this little change where no custom UI is used:

JTextField textField = new JTextField();
textField.setBorder(null);

So if it happens, then the border will show up.

Since this is difficult to reproduce, I can not give a code example where it always happens. Actually, the one above is already an example, but it does not happen always. But I hope to find someone who run into a similar issue where the L&F or any changes to it suddenly and unwanted changed back to the default. If so, I would appreciate if you could share your experience and hopefully a solution.

---------------- Edit: I found two workaraounds:

  1. For the border problem, I simply use textField.setBorder(new EmptyBorder()); instead of setting it to null.
  2. For the reset of the UI, which was indeed caused by unwanted calls to updateUI, I created for my customized swing objects subclasses that override updateUI(), eg:

     public class JTabbedPaneNoHeads extends JTabbedPane { public JTabbedPaneNoHeads() { setUI(new GUITabbedPaneNoHeadsUI()); } @Override public GUITabbedPaneNoHeadsUI getUI() { return (GUITabbedPaneNoHeadsUI) ui; } @Override public void updateUI() { /* This was to find out who is calling updateUI: StackTraceElement[] _stackTrace = Thread.currentThread().getStackTrace(); for (StackTraceElement element : _stackTrace ){ System.out.print(element + " -- "); } System.out.println(); */ setUI(new GUITabbedPaneNoHeadsUI()); } } 

Now everything works fine.

btw: the calls to updateUI had the following stack trace (generated by the commented block in the code above):

livedocket.GUI.design.JTabbedPaneNoHeads.updateUI(JTabbedPaneNoHeads.java:22)
javax.swing.SwingUtilities.updateComponentTreeUI0(SwingUtilities.java:1230)
.... many more of these updateComponentTreeUI0 ...
javax.swing.SwingUtilities.updateComponentTreeUI0(SwingUtilities.java:1245)
javax.swing.SwingUtilities.updateComponentTreeUI(SwingUtilities.java:1221)
javax.swing.plaf.metal.MetalLookAndFeel$AATextListener.updateWindowUI(MetalLookAndFeel.java:2329)
javax.swing.plaf.metal.MetalLookAndFeel$AATextListener.updateAllUIs(MetalLookAndFeel.java:2342)
javax.swing.plaf.metal.MetalLookAndFeel$AATextListener.access$200(MetalLookAndFeel.java:2295)
javax.swing.plaf.metal.MetalLookAndFeel$AATextListener$1.run(MetalLookAndFeel.java:2370)
java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:673)
java.awt.EventQueue.access$300(EventQueue.java:96)
java.awt.EventQueue$2.run(EventQueue.java:634)
java.awt.EventQueue$2.run(EventQueue.java:632)
java.security.AccessController.doPrivileged(Native Method)
java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:108)
java.awt.EventQueue.dispatchEvent(EventQueue.java:643)
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

As I mentioned before, these calls happen only very rarely and only on Java 1.6.

我在出现线程问题的应用程序中看到了类似的症状-如果我是我,那么我将仔细检查是否所有的GUI都是在事件调度线程上构建的。

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