java - 自定义 JFileChooser。如何将 JComboBox 添加到 JFileChooser 中

标签 java swing jcombobox jfilechooser

我想将 JComboBox 添加到 JFileChooser 中。

我尝试扩展 JFileChooser 并手动添加组合框。我实际上设法做到了这一点,但是从 JFileChooser 对话框中删除了文件导航面板。代码:

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

预期结果:

enter image description here

实际结果:

enter image description here

最佳答案

首先尝试获取要添加组合框位置的Container。 然后只需将组合框添加到该容器即可。

例如,首先我找到了保存取消按钮的Container面板。

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

然后我在 panel2 中添加了组合框

panel2.add(comboBox);

它看起来像:

enter image description here

完整代码:

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

   }
}

简单的过程:

此解决方案不会在底部添加组合框,但我认为它可以帮助您。

像这样更改你的类:

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

结果:

enter image description here

示例的完整工作代码:

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

关于java - 自定义 JFileChooser。如何将 JComboBox 添加到 JFileChooser 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24309517/

相关文章:

java - 正则表达式匹配完全限定的java变量

java - 来自 Controller 和其他类的 Spring Boot 静态资源

java - 表单验证无法正常工作

java - java中有没有返回系统主题颜色的方法?

java - 数据在 for 循环中的数组列表内的数组列表中重复 Recylerview Koltin

java - 如何读取堆大小——占用和空闲?

java - 如何查看JCheckbox被选中的数量?

java - JComboBox 的输出被打印两次

javascript - 无法过滤jcombo中的字符

java - JComboBox/JTextField 的每个单词在任何地方自动完成