I have a Jtable with a checkbox in first column. I want to strikethrough text of a row when the checkbox is selected. (eg same as we do in microsoft outlook when our task is complete.) I have tried using AttributeString, but not able to do it.
Can anyone please guide me to solve it?
String strStrike;
AttributedString as;
public void setTextStrikeThrough() {
for(int r=0;r< taskcells.length;r++) {
if (ttable.getValueAt(r,0).equals(Boolean.TRUE)) {
for(int c=2;c<7;c++) {
strStrike+=taskcells[r][c-1];
}//end inner for as=new
AttributedString(strStrike);
as.addAttribute(TextAttribute.STRIKETHROUGH,
TextAttribute.STRIKETHROUGH_ON);
as.getIterator();
}//end if
}//end for
}
I am not getting exactly where to call this method. I want to strikethrough text of a row when checkbox of that row has been checked.
I don’t know that an ActionListener will work well for a JCheckBox in a JTable since the check box isn’t a real button but rather a rendering of a checkbox. Perhaps playing with the table model will help. For instance you can use HTML to display a strike through of Strings displayed in table cells. For instance below I create a custom TableModel that extends DefaultTableModel and holds rows with a Boolean object followed by objects of a TextWrapper class that I’ve created that changes its toString result depending on a boolean.
e.g.,
I’m betting that there are better solutions including creating a custom renderer for your cells, but the code above offers a quick and dirty fix.