Jbutton in java swing (to view pc folders)

I want to create a “browse” button in a swing, in which, when a user “views” the browse button, he can choose a location from his folders on the hard drive to save the file. This is part of my interface .. how do I do this? I want the path to appear in the text box on the sidebar of the browse button.

+4
source share
2 answers

You should take a look at the Sun tutorial for the JFileChooser API. This will give you almost everything you need to accomplish what you are trying to do.

+4
source
 ... 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); } } } ... 
+2
source

Source: https://habr.com/ru/post/1301094/


All Articles