• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

unable to get row index in JTable

 
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);
}
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • 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.
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic