开发者

jbutton in java swing(to browse through pc folders)

I want to create a "browse" button in swing in which when a user "browse" browse button he shold be able to select a location from his hard drive folders to save the file.This is a part of my interface design.how do i do it? I want the path to be displayed onto the text box on t开发者_C百科he side of browse button.


You should take a look at Sun's tutorial for the JFileChooser API. This will give you pretty much everything you need to accomplish what you're trying to do.


...
public String fileID;
public JTextField txtField; //Assume this is the text box you placed beside browse button
public JButton btnBrowse = JButton("Browse");

public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == btnBrowse)
    {
        chooser = new JFileChooser(new File(System.getProperty("user.home") + "\\Downloads")); //Downloads Directory as default
        chooser.setDialogTitle("Select Location");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);

        if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
        { 
            fileID = chooser.getSelectedFile().getPath();
            txtField.setText(fileID);
        }
    }
}
...
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜