-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
171 lines (144 loc) · 6.15 KB
/
GUI.java
File metadata and controls
171 lines (144 loc) · 6.15 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
public class GUI extends JFrame {
private Dimension screenSize;
private JTextField inputFileTextField;
private JTextField outputFileTextField;
private String graphText;
public GUI() {
super("Bar Graph Creator");
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// Set up main panel with input and output file fields
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel pathPanel = new JPanel();
pathPanel.setLayout(new BorderLayout());
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BorderLayout());
JLabel inputLabel = new JLabel("Input file: ");
FontMetrics metrics = inputLabel.getFontMetrics(inputLabel.getFont());
int labelWidth = metrics.stringWidth("Output file: ");
inputLabel.setPreferredSize(new Dimension(labelWidth, inputLabel.getPreferredSize().height));
inputPanel.add(inputLabel, BorderLayout.WEST);
inputFileTextField = new JTextField(20);
inputPanel.add(inputFileTextField, BorderLayout.CENTER);
JButton inputFileChooserButton = new JButton("...");
inputFileChooserButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chooseInputFile();
}
});
inputPanel.add(inputFileChooserButton, BorderLayout.EAST);
pathPanel.add(inputPanel, BorderLayout.NORTH);
JPanel outputPanel = new JPanel();
outputPanel.setLayout(new BorderLayout());
outputPanel.add(new JLabel("Output file: "), BorderLayout.WEST);
outputFileTextField = new JTextField(20);
outputPanel.add(outputFileTextField, BorderLayout.CENTER);
JButton outputFileChooserButton = new JButton("...");
outputFileChooserButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chooseOutputFile();
}
});
outputPanel.add(outputFileChooserButton, BorderLayout.EAST);
pathPanel.add(outputPanel, BorderLayout.SOUTH);
mainPanel.add(pathPanel, BorderLayout.NORTH);
// Set up button panel with "Run" and "Show Graph" buttons
JPanel buttonPanel = new JPanel();
JButton runButton = new JButton("Run");
runButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
runBarGraphCreator();
}
});
buttonPanel.add(runButton);
JButton showGraphButton = new JButton("Show Graph");
showGraphButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showGraph();
}
});
buttonPanel.add(showGraphButton);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
// Add main panel to frame and set up frame
add(mainPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setSize(screenSize.width / 3, screenSize.height / 5);
// Center the frame on the screen
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
private void chooseInputFile() {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Text files", "txt");
fileChooser.setFileFilter(filter);
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
String filePath = file.getAbsolutePath();
if(!filePath.endsWith(".txt"))
filePath += ".txt";
inputFileTextField.setText(filePath);
}
}
private void chooseOutputFile() {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Text files", "txt");
fileChooser.setFileFilter(filter);
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
String filePath = file.getAbsolutePath();
if(!filePath.endsWith(".txt"))
filePath += ".txt";
outputFileTextField.setText(filePath);
}
}
private void runBarGraphCreator() {
String inputPath = inputFileTextField.getText();
String outputPath = outputFileTextField.getText();
CountChars counter = new CountChars(inputPath, outputPath);
if( (inputPath == null || inputPath.isBlank() || inputPath.isEmpty()) ||
(outputPath == null || outputPath.isBlank() || outputPath.isEmpty())) {
counter = new CountChars();
}
graphText = counter.run();
}
private void showGraph() {
if(graphText == null || graphText.isBlank() || graphText.isEmpty())
return;
JFrame graphFrame = new JFrame("Graph");
JTextArea textArea = new JTextArea(graphText);
JScrollPane scrollPane = new JScrollPane(textArea);
graphFrame.add(scrollPane);
graphFrame.pack();
graphFrame.setSize(screenSize.width/2, screenSize.height/2);
graphFrame.setLocationRelativeTo(null);
graphFrame.setVisible(true);
}
}