Det undgår Vector, men der er stadig Object[].
Jeg prøvede at lave en lidt mere moderne table model:
import java.awt.BorderLayout;
import java.awt.Color;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.AbstractTableModel;
public class ModernTableUsage extends JFrame {
public static class BeanTableModel<T> extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private List<T> data;
private List<Method> getter = new ArrayList<Method>();
private List<Method> setter = new ArrayList<Method>();
public BeanTableModel(Class<T> clz, List<T> data, String... flds) {
this.data = data;
for(String fld : flds) {
try {
getter.add(clz.getDeclaredMethod("get" + fld));
setter.add(clz.getDeclaredMethod("set" + fld, String.class));
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
}
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return getter.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
try {
return getter.get(columnIndex).invoke(data.get(rowIndex));
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
} catch (InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
try {
setter.get(columnIndex).invoke(data.get(rowIndex), aValue);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
}
public static class Data {
private int f1;
private String f2;
public Data() {
this(0, "");
}
public Data(int f1, String f2) {
super();
this.f1 = f1;
this.f2 = f2;
}
public int getF1() {
return f1;
}
public void setF1(int f1) {
this.f1 = f1;
}
public void setF1(String f1) { // <---- needed for default cell editor !!!!
this.f1 = Integer.parseInt(f1);
}
public String getF2() {
return f2;
}
public void setF2(String f2) {
this.f2 = f2;
}
@Override
public String toString() {
return String.format("(%s,%s)", f1, f2);
}
}
private static final long serialVersionUID = 1L;
public ModernTableUsage() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Moden table usage");
setLayout(new BorderLayout());
List<Data> data = new ArrayList<Data>(List.of(new Data(1, "A"), new Data(2, "BB"), new Data(3, "CCC")));
BeanTableModel<Data> model = new BeanTableModel<Data>(Data.class, data, "F1", "F2");
JTable table = new JTable(model);
table.setShowGrid(true);
table.setGridColor(Color.BLACK);
table.setBorder(new LineBorder(Color.BLACK, 2));
JPanel wrapper = new JPanel();
wrapper.setBorder(new EmptyBorder(10, 10, 10, 10));
wrapper.add(table);
add(wrapper, BorderLayout.CENTER);
JPanel buttons = new JPanel();
buttons.setBorder(new EmptyBorder(10, 10, 10, 10));
JButton test = new JButton("Test");
test.addActionListener(e -> JOptionPane.showMessageDialog(null, data.stream().map(Data::toString).collect(Collectors.joining("\r\n"))));
buttons.add(test);
JButton add = new JButton("Add row");
add.addActionListener(e -> { data.add(new Data()); model.fireTableDataChanged(); });
buttons.add(add);
add(buttons, BorderLayout.SOUTH);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new ModernTableUsage();
f.setVisible(true);
}
});
}
}