import javax.swing.*; import java.awt.*; public class ExampleTable extends JFrame { private JTable table; public ExampleTable() { super("Example Table"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); // Create data and column headers String[] headers = {"Name", "Age", "Gender"}; Object[][] data = { {"John", 25, "Male"}, {"Emily", 30, "Female"}, {"Alex", 18, "Male"} }; // Create table using data and headers table = new JTable(data, headers); add(new JScrollPane(table), BorderLayout.CENTER); pack(); setLocationRelativeTo(null); setVisible(true); } public void updateTable() { // Make changes to table model or rendering properties table.getColumnModel().getColumn(1).setPreferredWidth(50); // Refresh table UI table.updateUI(); } public static void main(String[] args) { ExampleTable example = new ExampleTable(); example.updateTable(); // Call updateUI method } }
import javax.swing.*; import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellRenderer; import java.awt.*; import java.util.ArrayList; public class CustomTable extends JFrame { private JTable table; private ArrayListIn this example, a custom JTable is created using a custom table model, cell renderers and editors. The updateTable method is called to modify the data model and refresh the UI using the updateUI method. Package library: javax.swing, javax.swing.table.people; public CustomTable() { super("Custom Table"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); // Create data and column headers String[] headers = {"Name", "Age", "Gender"}; people = new ArrayList<>(); people.add(new Person("John", 25, "Male")); people.add(new Person("Emily", 30, "Female")); people.add(new Person("Alex", 18, "Male")); // Create table using data and headers, and set custom renderers/editors table = new JTable(new CustomTableModel(people, headers)); table.setDefaultRenderer(Person.class, new PersonRenderer()); table.setDefaultEditor(Person.class, new PersonEditor()); add(new JScrollPane(table), BorderLayout.CENTER); pack(); setLocationRelativeTo(null); setVisible(true); } public void updateTable() { // Make changes to table model or rendering properties people.add(new Person("Sophie", 22, "Female")); // Refresh table UI table.updateUI(); } public static void main(String[] args) { CustomTable custom = new CustomTable(); custom.updateTable(); // Call updateUI method } private static class Person { private final String name; private final int age; private final String gender; public Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public String getName() { return name; } public int getAge() { return age; } public String getGender() { return gender; } public String toString() { return name; } } private static class CustomTableModel extends javax.swing.table.AbstractTableModel { private final ArrayList people; private final String[] headers; public CustomTableModel(ArrayList people, String[] headers) { this.people = people; this.headers = headers; } public int getColumnCount() { return headers.length; } public int getRowCount() { return people.size(); } public String getColumnName(int col) { return headers[col]; } public Object getValueAt(int row, int col) { Person person = people.get(row); Object value = null; switch (col) { case 0: value = person.getName(); break; case 1: value = person.getAge(); break; case 2: value = person.getGender(); break; } return value; } public Class getColumnClass(int col) { return Person.class; } public boolean isCellEditable(int row, int col) { return true; } public void setValueAt(Object value, int row, int col) { Person person = people.get(row); switch (col) { case 0: person.name = value.toString(); break; case 1: person.age = Integer.parseInt(value.toString()); break; case 2: person.gender = value.toString(); break; } fireTableCellUpdated(row, col); } } private static class PersonRenderer implements TableCellRenderer { private final JPanel panel; private final JLabel nameLabel; private final JLabel ageLabel; private final JLabel genderLabel; public PersonRenderer() { panel = new JPanel(new GridLayout(1, 3)); nameLabel = new JLabel(); ageLabel = new JLabel(); genderLabel = new JLabel(); panel.add(nameLabel); panel.add(ageLabel); panel.add(genderLabel); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Person person = (Person) value; nameLabel.setText(person.getName()); ageLabel.setText(String.valueOf(person.getAge())); genderLabel.setText(person.getGender()); return panel; } } private static class PersonEditor extends AbstractCellEditor implements TableCellEditor { private Person person; private final JPanel panel; private final JTextField nameField; private final JSpinner ageSpinner; private final JComboBox genderCombo; public PersonEditor() { panel = new JPanel(new GridLayout(1, 3)); nameField = new JTextField(); ageSpinner = new JSpinner(); genderCombo = new JComboBox<>(new String[]{"Male", "Female"}); panel.add(nameField); panel.add(ageSpinner); panel.add(genderCombo); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { person = (Person) value; nameField.setText(person.getName()); ageSpinner.setValue(person.getAge()); genderCombo.setSelectedItem(person.getGender()); return panel; } public Object getCellEditorValue() { return new Person(nameField.getText(), (int) ageSpinner.getValue(), (String) genderCombo.getSelectedItem()); } } }