-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLoader.java
More file actions
88 lines (78 loc) · 3 KB
/
FileLoader.java
File metadata and controls
88 lines (78 loc) · 3 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* A JPanel component for choosing an image file to be loaded into the SnapShop ImagePanel
* user can choose with a JFileChooser, or type into a text field
* ImagePanel loadImage is called by button action
*
* @author Richard Dunn, Tim Gesell, modified by Pranav Kadekodi
* @version November 5th 2023
*/
import java.awt.FlowLayout;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.File;
import java.io.IOException;
public class FileLoader extends JPanel implements ActionListener {
private JTextArea filenameBox;
private JButton loadButton;
private JButton browseButton;
private JFileChooser fileChooser;
private JLabel label;
private SnapShop s;
public FileLoader(SnapShop s) {
super(new FlowLayout()); //Call superclass JPanel's constructor
this.s = s;
label = new JLabel("Enter file name: ");
add(label); //calling superclass JPanel's add method
filenameBox = new JTextArea(1, 50);
filenameBox.setText(SnapShopConfiguration.getDefaultFilename());
add(filenameBox); //calling superclass JPanel's add method
loadButton = new JButton("Load");
loadButton.addActionListener(this);
add(loadButton); //calling superclass JPanel's add method
browseButton = new JButton("Browse");
browseButton.addActionListener(this);
add(browseButton); //calling superclass JPanel's add method
fileChooser = new JFileChooser(System.getProperty("user.dir"));
FileNameExtensionFilter imageFilter = new FileNameExtensionFilter("Image","jpg","jpeg","png","gif");
fileChooser.addChoosableFileFilter(imageFilter);
fileChooser.setFileFilter(imageFilter);
}
public void actionPerformed(ActionEvent e) {
File file;
if (e.getSource() == browseButton)
{
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
try{
filenameBox.setText(file.getCanonicalPath());
}catch(IOException ex)
{
filenameBox.setText(SnapShopConfiguration.getDefaultFilename());
}
}
else return;
}
else //if the source of the event was the load button
{
String filename = filenameBox.getText();
file = new File(filename);
}
try {
ImagePanel ip = s.getImagePanel();
ip.loadImage(file);
} catch (Exception ex) {
JOptionPane.showMessageDialog(s,
"Couldn't open file",
"Error Encountered",
JOptionPane.ERROR_MESSAGE);
}
}
}