更改为自定义JFileChooser的外观会引发异常

时间:2014-09-11 11:29:04

标签: java swing indexoutofboundsexception look-and-feel jfilechooser

当我通过CustomizedJFileChooser打开对话框时。 JfileChooser外观和感觉并不好。所以,为了外观和感觉,我正在添加代码

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

它引发了异常。

这是我的代码,

    public class FileChooser extends JFrame {

    private JPanel contentPane;
    MyFileChooser jc;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                    FileChooser frame = new FileChooser();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
        public FileChooser() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        jc = new MyFileChooser();
        JButton btnOpen = new JButton("open");
        contentPane.add(btnOpen, BorderLayout.NORTH);

        btnOpen.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                int returnVal = jc.showOpenDialog(FileChooser.this);

            }
        });
          pack();
         }

         }
         class MyFileChooser extends JFileChooser{
         public MyFileChooser() {
         JComboBox comboBox = new JComboBox();
          comboBox.setModel(new DefaultComboBoxModel(new String[] { "text", "binary" }));

          JPanel panel1 = (JPanel)this.getComponent(3);
        JPanel panel2 = (JPanel) panel1.getComponent(3);

           Component c1=panel2.getComponent(0);
        Component c2=panel2.getComponent(1);
        panel2.removeAll();
        panel2.add(new JLabel("Document Name: "));
        panel2.add(comboBox);
        panel2.add(c1);
        panel2.add(c2);
   }
}

这里叠加了痕迹:

java.lang.ArrayIndexOutOfBoundsException: No such child: 3
at java.awt.Container.getComponent(Container.java:327)
at MyFileChooser.<init>(FileChooser.java:62)
at FileChooser.<init>(FileChooser.java:41)
at FileChooser$1.run(FileChooser.java:27)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

谢谢。

2 个答案:

答案 0 :(得分:0)

如果添加外观,则对象结构发生变化:

如果您打印组件,您可以看到它。

没有设定外观:

[javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.BorderLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=], javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.BorderLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=], sun.swing.FilePane[,0,0,0x0,invalid,layout=java.awt.BorderLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=265,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=405,height=135]], javax.swing.JPanel[,0,0,0x0,invalid,layout=javax.swing.BoxLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]]
带着外观的感觉:

[sun.swing.WindowsPlacesBar[,0,0,0x0,invalid,layout=javax.swing.JToolBar$DefaultToolBarLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.EmptyBorder@1cad7d80,flags=328,maximumSize=,minimumSize=,preferredSize=,floatable=false,margin=,orientation=VERTICAL,paintBorder=true], javax.swing.JToolBar[,0,0,0x0,invalid,layout=javax.swing.JToolBar$DefaultToolBarLayout,alignmentX=0.0,alignmentY=0.0,border=com.sun.java.swing.plaf.windows.WindowsBorders$ToolBarBorder@420a6d35,flags=328,maximumSize=,minimumSize=,preferredSize=,floatable=false,margin=,orientation=HORIZONTAL,paintBorder=true], javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.BorderLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]]

答案 1 :(得分:0)

组件列表具有从零开始的索引。修改这两行,如下所示,它的工作原理。 MyFileChooser 在索引2处有 panel1 ,而 panel1 的另一个面板也在索引2处。

JPanel panel1 = (JPanel) this.getComponent(2);
JPanel panel2 = (JPanel) panel1.getComponent(2);

您忘记将第三个组件添加到 panel2 。组合框是撕裂的,但你可以在之后修复它的高度。

我的错误,我忘了添加包含按钮的面板。我还将组合框添加到它自己的面板中,现在它不再被撕掉了。现在应该工作正常。

class MyFileChooser extends JFileChooser {
public MyFileChooser() {
    JComboBox comboBox = new JComboBox();
    comboBox.setModel(new DefaultComboBoxModel(new String[]{"text", "binary"}));

    JPanel panel1 = (JPanel) this.getComponent(2);
      JPanel panel2 = (JPanel) panel1.getComponent(2);

      Component c1=panel2.getComponent(0);
      Component c2=panel2.getComponent(1);
      Component c3=panel2.getComponent(2);
      Component c4=panel2.getComponent(3);
      Component c5=panel2.getComponent(4);

      JPanel comboboxPanel = new JPanel();
      comboboxPanel.setLayout(new FlowLayout());
      comboboxPanel.add(new JLabel("Document Name: "));
      comboboxPanel.add(comboBox);

      panel2.removeAll();
      panel2.add(comboboxPanel);
      panel2.add(Box.createRigidArea(new Dimension(5,5)));
      panel2.add(c1);
      panel2.add(c2);
      panel2.add(c3);
      panel2.add(c4);
      panel2.add(c5);
}

}