Reputation: 127
I have two JTable having same header name and number of columns. Now I want to create a third JTable containing all values of this two JTables when i click on merge Button. Please suggest me any answer.
Upvotes: 0
Views: 2448
Reputation: 347334
You could create a proxy model the allows you to append 1 or more table models to it.
You would then override the required methods to pass the request from the table to those other tables via the proxy model.
The major problem would be mapping row indices between the models.
Updated with example
This is a VERY basic example...
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;
}
}
}
Upvotes: 5
Reputation: 285440
You will want to combine the data held in the two JTable's TableModels and add them to a third TableModel that is held by your third JTable. If the table structures are all the same, you could iterate through the models' rows adding each row to the new TableModel.
You can read up on JTables and TableModels at the Swing JTable Tutorial.
Upvotes: 5