views:

324

answers:

1

Hi all, I have a Jtable with 6 columns where i have Check boxes in the 6th Column.I am outing the text in to JTable by using the setValueAt() and getValueAt() methods.For the same Jtable I have Find,Replace and Replace All Controls to find,replace and replace all the text i the jtable.The Particular cel will be focussed for Find text.Particular cell will be focused and Replace the text with given text.

My problem is, at the time of replacing text with given text,im focusing the particular cell and using setValueAt() to replace.But the Check boxes in the 6th column are disturbed and Text is appearing in that column like YES or NO(For selected check box i used YES and Deselected checkbox i used NO strings). Here is my sample code:``

StringTokenizer st1 = new StringTokenizer(trstring1, "\t");//trstring1 is the Jtable string
        for (i = 0; st1.hasMoreTokens(); i++) {
            for (j = 1; j < 6; j++) {
                rowstring = st1.nextToken();
                if (rowstring.contains(findTxt)) {
                    rowstring = rowstring.replace(findTxt, replaceTxt);
                    str = trstring1.replaceFirst(findTxt, replaceTxt);
                    mProcessQuestionTestItemTable.setCellSelectionEnabled(true);
                    mProcessQuestionTestItemTable.changeSelection(i, j, false, false);
                    mProcessQuestionTestItemTable.requestFocus();
                    System.out.println("I:" + i);
                    System.out.println("J:" + j);
                    mProcessQuestionTestItemTable.setValueAt(rowstring, i, j);


                }

              }`
A: 

I have a Jtable with 6 columns where i have Check boxes in the 6th Column Therefore you should be looping from columns with the indices 0 through 4.

This:

        for (j = 1; j < 6; j++) {

should be this:

        for (j = 0; j < 5; j++) {

instead. If you noticed that in additon to the unwanton text appearing in the 6th column with the checkboxes, the replace text functions was not working for items in the 1st column as well, this explains/fixes it too.

HTH.

p.s. I am assuming quite a bit, reword your question to make it a bit clearer if this wasn't what you meant...


EDIT:

Just to elaborate on my comment:

The Java Swing tutorial is a good place to start: on how to get boolean values to display in JTables as checkboxes. The point of interest to you is where they implement a custom TableModel for their table by doing:

class MyTableModel extends AbstractTableModel {

    ...

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    ...
}

In your case it could be something more explicit like

    public Class getColumnClass(int c) {
        if (c == 7)
        {
            return Boolean.TYPE;
        }
        return String.class;
    }
bguiz
Sorry for the confusion,Actually i have total of 7 columns.1st column is serial number which i made cellEditable as false(No need to modify serial num).i want to find and replace from 2nd column(i.e j=1) to 6th column(j=5 or j<6).In the sevent column i have checkboxes.Instead of Checkboxes i am geting its text like YES/NO whch is equivalent to true/false.I traced and i found that checkboxes are nt appearing bcoz of the mProcessQuestionTestItemTable.setValueAt(rowstring, i, j);What can i do to solve this
Bharath
Consider both of these: 1) Set the table model to use checkboxes in a particular column to represent boolean values. 2) You are calling `mProcessQuestionTestItemTable.setValueAt(rowstring, i, j)` since you are looping j from 1 to 5, the 7th column (idx=6) will never be set.
bguiz