Linux 下Swing filechooser 崩溃

Swing的filechooser在linux下面有个bug, 如果你在弹出FileChooser的时候得到下面这个NPE,现在有个方法可以临时修正:

java.lang.NullPointerException
    at javax.swing.plaf.metal.MetalFileChooserUI$IndentIcon.getIconWidth(MetalFileChooserUI.java:896)
    at javax.swing.SwingUtilities.layoutCompoundLabelImpl(SwingUtilities.java:963)
    at javax.swing.SwingUtilities.layoutCompoundLabel(SwingUtilities.java:890)
    at javax.swing.plaf.basic.BasicLabelUI.layoutCL(BasicLabelUI.java:94)
    at javax.swing.plaf.basic.BasicLabelUI.getPreferredSize(BasicLabelUI.java:238)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)
    at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1363)
    at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1311)
    at javax.swing.plaf.basic.BasicListUI$Handler.valueChanged(BasicListUI.java:2623)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
    at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:405)
    at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:415)
    at javax.swing.DefaultListSelectionModel.setSelectionInterval(DefaultListSelectionModel.java:459)
    at javax.swing.JList.setSelectedIndex(JList.java:2210)
    at javax.swing.plaf.basic.BasicComboPopup.setListSelection(BasicComboPopup.java:1179)
    at javax.swing.plaf.basic.BasicComboPopup.access$300(BasicComboPopup.java:63)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.itemStateChanged(BasicComboPopup.java:1008)
    at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1223)
    at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1280)
    at javax.swing.JComboBox.contentsChanged(JComboBox.java:1330)

用这段代码包围filechooser就好了

LookAndFeel lookAndFeelBackup = UIManager.getLookAndFeel();
try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
//这里操作 filechooser ...
try {
        UIManager.setLookAndFeel(lookAndFeel); //恢复原来的样式
    } catch (UnsupportedLookAndFeelException e) {
}

你可能感兴趣的:(Linux 下Swing filechooser 崩溃)