selection in jtable
hi,
I need selection in jtable, but i want that when i change to the next line with the button pressed , that automatically the line where i start be complety select until the end of column and in the next line be selected from the begin until the point where i have the mouse pointer. For example:
0 0 0 0 S S S S S S S
S S S S S0 0 0 0 0 0
where S is the cell selected and the 0 in the unselected, the 5'th collumn of first line is where i start the selection and the 4th collumn of second line is where i have the mouse pointer. i Have also another case:
0 0 0 0 S S S S S S S
S S S S S S S S S S S
S S S S 0 0 0 0 0 0 0 0
i need help, i try to implement a selectionlist but i dont know how to control the selection. Anyone can tell me how to do that?
regards
# 1
I think you can't.
Here all what you can do with table selections:
int rows = 10;
int cols = 5;
JTable table = new JTable(rows, cols);
// Use this mode to demonstrate the following examples
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// The following column selection methods work only if these
// properties are set this way
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(false);
// Select a column - column 0
table.setColumnSelectionInterval(0, 0);
// Select an additional range of columns - columns 1 to 2
table.addColumnSelectionInterval(1, 2);
// Deselect a range of columns - columns 0 to 1
table.removeColumnSelectionInterval(0, 1);
// The following row selection methods work only if these
// properties are set this way
table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(true);
// Select a row - row 0
table.setRowSelectionInterval(0, 0);
// Select an additional range of rows - rows 1 to 2
table.addRowSelectionInterval(1, 2);
// Deselect a range of rows - rows 0 to 1
table.removeRowSelectionInterval(0, 1);
// The following cell selection methods work only if these
// properties are set this way
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
// Select a cell: cell (2,1)
int row = 2;
int col = 1;
boolean toggle = false;
boolean extend = false;
table.changeSelection(row, col, toggle, extend);
// Extend the selection to include all cells between (2,1) to (5,3)
row = 5;
col = 3;
toggle = false;
extend = true;
table.changeSelection(row, col, toggle, extend);
// Deselect a cell: cell (3,2)
// All cells in the row and column containing (3,2) are deselected.
row = 3;
col = 2;
toggle = true;
extend = false;
table.changeSelection(row, col, toggle, extend);
// This method actually toggles the selection state so that
// if it were called again, it exactly reverses the first call.
// Select cell (3,2) as well as the other cells that
// were deselected in the first call.
toggle = true;
extend = false;
table.changeSelection(row, col, toggle, extend);
// Select all cells
table.selectAll();
// Deselect all cells
table.clearSelection();
# 2
jTable1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
if (ALLOW_ROW_SELECTION) { // true by default
ListSelectionModel rowSM = jTable1.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
int[] vectC;
int[] vectR;
public void valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
//
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
System.out.println("No rows are selected.");
} else {
contr=1;
selectedRow = lsm.getMinSelectionIndex();
selectedRowM=lsm.getMaxSelectionIndex();
vectC=jTable1.getSelectedColumns();
vectR=jTable1.getSelectedRows();
System.out.println("Rowm " + vectR[0]
+ " RowM " + vectR[vectR.length-1]);
System.out.println("Colm " + vectC[0]
+ " ColM " + vectC[vectC.length-1]);
iniR=vectR[0];
fimR= vectR[vectR.length-1];
iniC=vectC[0];
fimC= vectC[vectC.length-1];
int ini=iniR;
if(iniR<fimR){
System.out.println("select to down!");
while(ini><=fimR){
System.out.println("ini: "+iniR+" Fim: "+fimR);
if(ini==iniR){
System.out.println("?igual");
jTable1.addRowSelectionInterval(ini,ini);
jTable1.addColumnSelectionInterval(1,fimC);
}
if(ini<fimR)
{
jTable1.addRowSelectionInterval(ini,ini);
jTable1.addColumnSelectionInterval(1,36);
}
if(ini==fimR)
{
jTable1.addRowSelectionInterval(ini,ini);
jTable1.addColumnSelectionInterval(1,fimC);
}
}
if(!e.getValueIsAdjusting()){
System.out.println("Actualizei a lista!");
}
I try to do this loop while but its impossible because it will be infinite i dont know how to resolve! can any tell me how to do that?>
# 3
add an ini++; just before the line if(!e.getValueIsAdjusting())
# 4
i forgot put this in the forum but i have it in my code, but the problem is that the variable ini is always with the value of iniR and dont increment, i dont know why,?because all the code in the valuechange is automatically refresh, i dont know how to resolve this can any body help me?
jTable1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
if (ALLOW_ROW_SELECTION) { // true by default
ListSelectionModel rowSM = jTable1.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
int[] vectC;
int[] vectR;
public void valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
//
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
System.out.println("No rows are selected.");
} else {
contr=1;
selectedRow = lsm.getMinSelectionIndex();
selectedRowM=lsm.getMaxSelectionIndex();
vectC=jTable1.getSelectedColumns();
vectR=jTable1.getSelectedRows();
System.out.println("Rowm " + vectR[0]
+ " RowM " + vectR[vectR.length-1]);
System.out.println("Colm " + vectC[0]
+ " ColM " + vectC[vectC.length-1]);
iniR=vectR[0];
fimR= vectR[vectR.length-1];
iniC=vectC[0];
fimC= vectC[vectC.length-1];
int ini=iniR;
if(iniR<fimR){
System.out.println("select to down!");
while(ini><=fimR){
System.out.println("ini: "+iniR+" Fim: "+fimR);
if(ini==iniR){
System.out.println("?igual");
jTable1.addRowSelectionInterval(ini,ini);
jTable1.addColumnSelectionInterval(1,fimC);
}
if(ini<fimR)
{
jTable1.addRowSelectionInterval(ini,ini);
jTable1.addColumnSelectionInterval(1,36);
}
if(ini==fimR)
{
jTable1.addRowSelectionInterval(ini,ini);
jTable1.addColumnSelectionInterval(1,fimC);
}
ini++;
}
}
if(!e.getValueIsAdjusting()){
System.out.println("Actualizei a lista!");
}
}
>
# 5
The previous response is right, you can't do want you want with the default table selection models. You would have to keep track of what cells are selected yourself. The best way to do that IMO is to override the JTable methods
public void changeSelection(int row, int column, boolean toggle, boolean extend)
and
public boolean isCellSelected(int row, int col)
# 6
Anybody knows if its possible, i try with changeselection but didnt result!
now i dont know how to make this! i find some article and code about any selection but this code only will permit how to do selection cell by cell; and i dont know how to discover how to know the selected cells:
the link is:
http://www.codeguru.com/java/articles/663.shtml
can anyone try help me!
# 7
What exactly do you mean bywhile(ini><=fimR)?
# 8
nothing! it appear like that i have While(ini<=fimR)can you help me?
# 9
anyone knows anything about selection listeners or some good tutorial? i need this, i saw one time this kind of selection in one aplication, and i want know how to do this! thanks in advanced!