以比例字体格式化的 JTable 列中的对齐日期部分
我需要使日期部分(dd、MMMM、yyyy)垂直对齐。我在日期格式中的固定长度的月份和日期提出了一个问题? 插入填充字母,但我发现它在比例字体的情况下没有帮助(字母的宽度不同)。例如,使用 Lucida Fax 字体:
正在考虑为不同的日期部分制作不同的标签,但这太手动了。如果列宽太小,很难使文本换行......
谢谢
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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意,对于所有渲染器(不包括准备好的渲染器),您必须/确保您必须在
JTable
中的任何列/行更改后调用它,在这里您可以为 TableCell 设置 BackGround、ForeGround
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
here you can set BackGround, ForeGround for TableCell
正如 @mKorbel 所建议的,合适的
TableCellRenderer
是正确的选择,但您可能必须重写paintComponent()
并使用图形上下文的FontMetrics< /code> 如图此处。
如果数字月份可以接受,则大多数按比例间隔的字体都会为数字字形提供相同的恒定提前量。
As suggested by @mKorbel, a suitable
TableCellRenderer
is the right choice, but you may have to overridepaintComponent()
and render the text using the graphics context'sFontMetrics
as shown here.If numeric months are acceptable, most proportionally-spaced fonts give digit glyphs the same constant advance.
感谢您的回答。我为自己找到了一个解决方案,该解决方案使用 JTextPane 作为 TableCellRenderer,然后定义制表位值,并在格式化日期中使用制表符。这似乎符合我的要求,并且还具有普通文本字段的其他功能,例如自动换行......
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...