Get Selected Rows in Jtable Using Abstracttablemodel

getting selected row through AbstractTableModel

The TableModel only concerns itself with the data, the ListSelectionModel concerns itself with what is currently selected, so, no you can't get the selected row from the TableModel.

Get Selected Rows in JTable using AbstractTableModel

In the example below, the TableModel updates a Set<Integer> checked in the implementation of setValueAt(). The model of an adjacent JList listens to the table's model and displays the currently selected row numbers. The example assumes that the number of selected rows is small compared to the number of rows. Note the use of TreeSet, whose iterator retains the natural order of the elements.

image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

/** @see http://stackoverflow.com/a/13919878/230513 */
public class CheckTable {

private static final CheckModel model = new CheckModel(5000);
private static final JTable table = new JTable(model) {

@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(150, 300);
}
};

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
JFrame f = new JFrame("CheckTable");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(1, 0));
f.add(new JScrollPane(table));
f.add(new DisplayPanel(model));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}

private static class DisplayPanel extends JPanel {

private DefaultListModel dlm = new DefaultListModel();
private JList list = new JList(dlm);

public DisplayPanel(final CheckModel model) {
super(new GridLayout());
this.setBorder(BorderFactory.createTitledBorder("Checked"));
this.add(new JScrollPane(list));
model.addTableModelListener(new TableModelListener() {

@Override
public void tableChanged(TableModelEvent e) {
dlm.removeAllElements();
for (Integer integer : model.checked) {
dlm.addElement(integer);
}
}
});
}
}

private static class CheckModel extends AbstractTableModel {

private final int rows;
private List<Boolean> rowList;
private Set<Integer> checked = new TreeSet<Integer>();

public CheckModel(int rows) {
this.rows = rows;
rowList = new ArrayList<Boolean>(rows);
for (int i = 0; i < rows; i++) {
rowList.add(Boolean.FALSE);
}
}

@Override
public int getRowCount() {
return rows;
}

@Override
public int getColumnCount() {
return 2;
}

@Override
public String getColumnName(int col) {
return "Column " + col;
}

@Override
public Object getValueAt(int row, int col) {
if (col == 0) {
return row;
} else {
return rowList.get(row);
}
}

@Override
public void setValueAt(Object aValue, int row, int col) {
boolean b = (Boolean) aValue;
rowList.set(row, b);
if (b) {
checked.add(row);
} else {
checked.remove(row);
}
fireTableRowsUpdated(row, row);
}

@Override
public Class<?> getColumnClass(int col) {
return getValueAt(0, col).getClass();
}

@Override
public boolean isCellEditable(int row, int col) {
return col == 1;
}
}
}

Java JTable getting the data of the selected row

http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html

You will find these methods in it:

getValueAt(int row, int column)
getSelectedRow()
getSelectedColumn()

Use a mix of these to achieve your result.

Get selected rows in JTable

One option is to print the data vector of the selected row:

DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
System.out.println(model.getDataVector().get(selectedRowIndex));

Or you can print the cell values of the row one after another:

TableModel model = jTable1.getModel();
for(int i = 0; i < model.getColumnCount(); i++){
if(i > 0){
System.out.println(", ");
}
System.out.println(model.getValueAt(selectedRowIndex, selectedColumnIndex));
}
System.out.println();

JTable row selection after TableModel update

Decision is simple: we should use Swing EventQueue:

final int selectedRow = 0;
// Save selected row table
tableList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
selectedRow = e.getFirstIndex();
}
});

// Restore selected raw table
model.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (selectedRow >= 0) {
tableList.addRowSelectionInterval(index, index);
}
}
});
}
});

Get selected row of JTable when itemStateChanged in JComboBox

The problem is that you are using the wrong listener on the wrong component. You should not be adding a listener to the combo box. The point of using an editor is to edit the data and update the TableModel. You should not be referencing the combo box for further processing.

So instead you should be adding a TableModelListener to the TableModel. Then a TableModelEvent will be generated whenever the value in column 2 is changed. The TableModelEvent will contain the row/column information of the cell that was changed.

You can check out: JTable -> TableModeListener for a basic example of using a TableModelListener. In your case you check if the second column changed and then you update your map.

Also note you are using the convertRowIndexToModel() method incorrectly:

selectedRow = jTable.convertRowIndexToModel(selectedRow);
...
String user = (String) jTable.getValueAt(selectedRow, 0);

First of all you only need to worry about coverting the row index if the table view is sorted or filtered. In the code provided you don't do either so there is no need to convert the index.

However, if you ever did sort of filter the view then you would need convert the view row to the model row and then you must access the data from the model, not the view. So the code would be:

String user = (String) jTable.getModel().getValueAt(selectedRow, 0);


Related Topics



Leave a reply



Submit