1

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

Community
  • 1
  • 1
Loc Phan
  • 4,304
  • 4
  • 29
  • 35
  • The question is that How to made a date column having date parts vertically aligned? As you can see on the attached images, the year is not aligned because the width of the mounth is different. – Loc Phan Jun 29 '11 at 08:06

3 Answers3

6

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);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Sorry, I don't understand this answer. This code creates a TableCellRenderer for date type. And the result are centrel aligned texts. What I need is the date parts (dd,MM,YYYY) are vertically aligned. You can see in the attached image, the years are not well aligned. – Loc Phan Jun 29 '11 at 10:36
  • are you tried put together ColumnRenderer with threads that you link you linked in your question, for me works together, and with Html syntax too – mKorbel Jun 29 '11 at 11:44
  • hmmm not maybe my Navajo <> English, put Html code/syntax in the TableCell, TableCell by default returns JLabel, and JLabel is best of JComponents for any of possible ZOO :-) – mKorbel Jun 29 '11 at 16:17
1

As suggested by @mKorbel, a suitable TableCellRenderer is the right choice, but you may have to override paintComponent() and render the text using the graphics context's FontMetrics as shown here.

If numeric months are acceptable, most proportionally-spaced fonts give digit glyphs the same constant advance.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Thanks for your answers. I found for myself a solution that uses JTextPane as a TableCellRenderer, then define the tabstop values, and use tabs in formatting date. It seems ok for my requirement and also have other features of normal text field such as word wrapping...

Loc Phan
  • 4,304
  • 4
  • 29
  • 35