-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTabela.java
151 lines (115 loc) · 3.77 KB
/
Tabela.java
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
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class Tabela {
public static void main(String[] args){
JFrame frame = new JFrame();
JTable table = new JTable();
Object[] columns = {"Id", "First Name", "Last Name", "Age"};
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(columns);
table.setModel(model);
table.setBackground(Color.blue);
table.setForeground(Color.black);
Font font = new Font("Sans Serif", Font.BOLD, 13);
table.setFont(font);
table.setRowHeight(30);
JLabel Id = new JLabel("Id");
JLabel FirstName = new JLabel("First Name");
JLabel LastName = new JLabel("Last Name");
JLabel Age = new JLabel("Age");
Id.setBounds(20, 200, 100, 50);
frame.add(Id);
FirstName.setBounds(20, 230, 100, 50);
frame.add(FirstName);
LastName.setBounds(20, 260, 100, 50);
frame.add(LastName);
JTextField textId = new JTextField();
JTextField textFname = new JTextField();
JTextField textLname = new JTextField();
JTextField textAge = new JTextField ();
JButton btnAdd = new JButton("Add");
JButton btnDelete = new JButton("Delete");
JButton btnUpdate = new JButton("Update");
textId.setBounds(100, 220, 100, 25);
textFname.setBounds(100, 250, 100, 25);
textLname.setBounds(100, 280, 100, 25);
textAge.setBounds(100, 310, 100, 25);
frame.add(textId);
frame.add(textFname);
frame.add(textLname);
frame.add(textAge);
btnAdd.setBounds(240, 220, 100, 25);
btnDelete.setBounds(240, 220, 100, 25);
btnUpdate.setBounds(240, 220, 100, 25);
JScrollPane pane = new JScrollPane(table);
pane.setBounds(0, 0, 880, 200);
frame.setLayout(null);
frame.add(pane);
frame.add(btnAdd);
frame.add(btnDelete);
frame.add(btnUpdate);
Object[] row = new Object[4];
btnAdd.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
row[0]=textId.getText();
row[1]=textFname.getText();
row[2]=textLname.getText();
row[3]=textAge.getText();
model.addRow(row);
}
});
btnDelete.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int i=table.getSelectedRow();
if(i>=0){
model.removeRow(i);
}
else{
System.out.println("Delete error");
}
}
});
table.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
int i = table.getSelectedRow();
textId.setText(model.getValueAt(i, 0).toString());
textFname.setText(model.getValueAt(i, 1).toString());
textLname.setText(model.getValueAt(i, 2).toString());
textAge.setText(model.getValueAt(i, 3).toString());
}
});
btnUpdate.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
int i = table.getSelectedRow();
if(i>=0){
model.setValueAt(textId.getText(), i, 0);
model.setValueAt(textFname.getText(), i, 1);
model.setValueAt(textLname.getText(), i, 2);
model.setValueAt(textAge.getText(), i, 3);
}
else{
System.out.println("Update Error");
}
}
});
frame.setSize(900, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}