我有兩個JTable具有相同的頭名稱和列數。現在我想創建一個包含這兩個JTable的所有值的第三個JTable,當我單擊合併按鈕時。請給我建議任何答案。如何將兩個Jtable合併爲第三個Jtable?
0
A
回答
5
您將需要合併兩個JTable的TableModel中保存的數據,並將它們添加到由第三個JTable持有的第三個TableModel中。如果表結構完全相同,則可以遍歷模型的行,將每行添加到新的TableModel。
您可以在Swing JTable Tutorial上閱讀JTables和TableModels。
5
您可以創建一個代理模型,允許您爲其添加一個或多個表模型。
然後,您將覆蓋所需的方法,通過代理模型將請求從表中傳遞到其他表。
主要的問題是在模型之間映射行索引。
已更新,例如
這是一個非常簡單的例子...
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;
}
}
}
相關問題
- 1. 如何合併兩列JTable
- 2. Mercurial - 將兩個分支合併爲第三個新分支
- 3. SQL服務器將兩個xmls合併爲第三個
- 4. 將兩個列表合併爲第三個列表 - 錯誤
- 5. 將兩個文件合併爲第三個使用perl
- 6. 兩個水平JTable的頭,並添加分隔符的JTable
- 7. 合併兩個行篩選了一個JTable
- 8. 如何將多個第三方標記(javascript)合併爲一個
- 9. 如何將JTable中的JComboBox重置爲JComboBox的第一個值
- 10. 如何在JFrame中顯示兩個JTable
- 11. 合併兩個工作表到第三
- 12. 第三合併兩個分支
- 13. 如何將一行從JTable複製到另一個JTable中
- 14. 我們如何將JTable添加到另一個JTable?
- 15. 如何將jTable的某些部分複製到另一個jTable?
- 16. Linq將兩個類合併成第三個類?
- 17. 合併JTable中的列
- 18. JTable單元跨度合併
- 19. 將兩個SQL數據庫的兩列「合併」成第三個(有一個catch)
- 20. JTable,三維矩陣和多個面板
- 21. 迭代通過兩個並行集合並輸出第三個
- 22. 如何在第三陣列合併兩個XML列在PHP
- 23. 如何將兩列合併爲一個
- 24. 如何將兩行合併爲一個?
- 25. 如何將JRadioButton添加到兩個JTable和ButtonGroup的組中。
- 26. 我如何合併兩個DOM元素集合到第三個使用javascript
- 27. 不確定如何將對象集合添加到一個JTable
- 28. 將三個查詢合併爲一個
- 29. 將三個函數合併爲一個
- 30. 將三個字段合併爲一個
我不知道這一點非常和善告訴我任何教程 – user2042166 2013-02-09 05:59:46
這是一個聰明的解決方案。 1+ – 2013-02-09 06:04:25
@ user2042166沒有任何教程(我知道)爲這樣的事情,你必須弄清楚yoursef。如果我有時間,我會嘗試做一個例子 – MadProgrammer 2013-02-09 06:07:54