JTable select  
Author Message
pat270881





PostPosted: 2005-4-4 5:39:00 Top

java-programmer, JTable select Hello,

Can anybody explain me how I can retrieve the value of a certain
clicked cell of a JTable?

thx

pat

 
Jason Herald





PostPosted: 2005-4-4 7:22:00 Top

java-programmer >> JTable select see if this helps:
http://java.sun.com/developer/Books/swing2/chapter18-01.html

--
If you have any additional questions or need source code email
email***@***.com.

Thanks

Jason Herald
"pat270881" <email***@***.com> wrote in message
news:email***@***.com...
> Hello,
>
> Can anybody explain me how I can retrieve the value of a certain
> clicked cell of a JTable?
>
> thx
>
> pat
>


 
IchBin





PostPosted: 2005-4-4 7:28:00 Top

java-programmer >> JTable select pat270881 wrote:
> Hello,
>
> Can anybody explain me how I can retrieve the value of a certain
> clicked cell of a JTable?
>
> thx
>
> pat
>
The method for determining the selected cells depends on whether column,
row, or cell selection is enabled.

JTable table = new JTable();

if (table.getColumnSelectionAllowed()
&& !table.getRowSelectionAllowed()) {
// Column selection is enabled
// Get the indices of the selected columns
int[] vColIndices = table.getSelectedColumns();
} else if (!table.getColumnSelectionAllowed()
&& table.getRowSelectionAllowed()) {
// Row selection is enabled
// Get the indices of the selected rows
int[] rowIndices = table.getSelectedRows();
} else if (table.getCellSelectionEnabled()) {
// Individual cell selection is enabled

// In SINGLE_SELECTION mode, the selected cell can be retrieved
using
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
int rowIndex = table.getSelectedRow();
int colIndex = table.getSelectedColumn();

// In the other modes, the set of selected cells can be
retrieved using

table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

// Get the min and max ranges of selected cells
int rowIndexStart = table.getSelectedRow();
int rowIndexEnd = table.getSelectionModel().getMaxSelectionIndex();
int colIndexStart = table.getSelectedColumn();
int colIndexEnd = table.getColumnModel().getSelectionModel()
.getMaxSelectionIndex();

// Check each cell in the range
for (int r=rowIndexStart; r<=rowIndexEnd; r++) {
for (int c=colIndexStart; c<=colIndexEnd; c++) {
if (table.isCellSelected(r, c)) {
// cell is selected
}
}
}
}


--


Thanks in Advance...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
if there is any reaction, both are transformed.'
- Carl Gustav Jung, (1875-1961), psychiatrist and psychologist
 
 
pat270881





PostPosted: 2005-4-5 1:50:00 Top

java-programmer >> JTable select Hello,

I used the following code:

int row = table.getSelectedRow();
int column = table.getSelectedColumn();
Object value = table.getValueAt(row, column);

I have a JTable where some lines are displayed. The user is able to
select a line and when he presses a button a new window is opened where
details to this line are displayed. The problem is when I close this
detail window and want to select another line in the JTable the
getSelectedRow and the getSelectedColumn always returns -1 and an
ArrayIndexOutOfBoundsException although I have selected once more a
line.

Does anbody know how I can fix this problem?

regards

pat