Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unable to get row index in JTable

 
Rajapragada Dharani
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic