2010-11-18 6 views
6
public class TabbedArea extends JTabbedPane { 
    public void addArea(){ 
    add(component); 
    final JPanel panel = new JPanel(new BorderLayout()); 
    panel.add(new JLabel(title), BorderLayout.LINE_START); 
    JButton closeButton = new JButton(new CloseIcon()); 
    closeButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     removeArea(subAreas.get(indexOfTabComponent(panel))); 
     } 
    }); 
    panel.add(closeButton, BorderLayout.LINE_END); 

    setTabComponentAt(getTabCount() - 1, panel); 
    } 
} 

public class LnFManager { 
    public void setTheme(PlasticTheme theme){ 
    for (Window window : Window.getWindows()) { 
     if (window.isDisplayable()) 
     SwingUtilities.updateComponentTreeUI(window); 
    } 
    } 
} 

Quando setTheme() viene chiamato, questo fa sì che questo accada ripetutamente (presumibilmente per ogni componente):albero validate() genera NullPointerException su L & F cambia?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at javax.swing.plaf.basic.BasicTabbedPaneUI.scrollableTabLayoutEnabled(BasicTabbedPaneUI.java:263) 
at javax.swing.plaf.basic.BasicTabbedPaneUI.access$400(BasicTabbedPaneUI.java:54) 
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabContainer.doLayout(BasicTabbedPaneUI.java:3850) 
at java.awt.Container.validateTree(Container.java:1568) 
at java.awt.Container.validateTree(Container.java:1575) 
at java.awt.Container.validateTree(Container.java:1575) 
at java.awt.Container.validateTree(Container.java:1575) 
at java.awt.Container.validateTree(Container.java:1575) 
at java.awt.Container.validate(Container.java:1540) 
at javax.swing.SwingUtilities.updateComponentTreeUI(SwingUtilities.java:1227) 
at flextrade.tca.ui.LnFManager.setTheme(LnFManager.java:113) 
at flextrade.tca.ui.ToolBar$15.actionPerformed(ToolBar.java:803) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335) 
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404) 
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:308) 
at javax.swing.AbstractButton.doClick(AbstractButton.java:374) 
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:829) 
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:873) 
at java.awt.Component.processMouseEvent(Component.java:6108) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) 
at java.awt.Component.processEvent(Component.java:5873) 
at java.awt.Container.processEvent(Container.java:2105) 
at java.awt.Component.dispatchEventImpl(Component.java:4469) 
at java.awt.Container.dispatchEventImpl(Container.java:2163) 
at java.awt.Component.dispatchEvent(Component.java:4295) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055) 
at java.awt.Container.dispatchEventImpl(Container.java:2149) 
at java.awt.Window.dispatchEventImpl(Window.java:2478) 
at java.awt.Component.dispatchEvent(Component.java:4295) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:604) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138) 

Commentando la riga 'setTabComponentAt (getTabCount() - 1, pannello);' blocca l'eccezione dall'essere lanciata (ma questo, ovviamente, interrompe l'interfaccia utente), così come sovrascrive updateUI() nella classe TabbedArea e wrapping super.updateUI() in SwingUtilities.invokeLater (..).

Questo sembra essere simile ad un bug che la squadra jEdit ha avuto con uno dei loro plug-in durante l'aggiornamento L & F. Tuttavia, la loro solution era semplicemente quello di non aggiornare aspetto dell'applicazione e sentire durante il runtime e fare affidamento su un riavviare per farlo.

Qualcuno ha qualche idea su come questo può essere risolto senza bisogno di un riavvio per cambiare l'aspetto grafico?

+0

L'oscillazione non è thread-safe, quindi è necessario modificare l'interfaccia utente nel thread di invio eventi. L'implementazione di wrapping di 'setTheme' in' SwingUtilities.invokeLater (...) 'risolve il problema? –

+0

Quali JRE stai usando e quali L & F stai usando? – wolfcastle

risposta

1

Guardando il sorgente per Java 6 ho trovato la linea 263 di BasicTabbedPaneUI è una dichiarazione di ritorno:

 /* In an attempt to preserve backward compatibility for programs 
     * which have extended BasicTabbedPaneUI to do their own layout, the 
     * UI uses the installed layoutManager (and not tabLayoutPolicy) to 
     * determine if scrollTabLayout is enabled. 
     */ 
     private boolean scrollableTabLayoutEnabled() { 
      return (tabPane.getLayout() instanceof TabbedPaneScrollLayout); 
     } 

L'unica cosa che può causare il NPE è che il riferimento tabPane è nullo. Senza ulteriori informazioni tutto quello che posso immaginare è che hai rimosso un componente di tabulazione senza rimuovere la scheda corrispondente.

Problemi correlati