在 JTable 中选择列时将标题文本设为粗体
当我选择其列的单元格时,可以将标题文本变为粗体吗?怎么做呢?
谢谢 莱安德罗
is possible to turn header text bold when I select a cell of its column? How to do it?
Thanks
Leandro
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将自定义渲染器设置到表头中。如果选择当前列,则将列字体设置为粗体。
Set custom renderer into table header. If current column is selected column set font to bold.
这是部分可能的。您所要做的就是编写模型,以便在表的单元格选择上您的模型执行 fireTableStructureChanges 方法(假设您使用 AbstractTableModel 作为基础)。这将重新绘制整个表格,包括列标题。您所要做的就是保持所选列的状态。
一开始我说“部分”可能。这是因为调用 fireTableStructureChanges 将重新验证整个表,并且您将丢失当前的列模型状态 - 列宽度和序列。
要使文本加粗,您可以使用 HTML - 类似于
您的文本
,但它必须根据模型的内部动态更改状态更新:也可以直接设置列表标题文本,但模型更改或 tableStructureChanged 事件将使表从模型中重新读取。
It is partially possible. What you have to do is write your model is such way that on table's cell selection your model executes fireTableStructureChanges method( assuming you use AbstractTableModel as a base). This will repaint the whole table including column header. All you have to do is keep the state of which column is selected.
In the beginning I said "partially" possible. That is because calling fireTableStructureChanges will revalidate the whole table and you will lose your current column model state - column widths and sequence.
To make your text bold you can use HTML - something like
<html><b>your text</b></html>
, but it has to change dynamically based on your model's internal stateUPDATE: Also column table header text can be set directly but model change or tableStructureChanged event will make the table to reread from the model.