JTable select

P

pat270881

Hello,

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

thx

pat
 
I

IchBin

pat270881 said:
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
 
P

pat270881

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top