'I can not change JTable Header value or data?
I am using java swing to code an simple desktop application but when i config jtable, it's not working:
public MainFrame() {
initComponents();
try {
//add header for jtable
String column_names[] = {"Code", "Name", "Phone", "Email", "Address", "Department"};
DefaultTableModel table_model = new DefaultTableModel(column_names, 0);
tbEmployee = new JTable(table_model);
//create fake data
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("code 1", "name 1", "phone 1", "email 1", "address 1", "department 1"));
employees.add(new Employee("code 2", "name 2", "phone 2", "email 2", "address 2", "department 2"));
//add list data to table
for (int i = 0; i < employees.size(); i++) {
String code = employees.get(i).getCode();
String name = employees.get(i).getName();
String phone = employees.get(i).getPhone();
String email = employees.get(i).getEmail();
String address = employees.get(i).getAddress();
String department = employees.get(i).getDepartment();
Object[] data = {code, name, phone, email, address, department};
table_model.addRow(data);
}
tbEmployee = new JTable(table_model);
//setting header color`
JTableHeader header = tbEmployee.getTableHeader();
header.setBackground(Color.yellow);
} catch (Exception e) {
e.printStackTrace();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
