-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddDropCompany.java
More file actions
83 lines (71 loc) · 2.18 KB
/
Copy pathAddDropCompany.java
File metadata and controls
83 lines (71 loc) · 2.18 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.event.*;
public class AddDropCompany{
private JTextField inputField;
private JList list;
private ArrayList<Company> companies;
private JButton addButton;
private JButton removeButton;
private JLabel console;
public AddDropCompany(){
companies = new ArrayList<Company>();
JFrame frame = new JFrame("Add and Drop Company");
frame.setSize(550, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.add(new JLabel("Input Company Data: "));
inputField = new JTextField(14);
AddListener addListener = new AddListener();
inputField.addActionListener(addListener);
panel.add(inputField);
addButton = new JButton("Add Company");
addButton.addActionListener(addListener);
panel.add(addButton);
removeButton = new JButton("Remove Company");
removeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
int removeSalary = Integer.parseInt(inputField.getText().trim());
int index = getIndex(removeSalary);
if(index != -1){
Company removed = companies.remove(index);
console.setText("Removed: " + removed);
}else{
console.setText("Salary " + removeSalary + " not found");
}
list.setListData(companies.toArray());
}
});
panel.add(removeButton);
frame.add(BorderLayout.NORTH, panel);
list = new JList();
frame.add(list);
console = new JLabel();
frame.add(BorderLayout.SOUTH, console);
frame.setVisible(true);
}
public int getIndex(int salary){
for(int i=0; i<companies.size(); i++){
Company sal = companies.get(i);
if (sal.getSalary() == salary){
return i;
}
}
return -1;
}
class AddListener implements ActionListener{
public void actionPerformed(ActionEvent event){
String[] items = inputField.getText().split(",");
Company company = new Company(items[0].trim(), items[1].trim(), items[2].trim(), items[3].trim());
companies.add(company);
console.setText("Added: " + company);
list.setListData(companies.toArray());
}
}
public static void main(String[] args){
new AddDropCompany();
}
}