-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewFrame.java
More file actions
71 lines (69 loc) · 2.65 KB
/
Copy pathViewFrame.java
File metadata and controls
71 lines (69 loc) · 2.65 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
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import javax.swing.*;
public class ViewFrame extends JFrame {
File textFile;
FileReader in;
BufferedReader readFile;
JPanel panel;
JScrollPane scroll;
JTextArea area;
String[] arr;
String lineOfText;
JButton back;
ApplicationFrame mainframe;
ViewFrame(){//this is the frame where the stock can be viewed from
addWindowListener(new WindowAdapter() {//detects when users click x
public void windowClosing(WindowEvent e) {
int choice = JOptionPane.showConfirmDialog(null, "By not properly exiting, your inventory could be wiped","Confirmation",JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setSize(300,300);
setVisible(true);
setLocationRelativeTo(null);
try {
textFile = new File("inventory.txt");//reads from a text file called "inventory"
in = new FileReader(textFile);
readFile = new BufferedReader(in);
lineOfText = readFile.readLine(); //stores the text into a single string
if(lineOfText==null) {//if file is empty it will fill the string with a single space which prevents the program from crashing
lineOfText = " ";
}
arr = lineOfText.split(" "); //tracks inventory within an array
//even number indexes = itemid
//odd number indexes = itemamount
readFile.close();//close filereaders
in.close();
}
catch(FileNotFoundException e){//file not found exception
JOptionPane.showMessageDialog(null, "File not found","Error",JOptionPane.ERROR_MESSAGE);
}
catch(IOException e){//io exception
JOptionPane.showMessageDialog(null, "IOException","Error",JOptionPane.ERROR_MESSAGE);
}
catch(Exception e) {//catches everything else
JOptionPane.showMessageDialog(null, "An error has occured","Error",JOptionPane.ERROR_MESSAGE);
}
area = new JTextArea(30,20);
for(int i = 0; i<arr.length-1;i=i+2) {
area.setText(area.getText() + "\n" + arr[i] + " " + arr[i+1]);//sets the textarea
}
area.setEditable(false);
back = new JButton("back");
back.addActionListener(a->{//back button closes current frame and brings user back to the main frame
mainframe = new ApplicationFrame();this.dispose();
});
panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(scroll);
panel.add(back);
setContentPane(panel);
}
}