Reputation: 5
What I'm trying to do:
I want to have two JTables side by side, and when you make a selection on the left JTable you see more detailed information about that row in the JTable to the right.
What's my problem?:
I have no idea how to implement this. I have tried using the addListSelectionListener, but I'm unsure on how I can use a listener from one table to interact with a completely different table. Such as, having the listener replace table2 with a new table that has completely new data.
I've included an SSCCE which shows the gist of what I did. I would appreciate any help, as I've been scouring the internet trying to piece this problem together.
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
public class TableInteraction extends JPanel
{
private JTable table1;
private JTable table2;
public TableInteraction()
{
super();
String[][] tableOneData = {{"Kathy", "Smith"}};
String[][] tableTwoData = {{"Kathy", "Smith"}};
String[][] tableThreeData = {{"Matt", "Bird"}};
setLayout(new BorderLayout());
FirstTableModel model1 = new FirstTableModel(tableOneData);
table1 = new JTable(model1);
table1.setPreferredScrollableViewportSize(new Dimension(500,200));
table1.setFillsViewportHeight(true);
table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table1.setRowSelectionInterval(0,0);
JScrollPane scrollPane1 = new JScrollPane(table1);
SecondTableModel model2 = new SecondTableModel(tableTwoData);
table2 = new JTable(model2);
table2.setPreferredScrollableViewportSize(new Dimension(500,200));
table2.setFillsViewportHeight(true);
table2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane2 = new JScrollPane(table2);
table1.getSelectionModel().addListSelectionListener(
new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent event)
{
int index = table1.getSelectedRow();
}
});
add(scrollPane1, BorderLayout.WEST);
add(scrollPane2, BorderLayout.EAST);
}
public class FirstTableModel extends AbstractTableModel
{
private String[] columnNames = {"First Name", "Last Name"};
private String[][] data;
public FirstTableModel(String[][] data)
{
this.data = data;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
public class SecondTableModel extends AbstractTableModel
{
private String[] columnNames = {"First Name", "Last Name"};
private String[][] data;
public SecondTableModel(String[][] data)
{
this.data = data;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("TableFilterDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableInteraction newContentPane = new TableInteraction();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Upvotes: 0
Views: 470
Reputation: 208984
Consider this (extension of my comment)
private String[] columnNames1 = {"First Name", "Last Name"};
private String[][] data1 = {{"Kathy", "Smith"},{"John","Doe"}};
private String[] columnNames2 = {"First Name", "Last Name"};
private String[][] data2 = {{"Kathy", "Smith"},{"John","Doe"}};
DefaultTableModel tableModel1 = new DefaultTableModel(data1, columnNames1);
DefaultTableModel tableModel2 = new DefaultTableModel(data2, columnNames2);
JTable jTable1 = new JTable(tableModel1);
JTable jTable2 = new JTable(tableModel1);
jTable1.getSelectionModel().addListSelectionListener(
new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent event)
{
int index = jTable1.getSelectedRow();
jTable2.removeRow(index);
}
});
When a row from table1 is selected, The same row from table2 is deleted. This is a bizarre example that would not work without some null checks, but it shows how two tables can interact using the table model
Edit: with OP added comment
If you want to set the second table based on the first table, you can do something like this
jTable2.setModel(jTable1.getModel());
Also, you should place you listener AFTER creating tables and models, so that you can can use the model. If they haven't been declared yet, you cant use them in the listener, unless they are declared as global
Upvotes: 1