在JXTable单元格中设置日期格式 [英] Setting the date format in JXTable cell

查看:67
本文介绍了在JXTable单元格中设置日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此线程上我已经弄清楚了如何将JXDatePicker添加到JXTable(或JTable)单元格中.但是我现在有一个小问题.

On this thread I've figured out how to add a JXDatePicker in to a JXTable (or JTable) cell. But I've a small issue now.

DatePicker弹出并根据我的需要正常工作.但是我无法更改单元格中日期的显示格式.它以以下长格式显示.

DatePicker pops up and works fine according to my need. But I can't change the display format of the date in the cell. It shows in the following long format.

Eg: Wed Aug 01 00:00:00 IST 2012

但是我需要 dd-MMM-yyyy 格式.

我尝试更改 DatePickerCellEditor 的格式.部分有效.也就是说,现在当该单元格处于焦点时,它将根据设置的格式显示日期.但是当我专注于其他单元格时,它又回到了上面的格式.

I've tried changing the DatePickerCellEditor's format. It partly works. Which means, now it shows the date according to the set format when that cell is in focus. But when I focus on some other cell, it again goes back to the above format.

可能不是 DatePickerCellEditor 的问题,可能是 tableModel 的原因.但是无法弄清楚.任何帮助表示赞赏..

May be the fault is not with the DatePickerCellEditor, may be it has to do something with the tableModel. But can't figure it out. Any help is appreciated..

谢谢!

推荐答案

根据您先前的问题,除了您要使用渲染器而不是编辑器之外,其处理过程几乎相同

As per you're previous question, the process it pretty much the same, except you want to use renderer instead of an editor

public class DateCellRenderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable jtable, Object value, boolean selected, boolean hasFocus, int row, int column) {

        if (value instanceof Date) {

            // You could use SimpleDateFormatter instead
            value = DateFormat.getDateInstance().format(value);

        }


        return super.getTableCellRendererComponent(jtable, value, selected, hasFocus, row, column);

    }

然后,要应用渲染,您可以将其应用到特定的列(这样只有该列才能使用它),或者在诸如 Date 之类的情况下,您可能希望将其全部使用使用 Date 值的列...

Then, to apply the render you can either apply it to specific column (so only that column will use it), or in the case of something like Date you might want to use it for all columns using the Date value...

JTable table = new JTable();

DateCellRenderer renderer = new DateCellRenderer();
// Apply for a single column
table.getColumnModel().getColumn(0).setCellRenderer(renderer);
// OR apply for all columns using the Date class
table.setDefaultRenderer(Date.class, renderer);

这篇关于在JXTable单元格中设置日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆