gpt4 book ai didi

java - Jtable 列中的复选框问题

转载 作者:行者123 更新时间:2023-12-01 16:09:13 24 4
gpt4 key购买 nike

我有一个包含 6 列的 Jtable,其中第六列中有复选框。我使用 setValueAt() 和 getValueAt() 方法将文本输出到 JTable 中。对于同一个 Jtable,我有 Find、Replace 和替换所有控件以查找、替换和替换 jtable 中的所有文本。特定单元格将聚焦于查找文本。特定单元格将聚焦并用给定文本替换文本。

我的问题是,在用给定文本替换文本时,我聚焦于特定单元格并使用 setValueAt() 进行替换。但是第六列中的复选框受到干扰,并且文本出现在该列中,例如 YES 或否(对于选定的复选框,我使用"is",对于取消选定的复选框,我使用“否”字符串)。这是我的示例代码:``

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);


}

}`

最佳答案

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.

这个:

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

应该是这样的:

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

相反。如果您注意到除了带有复选框的第六列中出现不合要求的文本之外,替换文本功能也不适用于第一列中的项目,这也解释/修复了它。

HTH。

附:我假设了很多,如果这不是您的意思,请重新表述您的问题以使其更清楚......

<小时/>

编辑:

只是详细说明我的评论:

Java Swing 教程是一个很好的起点:关于如何获取 boolean values to display in JTables as checkboxes 。您感兴趣的是他们通过执行以下操作为其表实现自定义 TableModel:

class MyTableModel extends AbstractTableModel {

...

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

...
}

就您而言,它可能是更明确的内容,例如

    public Class getColumnClass(int c) {
if (c == 7)
{
return Boolean.TYPE;
}
return String.class;
}

关于java - Jtable 列中的复选框问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1831672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com