我必须选择该复选框两次才能在 jTable 中选中/取消选中

发布于 2024-08-27 00:40:51 字数 2668 浏览 8 评论 0原文

我有一个打算使用的 jTable 代码,但问题是,当我单击复选框时,它不会选择/取消选择它,而是必须单击两次。但是,如果我选择该行中除包含复选框的单元格之外的任何其他单元格,目的就解决了。

这是我的代码:

public class TableSelectionTest extends JFrame implements ListSelectionListener {
private final int COLUMN_COUNT = 5;
private TblModel model;
public TableSelectionTest() {
    initialize();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
}
private void initialize() {
    List data = new ArrayList();
    for (int i = 0; i < 10; i++) {
    Object record[] = new Object[COLUMN_COUNT];
    record[0] = Boolean.FALSE;
    for (int j = 1; j < COLUMN_COUNT; j++)
    {
        record[j] = new Integer(j);
    }
    data.add(record);
}
model = new TblModel(data);
JTable table = new JTable(model);
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener (this);
JScrollPane scroll = new JScrollPane(table);
getContentPane().add(scroll, BorderLayout.CENTER);
}

public static void main(String[] args) {
TableSelectionTest f = new TableSelectionTest();
f.show();
}

class TblModel extends AbstractTableModel {
    private List data;
    public TblModel(List data) {
        this.data = data;
    }
    public int getColumnCount() {
        return COLUMN_COUNT;
    }
    public int getRowCount() {
        return data == null ? 0 : data.size();
    }
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        getRecord(rowIndex)[columnIndex] = value;
        super.fireTableCellUpdated(rowIndex, columnIndex);
    }
    public Object getValueAt(int rowIndex, int columnIndex) {
        return getRecord(rowIndex)[columnIndex];
    }
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if(columnIndex == 0)
            return true;
        else
            return false;
    }
    public Class getColumnClass(int columnIndex) {
        if (data == null || data.size() == 0) {
            return Object.class;
        }
        Object o = getValueAt(0, columnIndex);
        return o == null ? Object.class : o.getClass();
    }
    private Object[] getRecord(int rowIndex) {
        return (Object[]) data.get(rowIndex);
    }
}

public void valueChanged(ListSelectionEvent e) {
    if (!e.getValueIsAdjusting()) {
        ListSelectionModel lsm = (ListSelectionModel) e.getSource();
        int index = lsm.getMinSelectionIndex();
        if(model.getRecord(index)[0] == Boolean.FALSE)
            model.setValueAt(Boolean.TRUE, index, 0);
        else if(model.getRecord(index)[0] == Boolean.TRUE)
            model.setValueAt(Boolean.FALSE, index, 0);
    }
}
}

请尽快回复,因为它让我很烦恼 先感谢您 :)

I have a jTable code i intend to use, but the problem with it is that when i click on the checkbox once it doesn't select/deselect it, instead i have to click twice. But if i select any other cell in the row except the one containing the checkbox the purpose is solved.

HERE IS MY CODE :

public class TableSelectionTest extends JFrame implements ListSelectionListener {
private final int COLUMN_COUNT = 5;
private TblModel model;
public TableSelectionTest() {
    initialize();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
}
private void initialize() {
    List data = new ArrayList();
    for (int i = 0; i < 10; i++) {
    Object record[] = new Object[COLUMN_COUNT];
    record[0] = Boolean.FALSE;
    for (int j = 1; j < COLUMN_COUNT; j++)
    {
        record[j] = new Integer(j);
    }
    data.add(record);
}
model = new TblModel(data);
JTable table = new JTable(model);
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener (this);
JScrollPane scroll = new JScrollPane(table);
getContentPane().add(scroll, BorderLayout.CENTER);
}

public static void main(String[] args) {
TableSelectionTest f = new TableSelectionTest();
f.show();
}

class TblModel extends AbstractTableModel {
    private List data;
    public TblModel(List data) {
        this.data = data;
    }
    public int getColumnCount() {
        return COLUMN_COUNT;
    }
    public int getRowCount() {
        return data == null ? 0 : data.size();
    }
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        getRecord(rowIndex)[columnIndex] = value;
        super.fireTableCellUpdated(rowIndex, columnIndex);
    }
    public Object getValueAt(int rowIndex, int columnIndex) {
        return getRecord(rowIndex)[columnIndex];
    }
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if(columnIndex == 0)
            return true;
        else
            return false;
    }
    public Class getColumnClass(int columnIndex) {
        if (data == null || data.size() == 0) {
            return Object.class;
        }
        Object o = getValueAt(0, columnIndex);
        return o == null ? Object.class : o.getClass();
    }
    private Object[] getRecord(int rowIndex) {
        return (Object[]) data.get(rowIndex);
    }
}

public void valueChanged(ListSelectionEvent e) {
    if (!e.getValueIsAdjusting()) {
        ListSelectionModel lsm = (ListSelectionModel) e.getSource();
        int index = lsm.getMinSelectionIndex();
        if(model.getRecord(index)[0] == Boolean.FALSE)
            model.setValueAt(Boolean.TRUE, index, 0);
        else if(model.getRecord(index)[0] == Boolean.TRUE)
            model.setValueAt(Boolean.FALSE, index, 0);
    }
}
}

Please reply soon as it is bugging me a lot
Thank you in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提赋 2024-09-03 00:40:51

您的 ListSelectionListener 与您的 TableCellEditor 冲突。

理想情况下,我建议删除 ListSelectionListener。这不是必需的,因为 TableCellEditor 只是调用 setValueAt() 来更新单元格的内容。

Your ListSelectionListener is conflicting with your TableCellEditor.

Ideally, I'd suggest removing the ListSelectionListener. It's not necessary since the TableCellEditor just calls setValueAt() in order to update the cell's contents.

浴红衣 2024-09-03 00:40:51

放大 @rob 的正确答案,您的 ListSelectionListener 会在每次更改时重置该值;通过表格tab查看效果。您的 setValueAt() 方法已经更新了数据;添加此 println() 即可查看:

for (Object row : data) {
    System.out.println(((Object[]) row)[0]);
}

顺便说一句,请考虑使您的 data 更通用,并避免使用已弃用的方法,例如 show()

附录:这是一个例子;另请参阅如何使用表< /a>.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class TableSelectionTest extends JFrame {

    private final int COLUMN_COUNT = 5;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TableSelectionTest();
            }
        });
    }

    public TableSelectionTest() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTable table = new JTable(new TblModel());
        JScrollPane scroll = new JScrollPane(table);
        this.add(scroll, BorderLayout.CENTER);
        pack();
        this.setVisible(true);
    }

    class TblModel extends AbstractTableModel {

        private Random rnd = new Random();
        private List<Boolean> data = new ArrayList<Boolean>();

        public TblModel() {
            for (int i = 0; i < 10; i++) {
                data.add(rnd.nextBoolean());
            }
        }

        @Override
        public int getColumnCount() {
            return COLUMN_COUNT;
        }

        @Override
        public int getRowCount() {
            return data.size();
        }

        @Override
        public void setValueAt(Object value, int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                data.set(rowIndex, (Boolean) value);
            }
            super.fireTableCellUpdated(rowIndex, columnIndex);
            for (Boolean b : data) {
                System.out.print(b + " ");
            }
            System.out.println();
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                return data.get(rowIndex);
            } else {
                return columnIndex;
            }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex == 0;
        }

        @Override
        public Class getColumnClass(int columnIndex) {
            if (columnIndex == 0) {
                return Boolean.class;
            } else {
                return Integer.class;
            }
        }
    }
}

Amplifying on @rob's correct answer, your ListSelectionListener is resetting the value with each change; tab through the table to see the effect. Your setValueAt() method already updates the data; add this println() to see:

for (Object row : data) {
    System.out.println(((Object[]) row)[0]);
}

As an aside, consider making your data more generic and avoid deprecated methods such as show().

Addendum: Here's an example; see also How to Use Tables.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class TableSelectionTest extends JFrame {

    private final int COLUMN_COUNT = 5;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TableSelectionTest();
            }
        });
    }

    public TableSelectionTest() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTable table = new JTable(new TblModel());
        JScrollPane scroll = new JScrollPane(table);
        this.add(scroll, BorderLayout.CENTER);
        pack();
        this.setVisible(true);
    }

    class TblModel extends AbstractTableModel {

        private Random rnd = new Random();
        private List<Boolean> data = new ArrayList<Boolean>();

        public TblModel() {
            for (int i = 0; i < 10; i++) {
                data.add(rnd.nextBoolean());
            }
        }

        @Override
        public int getColumnCount() {
            return COLUMN_COUNT;
        }

        @Override
        public int getRowCount() {
            return data.size();
        }

        @Override
        public void setValueAt(Object value, int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                data.set(rowIndex, (Boolean) value);
            }
            super.fireTableCellUpdated(rowIndex, columnIndex);
            for (Boolean b : data) {
                System.out.print(b + " ");
            }
            System.out.println();
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                return data.get(rowIndex);
            } else {
                return columnIndex;
            }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex == 0;
        }

        @Override
        public Class getColumnClass(int columnIndex) {
            if (columnIndex == 0) {
                return Boolean.class;
            } else {
                return Integer.class;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文