Custom JFileChooser. How to add JComboBox into the JFileChooser

≯℡__Kan透↙ 提交于 2019-12-11 18:39:13

问题


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:


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/24309517/custom-jfilechooser-how-to-add-jcombobox-into-the-jfilechooser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!