aspose file tools*
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
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
Author

unable to get row index in JTable

Rajapragada Dharani
Greenhorn

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

myTableModel.getValueAt(myTable.getSelectedRow(),0);

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

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



Thanks in advance,
Rajarao.


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);
default:
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;
default:
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;
default:
return null;
}
}

public void removeRowAt(int row) {

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

}

}


****************************************************************************
//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);

myTable.setRowSelectionAllowed(true);
myTable.setColumnSelectionAllowed(false);

scrollList = new JScrollPane(myTable); //panelScroller);
myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel mySM = myTable.getSelectionModel();
mySM.addListSelectionListener(this);

myTable.addMouseListener(this);

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.
 
wood burning stoves
 
subject: unable to get row index in JTable