JTable 单元格渲染

发布于 2024-10-07 15:22:31 字数 350 浏览 0 评论 0原文

我正在获取数据库数据并显示在表格中。

我的 getColumnClass 是

@Override
public Class<? extends Object> getColumnClass(int column) {
    return getValueAt(0, column).getClass();
}

当我打印值时,我得到的类名称为 java.sql.Timestamp,但是当它显示时我遇到了问题,它只显示 dd/MM/yyyy,但我需要它显示 dd/MM /yyyy HH:mm,我怎样才能做到这一点?

除此之外,我还需要检查数据时间是否小于今天,然后禁用该行

I'm getting data for the DB and displaying in a table.

My getColumnClass is

@Override
public Class<? extends Object> getColumnClass(int column) {
    return getValueAt(0, column).getClass();
}

when I print the value, I get the class name as java.sql.Timestamp, but when it display I'm have a problem, it just display dd/MM/yyyy, but I need it to display dd/MM/yyyy HH:mm, how can I achieve this?

Apart from doing this, I need to check if the datatime is lesser than today then disable the row

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

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

发布评论

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

评论(2

东走西顾 2024-10-14 15:22:31

我知道这是一个非常古老的问题,但我遇到了一个非常相似的问题,并决定发布一个更好的解决方案来解决该问题。

首先,您应该定义一个新类:TimestampCellRenderer.java

package gui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.table.DefaultTableCellRenderer;

public class TimestampCellRenderer extends DefaultTableCellRenderer {

    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    public TimestampCellRenderer() {
        super();
    }

    public void setValue(Object value) {
        if (formatter == null) {
            formatter = DateFormat.getDateInstance();
        }
        setText((value == null) ? "" : formatter.format(value));
    }
}

然后在您的 GUI 类中,将此定义添加到您的类中:

yourTable.getColumnModel().getColumn(1).setCellRenderer(new TimestampCellRenderer());

在本例中,我决定格式化第一列,但定义一个常量将是一个非常好的实践在你的表模型中。

最好的部分是您可以将您的代码与使用时间戳数据类型的其他表重用。

希望这有帮助!

I know this is a very old question, but I was having a very similar problem and decided to post a better solution for the problem.

First, you should define a new class: TimestampCellRenderer.java

package gui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.table.DefaultTableCellRenderer;

public class TimestampCellRenderer extends DefaultTableCellRenderer {

    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    public TimestampCellRenderer() {
        super();
    }

    public void setValue(Object value) {
        if (formatter == null) {
            formatter = DateFormat.getDateInstance();
        }
        setText((value == null) ? "" : formatter.format(value));
    }
}

And then in your GUI class, add this definition to your class:

yourTable.getColumnModel().getColumn(1).setCellRenderer(new TimestampCellRenderer());

In this case, I decided to format the first column, but it would be a very good practise to define a constant in your table model.

The best part of this is that you can reuse your code with other tables that make use of the Timestamp data type.

Hope this helps!

久伴你 2024-10-14 15:22:31

我找到了解决方案,这就是

@Override
        public Class<? extends Object> getColumnClass(int column) {
            String value = getValueAt(0, column).getClass().toString();

            if (value.equalsIgnoreCase("class java.sql.Timestamp")) {
                return JTextField.class;
            }

            return getValueAt(0, column).getClass();
        }

有更好的方法吗?

I found a solution, and here it is

@Override
        public Class<? extends Object> getColumnClass(int column) {
            String value = getValueAt(0, column).getClass().toString();

            if (value.equalsIgnoreCase("class java.sql.Timestamp")) {
                return JTextField.class;
            }

            return getValueAt(0, column).getClass();
        }

Is there a better way to do it?

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