开发者

Java Swing: How to add a CellRenderer for displaying a Date?

I have a Table:

public class AppointmentTableModel extends AbstractTableModel {
    private int columns;
    private int rows;
    ArrayList<Appointment> appointments;...

So each row of the table contains one Appointment.

public class Appointment {

    private Date date;
    private Sample sample;
    private String comment;
    private ArrayList<Action> history;

    public Appointment(Date date, Sample sample, String co开发者_如何学编程mment) {
        this.date = date;
        this.sample = sample;
        this.comment = comment;
        this.history = new ArrayList<Action>();
    }

    public Object getByColumn(int columnIndex) {
        switch (columnIndex) {
        case 0: return date;//Date: dd:mm:yyyy

        case 1: return date;//Time mm:hh

        case 2: return sample;//sample.getID() int (sampleID)

        case 3: return sample;//sample.getNumber string (telephone number)

        case 4: return sample;//sample.getName string (name of the person)

        case 5: return history;//newst element in history as a string

        case 6: return comment;//comment as string


        }
        return null;

I added in comments what this one is going to mean. How would I create CellRenderers to display it like this.

   table.getColumnModel().getColumn(1).setCellRenderer(new DateRenderer());

I also want to add the whole row to be painted in red when the date is later then the current date. And then another column that holds a JButton to open up another screen with the corresponding Appointment as parameter.


See Table Format Renderers for rendering a date.

See Table Row Rendering for highlighting a line based on the value of a cell.

Edit:

This is how I created the data for table in the blog entry:

String[] columnNames = {"Date/Time", "Time", "Percent", "Currency"};
Object[][] data =
{
    {new Date(108, 0, 10), new Date(), new Double(.10), new Double(00075.25) },
    {new Date(108, 1, 15), new Date(), new Double(.50), new Double(01275.75) },
    {new Date(108, 2, 20), new Date(), new Double(.99), new Double(-4275.00) }
};

As you can see from the blog image, when you store a Date object in the model it is formatted correctly with the specified date or time.

Forget about your real program and create a SSCCE with the above data and prove to yourself that the concept works. Then figure out what you did wrong with your real code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜