我有两个具有相同标题名称和列数的 JTable。现在,当我单击合并按钮时,我想创建第三个 JTable,其中包含这两个 JTable 的所有值。请建议我任何答案。
问问题
2115 次
2 回答
5
您可以创建一个代理模型,允许您向其附加 1 个或多个表模型。
然后,您将覆盖所需的方法以通过代理模型将请求从表传递到其他表。
主要问题是在模型之间映射行索引。
更新了示例
这是一个非常基本的例子......
public class ProxyTableModelTest {
public static void main(String[] args) {
new ProxyTableModelTest();
}
public ProxyTableModelTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final PersonTableModel modelA = new PersonTableModel();
modelA.add(new Person("Guy", "Eichler", getDateOfBirth()));
modelA.add(new Person("Jamie", "Weishaar", getDateOfBirth()));
modelA.add(new Person("Jamie", "Sinkler", getDateOfBirth()));
modelA.add(new Person("Lorrie", "Collelo", getDateOfBirth()));
modelA.add(new Person("Hugh", "Rolls", getDateOfBirth()));
modelA.add(new Person("Tyrone", "Bogen", getDateOfBirth()));
modelA.add(new Person("Jamie", "Vandine", getDateOfBirth()));
modelA.add(new Person("Max", "Flippin", getDateOfBirth()));
modelA.add(new Person("Christian", "Folson", getDateOfBirth()));
modelA.add(new Person("Neil", "Ralphs", getDateOfBirth()));
final PersonTableModel modelB = new PersonTableModel();
modelB.add(new Person("Jamie", "Santillanes", getDateOfBirth()));
modelB.add(new Person("Carmella", "Leverich", getDateOfBirth()));
modelB.add(new Person("Mathew", "Valade", getDateOfBirth()));
modelB.add(new Person("Louisa", "Smead", getDateOfBirth()));
modelB.add(new Person("Nelson", "Vails", getDateOfBirth()));
modelB.add(new Person("Harriett", "Tengan", getDateOfBirth()));
modelB.add(new Person("Liza", "Uhler", getDateOfBirth()));
modelB.add(new Person("Ashlee", "Forbush", getDateOfBirth()));
modelB.add(new Person("Guy", "Kluender", getDateOfBirth()));
modelB.add(new Person("Eve", "Cooter", getDateOfBirth()));
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
JPanel controlsA = new JPanel();
JButton addA = new JButton("Add");
addA.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
modelA.add(new Person("A", "A", getDateOfBirth()));
}
});
controlsA.add(addA);
JPanel controlsB = new JPanel();
JButton addB = new JButton("Add");
addB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
modelB.add(new Person("B", "B", getDateOfBirth()));
}
});
controlsB.add(addB);
JPanel groupA = new JPanel(new BorderLayout());
groupA.add(new JScrollPane(new JTable(modelA)));
groupA.add(controlsA, BorderLayout.SOUTH);
JPanel groupB = new JPanel(new BorderLayout());
groupB.add(new JScrollPane(new JTable(modelB)));
groupB.add(controlsB, BorderLayout.SOUTH);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridy = 0;
gbc.gridx = 0;
frame.add(groupA, gbc);
gbc.gridx++;
frame.add(groupB, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(new JScrollPane(new JTable(new ProxyTableModel(modelA, modelB))), gbc);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ProxyTableModel extends AbstractTableModel implements TableModelListener {
private List<TableModel> models;
private int rowCount;
public ProxyTableModel() {
models = new ArrayList<>(3);
}
public ProxyTableModel(TableModel... models) {
this();
for (TableModel model : models) {
add(model);
}
System.out.println("rowCount = " + rowCount);
}
protected int getRowOffset(TableModel model) {
int rowOffset = 0;
for (TableModel proxy : models) {
if (proxy.equals(model)) {
break;
} else {
rowOffset += proxy.getRowCount();
}
}
return rowOffset;
}
protected TableModel getModelForRow(int row) {
TableModel model = null;
int rowOffset = 0;
for (TableModel proxy : models) {
if (row >= rowOffset && row < (rowOffset + proxy.getRowCount())) {
model = proxy;
break;
}
rowOffset += proxy.getRowCount();
}
return model;
}
protected void updateRowCount() {
rowCount = 0;
for (TableModel proxy : models) {
rowCount += proxy.getRowCount();
}
}
public void add(TableModel model) {
int firstRow = getRowCount();
int lastRow = firstRow + model.getRowCount() - 1;
models.add(model);
model.addTableModelListener(this);
updateRowCount();
fireTableRowsInserted(firstRow, lastRow);
}
public void remove(TableModel model) {
if (models.contains(model)) {
int firstRow = getRowOffset(model);
int lastRow = firstRow + model.getRowCount() - 1;
model.removeTableModelListener(this);
models.remove(model);
updateRowCount();
fireTableRowsDeleted(firstRow, lastRow);
}
}
@Override
public int getRowCount() {
return rowCount;
}
@Override
public int getColumnCount() {
int columnCount = 0;
if (models.size() > 0) {
columnCount = models.get(0).getColumnCount();
}
return columnCount;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
TableModel model = getModelForRow(rowIndex);
if (model == null) {
System.out.println("rowIndex = " + rowIndex);
System.out.println("rowCount = " + rowCount);
TableModel test = getModelForRow(rowIndex);
}
int rowOffset = getRowOffset(model);
rowIndex -= rowOffset;
return model.getValueAt(rowIndex, columnIndex);
}
@Override
public Class<?> getColumnClass(int columnIndex) {
Class clazz = String.class;
if (models.size() > 0) {
clazz = models.get(0).getColumnClass(columnIndex);
}
return clazz;
}
@Override
public String getColumnName(int column) {
String name = null;
if (models.size() > 0) {
name = models.get(0).getColumnName(column);
}
return name;
}
@Override
public void tableChanged(TableModelEvent e) {
int type = e.getType();
if (type == TableModelEvent.INSERT || type == TableModelEvent.DELETE || type == TableModelEvent.UPDATE) {
int firstRow = e.getFirstRow();
int lastRow = e.getLastRow();
TableModel model = getModelForRow(firstRow);
int rowOffset = getRowOffset(model);
firstRow += rowOffset;
lastRow += rowOffset;
updateRowCount();
TableModelEvent proxy = new TableModelEvent(this, firstRow, lastRow, e.getColumn(), type);
fireTableChanged(e);
} else {
updateRowCount();
TableModelEvent proxy = new TableModelEvent(this, e.getFirstRow(), e.getLastRow(), e.getColumn(), type);
fireTableChanged(e);
}
}
}
protected Date getDateOfBirth() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, random(0, 11));
cal.set(Calendar.DATE, random(1, 31));
cal.set(Calendar.YEAR, random(1900, 2012));
return cal.getTime();
}
protected int random(int min, int max) {
return min + ((int) Math.round(Math.random() * (max - min)));
}
public class PersonTableModel extends AbstractTableModel {
private List<Person> people;
public PersonTableModel() {
people = new ArrayList<>(25);
}
public void add(Person person) {
people.add(person);
int index = people.size() - 1;
fireTableRowsInserted(index, index);
}
public void remove(Person person) {
int index = people.indexOf(person);
if (index > -1) {
people.remove(index);
fireTableRowsDeleted(index, index);
}
}
@Override
public int getRowCount() {
return people.size();
}
@Override
public int getColumnCount() {
return 3;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
Person person = people.get(rowIndex);
switch (columnIndex) {
case 0:
value = person.getFirstName();
break;
case 1:
value = person.getLastName();
break;
case 2:
value = person.getDateOfBirth();
}
return value;
}
@Override
public String getColumnName(int column) {
String value = null;
switch (column) {
case 0:
value = "First name";
break;
case 1:
value = "Last name";
break;
case 2:
value = "Date of birth";
}
return value;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
Class value = null;
switch (columnIndex) {
case 0:
case 1:
value = String.class;
break;
case 2:
value = Date.class;
break;
}
return value;
}
}
public class Person {
private String firstName;
private String lastName;
private Date dateOfBirth;
public Person(String firstName, String lastName, Date dateOfBirth) {
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
}
于 2013-02-09T05:09:20.047 回答
5
您将希望将两个 JTable 的 TableModel 中保存的数据组合起来,并将它们添加到您的第三个 JTable 保存的第三个 TableModel 中。如果表结构都相同,您可以遍历模型的行,将每一行添加到新的 TableModel。
您可以在Swing JTable 教程中阅读 JTables 和 TableModels 。
于 2013-02-09T05:02:48.587 回答