Code: Select all
JTable table = new JTable(); // actually, a custom JTable type, but it's not the point
// we can't rely on auto-created columns and add them with table.addColumn()
table.addColumn(TableColumns.code());
table.addColumn(TableColumns.name());
Was ist der richtige Weg, Zeilen einzufügen, vorzugsweise ohne Duplikate von Spaltennamen oder Spaltenanzahl, wie im folgenden Snippet?
Code: Select all
private static void fillTable() {
List doctors = createDoctors();
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
// --------- duplication, bad -------------
model.setColumnCount(2);
model.setColumnIdentifiers(new Object[]{"Code", "Name"});
// ----------------------------------------
for (Doctor doctor : doctors) {
String code = doctor.getCode();
String name = doctor.getName();
model.addRow(new Object[]{code, name});
}
}
Code: Select all
private static Component createTable() {
// you can adjust table initialization if necessary
table = new JTable() {
@Override
public void addColumn(TableColumn aColumn) {
// important to avoid out-of-bounds exceptions during rendering
aColumn.setModelIndex(getColumnCount());
super.addColumn(aColumn);
}
};
table.setAutoCreateColumnsFromModel(false);
List columns = Arrays.asList(TableColumns.code(), TableColumns.name());
columns.forEach(table::addColumn);
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setColumnCount(table.getColumnCount());
model.setColumnIdentifiers(columns.stream().map(TableColumn::getIdentifier).toArray());
return table;
}
Vollständige Demo:
Code: Select all
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.util.ArrayList;
import java.util.List;
public class SimpleTableDemo {
static JTable table;
static JButton fillButton;
static JButton clearButton;
public static void main(String[] args) {
Container mainPanel = createMainPanel();
JFrame frame = new JFrame("Table Demo");
frame.setContentPane(mainPanel);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static Container createMainPanel() {
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(createScroller());
mainPanel.add(createButtonPanel(), BorderLayout.SOUTH);
return mainPanel;
}
private static Component createScroller() {
JScrollPane scroller = new JScrollPane();
scroller.setViewportView(createTable());
return scroller;
}
private static Component createTable() {
// you can adjust table initialization if necessary
table = new JTable();
table.addColumn(TableColumns.code());
table.addColumn(TableColumns.name());
return table;
}
private static Component createButtonPanel() {
// you can adjust table initialization if necessary
JPanel panel = new JPanel();
panel.add(createFillButton());
panel.add(createEmptyButton());
return panel;
}
private static Component createFillButton() {
fillButton = new JButton("Fill");
fillButton.addActionListener(e -> fillTable());
fillButton.addActionListener(e -> {
fillButton.setEnabled(false);
clearButton.setEnabled(true);
});
return fillButton;
}
private static Component createEmptyButton() {
clearButton = new JButton("Clear");
clearButton.setEnabled(false);
clearButton.addActionListener(e -> clearTable());
clearButton.addActionListener(e -> {
clearButton.setEnabled(false);
fillButton.setEnabled(true);
});
return clearButton;
}
private static void fillTable() {
List doctors = createDoctors();
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
// --------- duplication, bad -------------
model.setColumnCount(2);
model.setColumnIdentifiers(new Object[]{"Code", "Name"});
// ----------------------------------------
for (Doctor doctor : doctors) {
String code = doctor.getCode();
String name = doctor.getName();
model.addRow(new Object[]{code, name});
}
}
private static List createDoctors() {
List doctors = new ArrayList();
doctors.add(Doctor.of("1", "Maria"));
doctors.add(Doctor.of("2", "Patrick"));
return doctors;
}
private static void clearTable() {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
}
}
Code: Select all
public class Doctor {
private String code;
private String name;
private Doctor() {
}
public static Doctor of(String code, String name) {
Doctor doctor = new Doctor();
doctor.code = code;
doctor.name = name;
return doctor;
}
public String getName() {
return name;
}
public String getCode() {
return code;
}
}
Code: Select all
import javax.swing.table.TableColumn;
// for simplicity, no custom TableColumn types are used here
public class TableColumns {
private TableColumns() {
}
public static TableColumn code() {
TableColumn column = new TableColumn();
column.setIdentifier("CODE");
column.setHeaderValue("Code");
return column;
}
public static TableColumn name() {
TableColumn column = new TableColumn();
column.setIdentifier("NAME");
column.setHeaderValue("Name");
return column;
}
}
Mobile version