Java – Jfilechooser in JPanel; How to let users choose
The default Jfilechooser works, but what I don't like is the fact that it pops up I'd rather have a GUI where all the actions happen
Now, I did manage to do it The following code puts the filechooser menu well in the GUI instead of popping up on it
What I'm working on is how to get the selected file I know Jfilechooser has no valid code when embedded in the panel, but I can't use it
anybody?
PS. I did try to find it, but although Oracle did mention the possibility of putting it in a container, it did not provide an example 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); } }
Solution
Note that you can add the actionlistener to the Jfilechooser that will press the response button, and the getActionCommand of ActionEvent will tell you which button is pressed For example,
public void createPanel3() { panel3 = new JPanel(); panel3.setLayout(new BorderLayout()); panel3.setPreferredSize(new Dimension(400,100)); panel3.setMinimumSize(new Dimension(100,50)); final 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); fileChooser.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) { System.out.println("File selected: " + fileChooser.getSelectedFile()); } } }); }