PLAF上的NullPointerException变为Motif

时间:2013-04-26 09:49:02

标签: java swing nullpointerexception look-and-feel motif

有人能看到我做了什么导致NullPointerException(下面的堆栈跟踪)?

面板显示OK:

Visually working layout

文字输出

os.name:    Windows Vista
os.version:     6.1
java.version:   1.6.0
java.vm.version:    1.6.0-b105
java.vendor:    Sun Microsystems Inc.

那个(1.6.0-b105)很古老,它是Netbeans的默认运行时环境。必须更新到版本7 JRE。

代码

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.GroupLayout.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.EmptyBorder;

class GroupLayoutHelper {

    /**
     * Provides a JPanel with two columns (labels & fields) laid out using
     * GroupLayout. The arrays must be of equal size.
     *
     * @param labels The first column contains labels.
     * @param fields The last column contains fields.
     * @param addMnemonics Add mnemonic by next available letter in label text.
     * @return JComponent A JPanel with two columns of the components provided.
     */
    public static JComponent getTwoColumnLayout(
            JLabel[] labels,
            JComponent[] fields,
            boolean addMnemonics) {
        JComponent panel = new JPanel();
        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);
        // Turn on automatically adding gaps between components
        layout.setAutoCreateGaps(true);
        // Create a sequential group for the horizontal axis.
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        Group yLabelGroup = layout.createParallelGroup(Alignment.TRAILING);
        hGroup.addGroup(yLabelGroup);
        Group yFieldGroup = layout.createParallelGroup();
        hGroup.addGroup(yFieldGroup);
        layout.setHorizontalGroup(hGroup);
        // Create a sequential group for the vertical axis.
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        layout.setVerticalGroup(vGroup);

        int p = GroupLayout.PREFERRED_SIZE;
        // add the components to the groups
        for (JLabel label : labels) {
            yLabelGroup.addComponent(label);
        }
        for (Component field : fields) {
            yFieldGroup.addComponent(field, p, p, p);
        }
        for (int ii = 0; ii < labels.length; ii++) {
            vGroup.addGroup(layout.createParallelGroup().
                    addComponent(labels[ii]).
                    addComponent(fields[ii], p, p, p));
        }

        if (addMnemonics) {
            Map<Character, Object> m = new HashMap<Character, Object>();
            for (int ii = 0; ii < labels.length; ii++) {
                labels[ii].setLabelFor(fields[ii]);
                String lwr = labels[ii].getText().toLowerCase();
                for (int jj = 0; jj < lwr.length(); jj++) {
                    char ch = lwr.charAt(jj);
                    if (m.get(ch) == null) {
                        m.put(ch, ch);
                        labels[ii].setDisplayedMnemonic(ch);
                        break;
                    }
                }
            }
        }

        return panel;
    }

    /**
     * Provides a JPanel with two columns (labels & fields) laid out using
     * GroupLayout. The arrays must be of equal size.
     *
     * @param labelStrings Strings that will be used for labels.
     * @param fields The corresponding fields.
     * @return JComponent A JPanel with two columns of the components provided.
     */
    public static JComponent getTwoColumnLayout(
            String[] labelStrings,
            JComponent[] fields) {
        JLabel[] labels = new JLabel[labelStrings.length];
        for (int ii = 0; ii < labels.length; ii++) {
            labels[ii] = new JLabel(labelStrings[ii]);
        }
        return getTwoColumnLayout(labels, fields);
    }

    /**
     * Provides a JPanel with two columns (labels & fields) laid out using
     * GroupLayout. The arrays must be of equal size.
     *
     * @param labels The first column contains labels.
     * @param fields The last column contains fields.
     * @return JComponent A JPanel with two columns of the components provided.
     */
    public static JComponent getTwoColumnLayout(
            JLabel[] labels,
            JComponent[] fields) {
        return getTwoColumnLayout(labels, fields, true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                final JComboBox plafCombo = new JComboBox(
                        UIManager.getInstalledLookAndFeels());
                plafCombo.setRenderer(new DefaultListCellRenderer() {

                    @Override
                    public Component getListCellRendererComponent(
                            JList list,
                            Object value,
                            int index,
                            boolean isSelected,
                            boolean cellHasFocus) {
                        Component c = super.getListCellRendererComponent(
                                list, value, index, isSelected, cellHasFocus);
                        if (c instanceof JLabel) {
                            JLabel l = (JLabel) c;
                            String s = value.toString();
                            s = s.substring(
                                    s.lastIndexOf(".") + 1, s.length() - 12);
                            l.setText(s);
                            return l;
                        }
                        return c;
                    }
                });
                plafCombo.addItemListener(new ItemListener() {

                    @Override
                    public void itemStateChanged(ItemEvent e) {
                        Object o = plafCombo.getSelectedItem();
                        LookAndFeelInfo plafi = (LookAndFeelInfo) o;
                        try {
                            UIManager.setLookAndFeel(plafi.getClassName());
                            Container c = plafCombo.getTopLevelAncestor();
                            SwingUtilities.updateComponentTreeUI(c);
                            if (c instanceof Window) {
                                Window w = (Window) c;
                                w.pack();
                            }
                        } catch (Exception ignoreAndContinue) {
                        }
                    }
                });
                String longText = getProperty("os.name") +
                        getProperty("os.version") +
                        getProperty("java.version") +
                        getProperty("java.vm.version") +
                        getProperty("java.vendor") +
                        "";
                JComponent[] c = {
                    new JTextField(5),
                    new JTextField(20),
                    plafCombo,
                    new JPasswordField(8),
                    new JScrollPane(new JTextArea(longText, 5, 38))
                };

                String[] ls = {"ID", "Name", "Look & Feel", "Password", "Postcript"};
                JLabel[] l = new JLabel[ls.length];
                JComponent gui = getTwoColumnLayout(ls, c);

                gui.setBorder(new EmptyBorder(4, 8, 4, 8));

                JFrame f = new JFrame("Groupie");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };

        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }

    public static String getProperty(String name) {
        return name + ": \t" + 
                System.getProperty(name) + 
                System.getProperty("line.separator");
    }
}

堆栈跟踪

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.plaf.basic.BasicComboBoxUI.selectNextPossibleValue(BasicComboBoxUI.java:1072)
    at javax.swing.plaf.basic.BasicComboBoxUI$Actions.actionPerformed(BasicComboBoxUI.java:1450)
    at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
    at javax.swing.JComponent.processKeyBinding(JComponent.java:2844)
    at javax.swing.JComponent.processKeyBindings(JComponent.java:2890)
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2807)
    at javax.swing.JComboBox.processKeyEvent(JComboBox.java:1399)
    at java.awt.Component.processEvent(Component.java:5815)
    at java.awt.Container.processEvent(Container.java:2058)
    at java.awt.Component.dispatchEventImpl(Component.java:4410)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:693)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:958)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:830)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:657)
    at java.awt.Component.dispatchEventImpl(Component.java:4282)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Window.dispatchEventImpl(Window.java:2429)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.plaf.basic.BasicComboBoxUI.selectNextPossibleValue(BasicComboBoxUI.java:1072)
    at javax.swing.plaf.basic.BasicComboBoxUI$Actions.actionPerformed(BasicComboBoxUI.java:1432)
    at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
    at javax.swing.JComponent.processKeyBinding(JComponent.java:2844)
    at javax.swing.JComponent.processKeyBindings(JComponent.java:2890)
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2807)
    at javax.swing.JComboBox.processKeyEvent(JComboBox.java:1399)
    at java.awt.Component.processEvent(Component.java:5815)
    at java.awt.Container.processEvent(Container.java:2058)
    at java.awt.Component.dispatchEventImpl(Component.java:4410)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:693)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:958)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:830)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:657)
    at java.awt.Component.dispatchEventImpl(Component.java:4282)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Window.dispatchEventImpl(Window.java:2429)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    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)

2 个答案:

答案 0 :(得分:3)

  • 我在1秒内添加了Swing Timer 30次所有L&amp; Fs。期间,没问题,

Win8(64b企业版)

  • jdk1.6.0_22

enter image description here

  • jdk1.7.0_11

enter image description here


WinXP

  • jdk1.6.0_22

enter image description here


Win7(64b企业版)

  • jdk1.7.0_11(注意渲染,resp。刷新太懒,不能在SwingTimer上使用1秒)

enter image description here

  • jdk1.6.0_22(L&amp; F切换后没有问题渲染,不明白,JDK7同时用JDK6测试了两个窗口,phaaa我需要安装最后一个JDK7(不知道版本) ): - )

enter image description here

  • jdk1.7.0_21没有渲染问题,分别是。和另一个在另一个WinOS版本上的JDK一样,奇怪的是这个jdk1.7.0_11,问题仍然存在,可重现,多少我讨厌Win7,Swing,L&amp; F编译在jdk1.7.0_11上

答案 1 :(得分:0)

使用箭头键更改JComboBox的值时会导致此错误。

我有这个错误,我不知道如何解决。

我认为最佳镜头是将JComboBox设置为默认状态然后更改LAF。