JTable-box

I get data for the database and are displayed in the table.

My getColumnClass

@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 displays, I have a problem, it just displays dd / MM / yyyy, but I need to display dd / MM / yyyy HH: mm, like I can i achieve this?

Other than that, I need to check if there is less data than today, and then turn off the line

+5
source share
2 answers

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 this?

+1
source

I know this is a very old question, but I had a very similar problem, and I decided to post the best solution to the problem.

: 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());

, .

, , Timestamp.

, !

+3

All Articles