File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes unable to get row index in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "unable to get row index in JTable" Watch "unable to get row index in JTable" New topic

unable to get row index in JTable

Rajapragada Dharani

Joined: Oct 23, 2003
Posts: 15
HI All,
I'am having problem in getting the index of row once a selection is made.I have implemented all the required methods but still problem persists.

The problem is in the last line


were it throws an exception:
"java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.get("

Here's the brief code I have attached please suggest me some solution.

Thanks in advance,

public class myTableModel extends AbstractTableModel {

private Vector datalist;
private String[] columns = {"Hostname", "Serial Number","Registration"};

public myTableModel() {

public myTableModel(Vector l) {
datalist = l;

public String getColumnName(int col) {
return columns[col];

public int getColumnCount() {
return columns.length;

public int getRowCount() {
return datalist.size();

public Object getValueAt(int row, int col) {

Vector rowList = (Vector)datalist.get(row);

if(rowList.firstElement()!=null) {

switch (col) {

case 0:
return (String) rowList.elementAt(0);
case 1:
return (String) rowList.elementAt(1);
case 2:
return (String) rowList.elementAt(2);
return null;
}else return null;


// This one method decides exactly which cells are editable.
public boolean isCellEditable(int row, int col) {
switch (col) {

case 0: //Hostname
return false;
case 1: //Product Family
return false;
case 2: //Serial Number
return false;
return false;

public Class getColumnClass(int col) {
switch (col) {

case 0: //Hostname
return String.class;
case 1: //Product Family
return String.class;
case 2: //Serial Number
return String.class;
return null;

public void removeRowAt(int row) {

ArrayList removeRow = (ArrayList) datalist.remove(row);



//This is from my main class which implements ListSelectionListener

myTableModel myTableModel;
JTable myTable;

Vector tableData = new Vector();
//this returns all the data to be shown in table
tableData = returnList();

myTableModel = new myTableModel(tableData);
myTable = new JTable(myTableModel);


scrollList = new JScrollPane(myTable); //panelScroller);
ListSelectionModel mySM = myTable.getSelectionModel();


public void valueChanged(ListSelectionEvent mySelectionEvent)
if (mySelectionEvent.getValueIsAdjusting()) return;

ListSelectionModel lsm = (ListSelectionModel)mySelectionEvent.getSource();
//Here i wanted to do same action if selection is empty.
String hostName;
hostName = (String)myTableModel.getValueAt(myTable.getSelectedRow(),0);
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

One problem with this code is that
you name your variable myTableModel the same as the class myTableModel.
The class should start with caps: MyTableModel
so it does not conflict with the instance name myTableModel.

Please post the rest of your main class if that doesn't help.
I agree. Here's the link:
subject: unable to get row index in JTable
It's not a secret anymore!