开发者

JFileChooser inside JPanel; how to get users choice

The default JFileChooser works, but what I don't like is the fact that it pops up. I'd rather have one GUI in which all the action takes place.

Now, I did manage to do that. The code below places the FileChooser menu nicely inside the GUI, instead of popping up above it.

What I am having a hard time with is how to get my hands on the selected file. I do know the code that works when JFileChooser is not embedded in a Panel, but I cant get this to work.

Anybody??

ps. I did try and look it up, but though Oracle does mention the possibility to place it in a container, it doesnt supply 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.setFileSelec开发者_如何学运维tionMode(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);
    }
}


Note that you can add an ActionListener to a JFileChooser that will respond to button press, and the ActionEvent's getActionCommand will tell you which button was pressed. E.G.,

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜