This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable - selecting multiple rows by selecting checkbox column

 
firsco bear
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
firsco bear
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope this example would help you out.

 
firsco bear
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic