-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path66_SelectAFile
More file actions
52 lines (43 loc) · 1.44 KB
/
66_SelectAFile
File metadata and controls
52 lines (43 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// JFileChooser = A GUI mechanism that let's a user choose a file (helpful for opening or saving file)
<Main.java>
public class Main {
public static void main(String[] args) {
new MyFram();
}
}
<MyFram.java>
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class MyFram extends JFrame implements ActionListener {
JButton button;
MyFram(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
button = new JButton("Select File");
button.addActionListener(this);
this.add(button);
this.pack();
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button){
JFileChooser fileChooser = new JFileChooser();
/*
//TO OPEN
int response = fileChooser.showOpenDialog(null);//(parent): select file to open
if(response == JFileChooser.APPROVE_OPTION){
JFileChooser jFileChooser;
File file = new File(fileChooser.getSelectedFile().getAbsolutePath()); //Show path
System.out.println(file);
}
*/
//TO SAVE
fileChooser.setCurrentDirectory(new File("."));
int response2 = fileChooser.showSaveDialog(this);//Select file to save
}
}
}