简体   繁体   中英

Custom JFileChooser. How to add JComboBox into the JFileChooser

I want to add JComboBox into the JFileChooser.

I tried to extend JFileChooser and to add the combo box manually. I actually managed to do it but that removed the file navigation panel from the JFileChooser dialog. Code:

 public class CustomDefinitionJFileChooser extends JFileChooser{
       public CustomDefinitionJFileChooser() {
            JComboBox comboBox = new JComboBox();
            comboBox.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2" }));
            super.add(comboBox);
       }
 }

Expected result:

在此处输入图片说明

Actual result:

在此处输入图片说明

First try to get the Container of the location you want to add the combobox . Then simply add the combobox to that Container.

For example here first I have found the Container panel of the Save and Cancel button.

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

Then I have added the combobox in the panel2

panel2.add(comboBox);

It will look like:

在此处输入图片说明

Full Code:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

public class TestFilechooser extends JFrame {

    private JPanel contentPane;
    MyFileChooser jc;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestFilechooser frame = new TestFilechooser();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public TestFilechooser() {
        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(TestFilechooser.this);

            }
        });
        pack();
    }

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

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

        Component c1=panel2.getComponent(0);//optional used to add the buttons after combobox
        Component c2=panel2.getComponent(1);//optional used to add the buttons after combobox
        panel2.removeAll();

        panel2.add(comboBox);
        panel2.add(c1);//optional used to add the buttons after combobox
        panel2.add(c2);//optional used to add the buttons after combobox

   }
}

Easy process:

This solution will not add the combobox at the bottom but I think it can help you.

Change your class like this:

class MyFileChooser extends JFileChooser{
    public MyFileChooser() {
        JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2" }));
        JPanel panel = new JPanel();
        panel.add(comboBox);
        setAccessory(panel);
        //add(comboBox, BorderLayout.SOUTH);
   }
}

Result:

在此处输入图片说明

Full Working Code for the example:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

public class TestFilechooser extends JFrame {

    private JPanel contentPane;
    MyFileChooser jc;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestFilechooser frame = new TestFilechooser();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public TestFilechooser() {
        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(TestFilechooser.this);

            }
        });
        pack();
    }

}
class MyFileChooser extends JFileChooser{
    public MyFileChooser() {
        JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2" }));
        JPanel panel = new JPanel();
        panel.add(comboBox);
        setAccessory(panel);
        //add(comboBox, BorderLayout.SOUTH);
   }
}
private void addEncodingComboBox(JFileChooser f,JComboBox<String> encodingComboBox, JLabel encodingLabel) {
    Component comp =f.getComponent(2);
    JPanel fPanel=(JPanel) comp;
    JPanel na=(JPanel)fPanel.getComponent(2);
    JPanel fields=(JPanel)na.getComponent(2);
    fields.add(Box.createRigidArea(new Dimension(1,8)));
    fields.add(encodingComboBox);
    JPanel labels=(JPanel)na.getComponent(0);
    labels.add(Box.createRigidArea(new Dimension(1,12)));
    labels.add(encodingLabel);
}

fields contains the combobox, labels contains the labels for components in fields. This code only works if UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); is called The result: http://kepfeltoltes.hu/140810/103167300asd_www.kepfeltoltes.hu_.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM