编辑单元格时切换值

发布于 2024-09-11 01:44:23 字数 665 浏览 8 评论 0原文

我有一个 JTable,它使用自定义 TableModel 来显示一系列对象实例。 getValueAt(int row, int column) 内部有一个 switch case,用于根据给定属性返回值(见下文)。一个 return 语句涉及返回值 1/0 作为真/假。

有没有办法可以修改此 TableModel,以便在编辑单元格时显示 1/0?

public Object getValueAt(int row, int column) {
    User user = (User)dataVector.get(row);
    switch (column) {
        case ID_INDEX:
           return user.getId();
        case USERNAME_INDEX:
           return user.getUserName();
        case PASSWORD_INDEX:
            return "****";
        case ACTIVATED_INDEX:
            return (user.getActivated())?"true":"false";
        default:
           return new Object();
    }
}

I have a JTable which uses a custom TableModel to display a series of object instances. There's a switch case inside getValueAt(int row, int column) to return values according to given attributes (see below). One return statement involves returning a value of 1/0 as true/false.

Is there a way that I can modify this TableModel so that it displays a 1/0 when a cell is edited?

public Object getValueAt(int row, int column) {
    User user = (User)dataVector.get(row);
    switch (column) {
        case ID_INDEX:
           return user.getId();
        case USERNAME_INDEX:
           return user.getUserName();
        case PASSWORD_INDEX:
            return "****";
        case ACTIVATED_INDEX:
            return (user.getActivated())?"true":"false";
        default:
           return new Object();
    }
}

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

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

发布评论

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

评论(2

不甘平庸 2024-09-18 01:44:23

的默认渲染器和编辑器>Boolean 是一个 JCheckBox。考虑使用

case ACTIVATED_INDEX:
    return Boolean.valueOf(user.getActivated());

替代方案,

case ACTIVATED_INDEX:
    return (user.getActivated())?"1":"0";

附录:例如, DefaultTableModel 不会覆盖 getColumnClass(),并且 AbstractTableModel 只是返回 Object.class< /代码>。您的 TableModel 应相应地覆盖 getColumnClass()

DefaultTableModel dtm = new DefaultTableModel() {

    @Override
    public Class<?> getColumnClass(int col) {
        return getValueAt(0, col).getClass();
    }
};
// add some data
JTable table = new JTable(dtm);

The default renderer and editor for Boolean is a JCheckBox. Consider using

case ACTIVATED_INDEX:
    return Boolean.valueOf(user.getActivated());

Alternatively,

case ACTIVATED_INDEX:
    return (user.getActivated())?"1":"0";

Addendum: As an example, DefaultTableModel does not override getColumnClass(), and AbstractTableModel simply returns Object.class. Your TableModel should override getColumnClass() accordingly:

DefaultTableModel dtm = new DefaultTableModel() {

    @Override
    public Class<?> getColumnClass(int col) {
        return getValueAt(0, col).getClass();
    }
};
// add some data
JTable table = new JTable(dtm);
枕花眠 2024-09-18 01:44:23

您需要查看 TableCellRendererTableCellEditor

TableCellRenderer 负责在未编辑时渲染单元格数据,其中TableCellEditor 负责提供用于编辑单元格值的组件。因此,您可以用两种不同的方式表示数据,具体取决于数据是正在编辑还是仅按正常方式呈现。

然而,您应该考虑到,如果您从 getValueAt() 方法返回布尔类型,您的 JTable 应该自动呈现一个 JCheckBox,当单元格处于编辑模式时,可以像往常一样通过单击它来更改 JCheckBox 值。为此,只需返回:

case ACTIVATED_INDEX:
    return Boolean.valueOf(user.getActivated());

You need to have a look at TableCellRenderer and TableCellEditor:

A TableCellRenderer is responsible for rendering cell data when it is not being edited, where as a TableCellEditor is responsible for providing a component used to edit the value of a cell. You can therefore represent the data in two separate ways depending on whether it is being edited or just rendered as per normal.

You should however consider that if you return a Boolean type from the getValueAt() method, your JTable should automatically render a JCheckBox, when the cell is in edit mode, the JCheckBox value can be changed by clicking on it as usual. To do this just return:

case ACTIVATED_INDEX:
    return Boolean.valueOf(user.getActivated());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文