The Artima Developer Community
Sponsored Link

Java Answers Forum
Urgent Help needed to remove row in JTable!

3 replies on 1 page. Most recent reply: Nov 24, 2004 9:03 PM by Amol Brid

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
J.S

Posts: 2
Nickname: jay11
Registered: Nov, 2004

Urgent Help needed to remove row in JTable! Posted: Nov 23, 2004 9:36 PM
Reply to this message Reply
Advertisement
Hi,

I have a customized table model class that extends AbstractTableModel. Does anyone know how to use removeRow() to remove the record in the JTable?

(I tried extending DefaultTableModel in order to use the method removeRow(int row). However, it always throws ArrayIndexOutOfBoundsException: 3>=0. 3 here is my row index. No matter what the row index is, it's alway that row index number >=0 in the exception thrown out. )


Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Urgent Help needed to remove row in JTable! Posted: Nov 24, 2004 4:37 AM
Reply to this message Reply
Hi,

I think you might not have added the objects in the model or you might be adding in some other model.

I have used this many times and it works.

Can you post code snippet, so that i can help you.

Regards,
Amol Brid.

J.S

Posts: 2
Nickname: jay11
Registered: Nov, 2004

Re: Urgent Help needed to remove row in JTable! Posted: Nov 24, 2004 8:18 AM
Reply to this message Reply
Hi,

really appreciate it! Here's my table model class. It still extends AbstractTableModel since I always got the ArrayOutOfBoundsException when extending DefaultTableModel. (But now I cannot use that removeRow()method)


public class DBTableModel extends AbstractTableModel
{
public static Vector results;
int colCount = 0;
String[] headers;
Connection con = null;
Statement stmt = null;
ResultSet queryResults = null;

public DBTableModel(Connection conn)
{
results = new Vector();
setConnection(conn);

}

public void setConnection(Connection conn) {
con = conn;
}

public String getColumnName(int i) {
return headers;
}

public int getColumnCount() { return colCount; }

public int getRowCount() { return results.size();}

public Object getValueAt(int row, int col)
{
return ((String[])results.elementAt(row))[col];
}

public boolean isCellEditable(int row, int col) {
return true;
}

public void setValueAt(Object value, int row,
int col)
{
((String[])results.elementAt(row))[col] =
value.toString();
fireTableCellUpdated(row, col);
System.out.println("New value @ROW-"+(row+1)
+", COLUMN-"+(col+1)
+": "+((String[])
results.elementAt(row))[col]);
}

public Vector getVector() {
return results;
}

public void setQuery(String q, Connection conn)
{
results = new Vector();
try {
queryResults = ReadDB3.searchQuery(q, con);
ResultSetMetaData meta =
queryResults.getMetaData();
colCount = meta.getColumnCount();
headers = new String[colCount];
for (int h=1; h <= colCount; h++)
headers[h-1] = meta.getColumnName(h);

while (queryResults.next()) {
String[] record = new String[colCount];
for (int i=0; i < colCount; i++) {
record = queryResults.getString(i +
1);
}
results.addElement(record);
}
fireTableChanged(null);
} catch(Exception e) {
results = new Vector();
e.printStackTrace();
}
}

}

Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Urgent Help needed to remove row in JTable! Posted: Nov 24, 2004 9:03 PM
Reply to this message Reply
hi,

If i am not wrong, you are using setQuery() method to populate the data from the database.

For removing a row from the model, you need to write a method in your DBTableMode class which will remove a single row. The below given code snippet will do this:
public void removeRow(int row)
{
	results.removeElementAt(row);
	// write code for notifying the view and other listeners
}

To call this method:
JTable table = new JTable( new DBTableMode(...));
.
.
.
// remove the second row
((DBTableModel)table.getModel()).removeRow(2);


In case of DefaultTableModel, if you are using same setQuery() method to populate data then the exception is excepted as DefaultTableModel using its own datastructure to store table informaiton. You can override the removeRow(int) method and write the same code as above.

Regards,
Amol Brid.

Flat View: This topic has 3 replies on 1 page
Topic: two question for java Previous Topic   Next Topic Topic: Rounding error with double

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use