making a few rows in jTable uneditable

Hi

I have a simple problem :)

1) i send some data, read from jTable, to sever and wait for reply

2) i recieve data from server.. have to put that data to jtable in a specific column

3) now i have to make the whole row in jtable uneditable

1) and 2) are done

i need help with only 3)

Thanks

[338 byte] By [abhilash.l.la] at [2007-11-27 4:54:00]
# 1

There is a method called boolean isCellEditable(int rowIndex,int columnIndex) in TableModel interface. create a custom TableModel class and implemet this method like that:

boolean isCellEditable(int rowIndex, int columnIndex)

{

if (rowIndex == <index-you-want-1> || rowIndex == <index-you-want-2>)

{

return false;

}

return true;

}

if you want to disable editability of row at run-time, you can declare an integer array in your table model implementation class and add indices to that array while run-time. then change the method isCellEditable

implementation to loop through that array.

Good Luck

Ahmad Elsafty

NourElsaftya at 2007-7-12 10:08:34 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks NourElsafty,

I must tell you that i am using netbeans

and i have to display that jtable in an applet

also users can enter data into the jtable directly which i would like to selectively allow/stop

i wud like to do something like jtable.seteditable(false,row,col) or on similar lines... similar to text fields

am new to java.. sorry if i didnt completely understand your reply

abhilash.l.la at 2007-7-12 10:08:35 > top of Java-index,Java Essentials,Java Programming...
# 3

ok

Create a separate class in your netbeans project, this is the minimum requirements:

import javax.swing.table.DefaultTableModel;

import java.util.Vector;

public class MyTableModel extends DefaultTableModel{

private Vector<Integer> uneditableIndices;

public MyTableModel()

{

uneditableIndices = new Vector<Integer>();

}

public boolean isCellEditable(int row, int column)

{

for (int index : uneditableIndices)

if (row == index)

return false;

return true;

}

public void addUneditableRowIndex(int rowIndex)

{

uneditableIndices.add(rowIndex);

}

public int removeUneditableRowIndex(int rowIndex)

{

uneditableIndices.remove(rowIndex);

}

}

I did not check the previous code, so it needs to be checked.

In the Table Properties window select the previous class to be the default model for your table.

then you can handle adding new indexes to the indexes vector while run-time.

PLEASE FEED ME BACK.

Good Luck.

Ahmad Elsafty

NourElsaftya at 2007-7-12 10:08:35 > top of Java-index,Java Essentials,Java Programming...
# 4

sorry for late reply

i have exams going on.. so reading for tat :(

anyways.. will try and let you know

also i have set column headers and the number of rows/columns fixed through netbeans

i have also given column headers and the datatype the column shall take in.

now will using this as the table model change all that?

i tried looking at the jtable>properties>model window

i couldnt figure out how to get tablemodel changed

abhilash.l.la at 2007-7-12 10:08:35 > top of Java-index,Java Essentials,Java Programming...