自定义JFileChooser。如何将JComboBox添加到JFileChooser中
问题描述:
我想将JComboBox添加到JFileChooser中。自定义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);
}
}
预期结果:
实际结果:
答
第一次尝试得到你想要添加的combobox
位置的Container
。 然后只需将该组合框添加到该Container。
例如,在这里首先我找到Save
和Cancel
按钮的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);
}
}
答
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);
}
领域CONTA在组合框中,标签包含字段中组件的标签。 此代码仅在UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());被称为 结果:http://kepfeltoltes.hu/140810/103167300asd_www.kepfeltoltes.hu_.png
不在你的构造函数中调用'super();'可能没有帮助。 –
我会避免试图这样做,除非你真的知道你在做什么。我只会使用更多API特定的东西,比如['JFileChooser.setAccessory'](http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#accessory)。你可能得不到你想要的确切外观,但至少你不会破坏任何东西。这里还有一个[示例](http://*.com/a/24291619/2587435) –
*“我想将JComboBox添加到JFileChooser中。”*为什么?这个组合是什么?为标准文件选择器没有的最终用户做什么? –