1

When i open the dialog box through CustomizedJFileChooser. JfileChooser look and Feel not good. so, for look and feel i am adding the code

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

it raise the exception.

Here is my code,

    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);
   }
}

Here stack trace of excetpion:

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)

Thank you.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Can you add the stacktrace please? – Jens Sep 11 '14 at 11:30
  • `java.lang.ArrayIndexOutOfBoundsException: No such child: 3` I think your problem lies in the line `JPanel panel1 = (JPanel)this.getComponent(3);` Your JPanel doesn't contain a third element. – Alexander_Winter Sep 11 '14 at 11:36
  • Yes your correct. But when i remove above single line code it is working properly but look and feel not good. But, i need the look and feel. how to add look and feel to customized filechooser. @Alexander_Winter – user3531731 Sep 11 '14 at 11:40
  • 3
    Different look and feels treat the laying out of components differently. Your custom implementation may not work on all the look and feels. Consider maybe just using `JFileChooser.setAcessory` to avoid this problem. As seen [here](http://stackoverflow.com/a/24291619/2587435). Or seen in [the tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#accessory) – Paul Samsotha Sep 11 '14 at 11:40
  • 1
    Can you edit your post showing the new code so we can test. – Paul Samsotha Sep 11 '14 at 11:55
  • Also what system are you on? I just tested the code from the link in my comment, with windows, and it works fine – Paul Samsotha Sep 11 '14 at 12:00
  • i am using windows.just i add FileListAccessory accessory = new FileListAccessory(jc); jc.setAccessory(accessory); these two lines to my code and copy FileListAccessory class based upon provieded link and tested. @peeskillet – user3531731 Sep 11 '14 at 12:06
  • There's a launching program `JavaApplication4`, in the linked answer also. Try to run it with _that_ and see what happens. If that works, then show _your_ code so we can see where you may be going wrong – Paul Samsotha Sep 11 '14 at 12:08
  • thanks for provide the link. its working properly. @peeskillet – user3531731 Sep 11 '14 at 12:21
  • Several approaches are compared [here](http://stackoverflow.com/a/25538945/230513). – trashgod Sep 11 '14 at 14:46
  • Post another question – Paul Samsotha Sep 12 '14 at 06:03

2 Answers2

0

If you add the Look and Feel the object structure changed:

If you print the components you can see that.

Without setting look and feel:

[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=]]

with lookand feel:

[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=]]
Jens
  • 67,715
  • 15
  • 98
  • 113
0

The Component List has a zero-based index. Modify these two lines like below and it works. MyFileChooser has panel1 at index 2 and panel1 has the other panel also at index 2.

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

You forgot to add the third component to panel2. The combobox is teared but you can fix its height afterwards.

My mistake, I forgot to add the panel containing the buttons. I have also added the combobox to its own panel and now it doesn't get teared anymore. Should work fine now.

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);
}

}

Cristian G.
  • 135
  • 7
  • Thanks, really i need this but one question when i use comboBox.setPreferredSize(new Dimension(50,2)) height of the dropdown box is not decreased. how to decrease the height of the dropdown box. @Cristian – user3531731 Sep 12 '14 at 05:33
  • @user351731 - You are welcome! As to the combobox, decrease to what size? does it appear teared? – Cristian G. Sep 19 '14 at 10:49