wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JTable - selecting multiple rows by selecting checkbox column Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable - selecting multiple rows by selecting checkbox column" Watch "JTable - selecting multiple rows by selecting checkbox column" New topic
Author

JTable - selecting multiple rows by selecting checkbox column

firsco bear
Greenhorn

Joined: Feb 07, 2005
Posts: 3
Hi,
I an new to swing .so please bear with me

I am trying to implement functionality something similar to yahoo mail.
I have a JTable having 5 columns. now i want to add one more column which has checkbox for each row.
Then when the user selects multiple checkboxs and clicks on add, i want to select corresponding rows from the table and add in the db.

Till now i am able to add a column to the table, showing checkbox in each row.

now my problem is i am not able to select multiple checkboxes at a time.when i select the second one, firstone is getting deselected.
can anyone please tell me what should i do to select multiple checkboxes (select multiple rows).
I tried for examples in net..but no help.
can anyone please give me a sample example, if they have...
Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

Why do not you post your code here so any one can help you out in your own coding rather giving you examples (that may or may not be useful for you).

I already worked out some sort of things. So i might help you out in your coding.


Love is GOD and GOD is Love.
Anand Karia
IT Concretor.......
M/s. Anand Karia Concreting IT
firsco bear
Greenhorn

Joined: Feb 07, 2005
Posts: 3
Here is my code i am using to add a checkbox column onto JTable

My CellEditor class
........................



public class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {

protected JCheckBox checkBox;;

public CheckBoxCellEditor() {
checkBox = new JCheckBox();
checkBox.setHorizontalAlignment(SwingConstants.CENTER);
checkBox.setBackground( Color.white);

}

public Component getTableCellEditorComponent(
JTable table,
Object value,
boolean isSelected,
int row,
int column) {

if(column==0)
{
checkBox.setSelected(((Boolean) value).booleanValue());

Component c =table.getDefaultRenderer(String.class).getTableCellRendererComponent(table, value, isSelected, false, row, column);

if (c != null) {
//checkBox.setBackground(c.getBackground());
}
}


return checkBox;

}


public Object getCellEditorValue() {
return new Boolean(checkBox.isSelected());



}





}





In my Tablemodel I implemented these two methods.
public Class getColumnClass(int col)
{ switch (col)
{
case 0: return Boolean.class;
default: return Object.class;
}
}





public boolean isCellEditable(int rowIndex, int columnIndex) {

if(columnIndex == 0){
return true;}
else {return false; }
}




In my class I am setting the column with renderer and editor like this


TableColumn tc = GlobalAccess.tblChanges.getColumnModel().getColumn(0);
tc.setCellEditor(new CheckBoxCellEditor());
tc.setCellRenderer(GlobalAccess.tblChanges.getDefaultRenderer(Boolean.class));

SO AS OF NOW I AM ABLE TO SHOW THE CHECKBOX ON THE JTABLE FIRST COLUMN.
NOW I WANT TO ADD FUNTIONALITY SO THAT WHEN I SELECT CHECKBOXES(MULITPLE), I WANT TO SELECT THE CORRESPONDING ROWS.
BUT NOW WHEN I CLICK ON CHECKBOX, IT IS GETTING SELECTED, BUT WHEN I CILCK ON SECOND THE FIRST CHECKED CHECKBOX IS AGAIN GETTING DESELETED.

CAN ANYONE PLEASE HELP ME WHAT I AM MISSING.
Vege Thokr
Greenhorn

Joined: Feb 04, 2005
Posts: 3
I'm not sure what's wrong with your code. But there is an easier way for a checkbox in JTable. If you have the datatype of that column as Boolean, it will be very straightforward. Java tutorial has a simple example on this, TableDemo.java. Hope this helps.
Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

Hope this example would help you out.

firsco bear
Greenhorn

Joined: Feb 07, 2005
Posts: 3
Thanks guys.

May be i am getting confused.

i am stuck at this point.
- Trying to select mulitple checkboxes at a time.
Can anyone please help me how can i select multiple checkboxes at atime.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable - selecting multiple rows by selecting checkbox column
 
Similar Threads
adding JCheckBox in JTable
JTable
adding a JCheckBox in a JTable
JTable row update
JTable(!!!)