代码之家  ›  专栏  ›  技术社区  ›  HansDampf

如何在jtable中设置具体的列设置?

  •  1
  • HansDampf  · 技术社区  · 16 年前

    如何使jtable为每个列显示特定的内容?

    我可以将对象用于多个列吗?我希望有一列显示日期(dd.mm.yyyy),另一列显示时间(hh:mm),但现在它只显示日期对象作为一个整体的字符串表示。

    public class AppointmentTableModel extends AbstractTableModel {
        ...
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return appointments.get(rowIndex).getByColumn(columnIndex);
        }
    }
    
    public class Appointment {
        ...
        public Object getByColumn(int columnIndex) {
            switch (columnIndex) {
                case 0: return date;
                case 1: return date;
                case 2: return sample;
                case 3: return sample;
                case 4: return history;
                case 5: return comment;
            }
            return null;
        }
    }
    
    2 回复  |  直到 16 年前
        1
  •  3
  •   akf    16 年前

    您可以为两列设置特定的呈现器,其中一列将 Date 对象并将其格式化为日期字符串,另一个将 日期 对象并将其格式化为时间字符串。

    this tutorial 例如日期渲染。

        2
  •  1
  •   Peter Lang    16 年前

    你唯一需要改变的是 getByColumn .

    你只需要应用两种不同的格式 case 0 case 1 例如。


    您可以使用 SimpleDateFormat 为此:

    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
    // ...
    case 0: return dateFormat.format(date);
    case 1: return timeFormat.format(date);