【问题标题】:Custom JFileChooser. How to add JComboBox into the JFileChooser自定义 JFileChooser。如何将 JComboBox 添加到 JFileChooser
【发布时间】:2014-06-19 14:48:47
【问题描述】:

我想将 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);
       }
 }

预期结果:

实际结果:

【问题讨论】:

  • 在构造函数中不调用 super(); 可能无济于事。
  • 除非您真的知道自己在做什么,否则我会避免尝试这样做。我只会使用更具体的 API,例如 JFileChooser.setAccessory。您可能无法获得您想要的确切外观,但至少您不会破坏任何东西。这里还有一个example
  • “我想将 JComboBox 添加到 JFileChooser 中。” 为什么?这是什么组合。为最终用户做标准文件选择器不做的事情?

标签: java swing jcombobox jfilechooser


【解决方案1】:

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

例如这里首先我找到了SaveCancel 按钮的Container 面板。

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

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

panel2.add(comboBox);

它看起来像:

完整代码:

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

结果:

示例的完整工作代码:

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 包含组合框,labels 包含字段中组件的标签。 此代码仅适用于 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());叫做 结果:http://kepfeltoltes.hu/140810/103167300asd_www.kepfeltoltes.hu_.png

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 2015-06-02
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      相关资源
      最近更新 更多