-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompanyGUI.java
More file actions
186 lines (154 loc) · 4.91 KB
/
Copy pathCompanyGUI.java
File metadata and controls
186 lines (154 loc) · 4.91 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.event.*;
public class CompanyGUI{
private JTextField fileField;
private JButton nameButton;
private JButton locationButton;
private JButton quitButton;
private JLabel photo;
private JTextArea contents;
private JLabel console;
private ArrayList<Company> companies;
private String[][] tableData;
private JTable table;
public CompanyGUI(){
companies = new ArrayList<Company>();
JFrame frame = new JFrame();
frame.setSize(750, 650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.add(new JLabel("Enter file name"));
fileField = new JTextField("companies.csv");
fileField.addActionListener(new FileListener());
panel.add(fileField);
nameButton = new FancyButton("Sort By Name");
nameButton.addActionListener(new NameListener());
panel.add(nameButton);
locationButton = new FancyButton("Sort by Location");
locationButton.addActionListener(new CompanyListener());
panel.add(locationButton);
salaryButton = new FancyButton("Sort by Salary");
salaryButton.addActionListener(new CompanyListener());
panel.add(salaryButton);
quitButton = new WarningButton("Quit");
quitButton.addActionListener(new QuitListener());
panel.add(quitButton);
frame.add(BorderLayout.NORTH, panel);
String[] columns = {"Salary", "Company Name", "Location", "Age", "Number of countries"};
tableData = new String[companies.length][5];
table = new JTable(tableData, columns);
setTableData();
ListSelectionModel select= table.getSelectionModel();
select.addListSelectionListener(new TableListener());
photo = new JLabel();
showPhoto();
JSplitPane pane = new JSplitPane(SwingConstants.VERTICAL, table, photo);
pane.setDividerLocation(90);
frame.add(pane);
frame.setVisible(true);
contents =new JTextArea(5, 40);
frame.add(contents);
console = new JLabel();
console.setForeground(Color.white);
console.setBackground(Color.black);
frame.setVisible(true);
}
class QuitListener implements ActionListener{
public void actionPerformed(ActionEvent event){
System.out.println("----End-------");
System.exit(0);
}
}
private void setTableData(){
for(int i=0; i<companies.size(); i++){
tableData[i][0] = companies.get(i).getSalary() + "";
tableData[i][1] = companies.get(i).getName();
tableData[i][2] = companies.get(i).getLocation();
tableData[i][3] = companies.get(i).getAge();
tableData[i][4] = companies.get(i).getVacation();
tableData[i][5] = companies.get(i).getNumOfCountries();
}
}
class FileListener implements ActionListener{
public void actionPerformed(ActionEvent event){
String fileName = fileField.getText().trim();
try{
FileReader fr = new FileReader(fileName);
BufferedReader file = new BufferedReader(fr);
int size = Integer.parseInt(file.readLine().trim());
companies = new Company[size];
for (int i=0; i<companies.length; i++){
String line = file.readLine();
String[] items = line.split(",");
companies = new ArrayList<Company>(
items[0].trim(),
items[1].trim(),
items[2].trim(),
items[3].trim());
}
StringBuilder builder = new StringBuilder();
for(Company c: companies){
builder.append(c.toString() + "\n");
}
contents.setText(builder.toString());
}
catch(IOException e){
System.out.println(e);
}
}
}
public String companyString(){
StringBuilder builder = new StringBuilder();
for(Company c: companies){
builder.append(c.toString() + "\n");
}
return builder.toString();
}
private void showPhoto(){
int selectedIndex = (int)(Math.random() * companies.length);
photo.setIcon(new ImageIcon(companies.get(selectedIndex).getPhoto()));
}
class NameListener implements ActionListener{
public void actionPerformed(ActionEvent event){
Collections.sort(companies, Company.NAME);
int selectedIndex = table.getSelectedRow();
photo.setIcon(new ImageIcon(companies.get(selectedIndex).getPhoto()));
contents.setText(companyString());
setTableData();
}
}
class CompanyListener implements ActionListener{
public void actionPerformed(ActionEvent event){
Collections.sort(companies, Company.LOCATION);
contents.setText(companyString());
setTableData();
}
}
class TableListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent event){
int selectedIndex = table.getSelectedRow();
photo.setIcon(new ImageIcon(companies.get(selectedIndex).getPhoto()));
}
}
public static void main(String[] args){
CompanyGUI gui = new CompanyGUI();
}
}
class FancyButton extends JButton{
public FancyButton(String str){
super(str);
setBackground(Color.blue);
setForeground(Color.yellow);
}
}
class WarningButton extends JButton{
public WarningButton(String str){
super(str);
setBackground(Color.red);
setBackground(Color.red);
}
}