gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:30:51 28 4
gpt4 key购买 nike

我想将 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com