Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

java - Alignment date parts in JTable column formatted in propotional font

I need to make the date parts (dd, MMMM, yyyy) to be vertically aligned. I asked a question at Fixed length of month and day in date format? to insert padding letters, but I found that it doesn't help in case of proportional font (the width of the letters are different). For example, with Lucida Fax font:

enter image description here
Making different labels for different date parts is considering but it's too manual. It's hard to make the text wrapped if the column width is small....
Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

note as for all Renderers (excluding preparedRenderer)you have to/be sure that you have to call that after any column/row changes in JTable

TableColumnModel m = myTable.getColumnModel();
m.getColumn(5).setCellRenderer(new SubstDateRenderer());

here you can set BackGround, ForeGround for TableCell

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.table.DefaultTableCellRenderer;

public class SubstDateRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = 1L;
    private Date dateValue;
    private SimpleDateFormat sdfNewValue = new SimpleDateFormat("dd.MMMM.yyyy");
    private String sdfNewValueString = "";

    public SubstDateRenderer() {// formating TableCell
        super();
        setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    }

    @Override
    public void setValue(Object value) {
        if ((value != null) && (value instanceof Date)) {
            dateValue = (Date) value;
            sdfNewValueString = sdfNewValue.format(dateValue);
            value = sdfNewValueString;
        }
        super.setValue(value);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...