默认的JFileChooser可以工作,但我不喜欢它弹出来的事实。我更愿意有一个GUI,所有操作都在其中进行。
现在,我确实成功了。下面的代码将FileChooser菜单放置在GUI内部,而不是弹出在其上方。
我遇到的困难是如何获取所选文件。当JFileChooser嵌入面板时,我知道适用于未嵌入面板的JFileChooser的代码,但我无法使其正常工作。
有人能帮忙吗?
附:我尝试查找,但Oracle虽然提到了将其放置在容器中的可能性,但没有提供示例。 http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html
现在,我确实成功了。下面的代码将FileChooser菜单放置在GUI内部,而不是弹出在其上方。
我遇到的困难是如何获取所选文件。当JFileChooser嵌入面板时,我知道适用于未嵌入面板的JFileChooser的代码,但我无法使其正常工作。
有人能帮忙吗?
附:我尝试查找,但Oracle虽然提到了将其放置在容器中的可能性,但没有提供示例。 http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html
import java.awt.*;
import javax.swing.*;
class SplitPane extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JSplitPane splitPaneV;
private JSplitPane splitPaneH;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
public SplitPane() {
setTitle("Split Pane Application");
setBackground(Color.gray);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
topPanel.setPreferredSize(new Dimension(700, 500));
getContentPane().add(topPanel);
// Create the panels
createPanel1();
createPanel2();
createPanel3();
// Create a splitter pane
splitPaneV = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
topPanel.add(splitPaneV, BorderLayout.CENTER);
splitPaneH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPaneH.setLeftComponent(panel1);
splitPaneH.setRightComponent(panel2);
splitPaneV.setLeftComponent(splitPaneH);
splitPaneV.setRightComponent(panel3);
}
public void createPanel1() {
panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
// Add some buttons
panel1.add(new JButton("North"), BorderLayout.NORTH);
panel1.add(new JButton("South"), BorderLayout.SOUTH);
panel1.add(new JButton("East"), BorderLayout.EAST);
panel1.add(new JButton("West"), BorderLayout.WEST);
panel1.add(new JButton("Center"), BorderLayout.CENTER);
}
public void createPanel2() {
panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.add(new JButton("Button 1"));
panel2.add(new JButton("Button 2"));
panel2.add(new JButton("Button 3"));
}
public void createPanel3() {
panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.setPreferredSize(new Dimension(400, 100));
panel3.setMinimumSize(new Dimension(100, 50));
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser
.setDialogTitle("Browse naar de locatie waar je de gesorteerde bestanden wil zetten en klik op \"OPEN\"");
panel3.add(fileChooser, BorderLayout.NORTH);
}
// this is where my quest starts. Now, I would like to work with the file
// chosen...
// for my ordinary 'popup' fileChoosers the code below works, so I tried the
// code below
// int returnVal = fileChooser.showOpenDialog(panel3);
// if (returnVal == JFileChooser.APPROVE_OPTION)
// fileName = fileChooser.getSelectedFile().getPath();
// System.out.println(fileName);
// but in this case it messes everything up..., after uncommenting I lose
// the frames, and get a popup again...
// anybody a suggestion how to actually get the users chosen file?
public static void main(String args[]) {
// Create an instance of the test application
SplitPane mainFrame = new SplitPane();
mainFrame.pack();
mainFrame.setVisible(true);
}
}