JTable 右对齐标题
基本上,我有一个 JTable,其中包含带有右对齐单元格但左对齐标题的列,这看起来非常糟糕。我想右对齐这些列的标题而不改变标题的“外观和感觉”。
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
基本上,我有一个 JTable,其中包含带有右对齐单元格但左对齐标题的列,这看起来非常糟糕。我想右对齐这些列的标题而不改变标题的“外观和感觉”。
谢谢
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(13)
我在JAVA8下测试过。工作正常。
I have tested in JAVA8. working fine.
试试这个代码,
Try this code,
我已经根据 pvbemmelen62 的解决方案创建了一个类,可以非常轻松地使用它,例如:
或
代码如下:
I have created a class based on the solution of pvbemmelen62, that can be used very easily, for example:
or
Here's the code:
秘诀是使用虚拟表中的渲染器来获取正确的 L&F,并从真实表的行渲染器中复制对齐方式。这样每一列就分别对齐。代码如下:
上面的代码没有保留任何对渲染器的引用,因此避免了上面提到的 NPE bug。它不需要任何命名类,因此您只需将代码放在需要的地方即可。
The secret is to use the renderer from a dummy table to get correct L&F, and copy the alignment from the real table's row renderer. That way each column in aligned separately. Here is the code:
The above code does not keep any references to renderers, so it avoids the NPE bug mentioned above. It does not require any named class, so you can just drop the code in wherever you need it.
这是修改表的
JTableHeader
。对于这种用法来说,这并不是绝对必要的,但它最大限度地减少了对 UI 委托外观的影响。典型用法:
自定义标头渲染器:
Here's an alternate approach to modifying the
TableCellRenderer
of a table'sJTableHeader
. It's not strictly necessary for this usage, but it minimizes the impact on the UI delegate's appearance.Typical usage:
Custom header renderer:
试试这个:
Try this:
其中
0
是中心。Where
0
is Centre.上面显示的 HeaderRenderer(2011/sep/21,trashgod)与 Heisenbug (2011/sep/21) 的代码相结合,只有在所有标题对齐相同的情况下才能正常工作。
如果您想以不同的方式对齐不同的标题,那么您将必须使用以下代码:
即
:
在
getTableCellRendererComponent
中设置对齐方式,而不是在HeaderRenderer
构造函数中设置。The HeaderRenderer shown above (2011/sep/21 by trashgod) combined with code from Heisenbug (2011/sep/21) , will only work correctly if you have all headers aligned the same.
If you want to align different headers differently, then you will have to use the following code:
and
That is:
Set the alignment in
getTableCellRendererComponent
, and not in theHeaderRenderer
constructor.关于包装默认表头要记住的一件事:不要保留它们的引用。
如果您(或您的用户)在 Windows 7 和您的应用程序上使用 Windows 经典主题 设置默认系统 LAF,答案发布者@trashgod 可能会给你带来问题。
它受到十年前发布的此错误的影响(严重) 。如果您的表格正在显示,并且您在 Windows 首选项中将主题从 Aero 主题切换到 Windows 经典,则会出现一系列 NPE。您不应该保留渲染器的引用,因为它可能在某个时间点变得无效。包装应该以动态方式完成,正如错误报告的评论中所建议的那样。我从那里获取了代码并创建了以下可运行的示例:
只需运行该示例并选择
Yes
两次,然后观察它的分解。然后将I_WANT_THE_BUG_TO_HAPPEN
静态成员更改为false
并重复。将此成员设置为 true 的情况与此处获得最多支持的答案本质上相同。此示例最重要的部分是扩展的JTable
(MyAlternativeJTable
),它动态执行包装。目前对该问题接受的答案已被广泛使用,但这是不明智的。您可以在丢失的应用程序(包括 Netbeans 8.0.2)(其本身基于 Java)的情况下重现它,同时它显示可排序的表,例如
Window > 。 IDE工具>具有讽刺意味的是,通知
,您还可以在其中获得 NPE 报告。只需在 Windows 7 上将 Windows 主题从 Aero 切换到 Windows Classic(通过右键单击桌面 > 个性化 > 更改计算机上的视觉效果和声音
)即可。如果您使用 Glazed Lists 并调用 <一个href="https://svn.java.net/svn/glazedlists~svn/tags/glazedlists-1_9_0/www/api/ca/odell/glazedlists/swing/TableComparatorChooser. html#install(javax.swing.JTable,%20ca.odell.glazedlists.SortedList,%20java.lang.Object,%20ca.odell.glazedlists.gui.TableFormat)" rel="nofollow noreferrer">
ca.odell.glazedlists.swing.TableComparatorChooser.install
,您也会受到影响。它注入自己的自定义渲染器来对箭头进行排序。我在尝试寻找 这个问题我怀疑是相关的。
A thing to remember about wrapping default table headers: do not hold on to a reference of them.
If you (or your users) are using a Windows Classic theme on Windows 7 and your application sets default system LAF, the answer posted by @trashgod may cause problems for you.
It is affected by this bug, posted a decade ago (seriously). If your table is showing and you switch the theme in Windows preferences from an Aero Theme to Windows Classic, there will be a barrage of NPEs. You are NOT supposed to hold on to a reference of a renderer as it may become invalid at some point in time. The wrapping should be done in a dynamic way, as suggested in the comments of the bug report. I took the code from there and created the following runnable example:
Simply run the example and choose
Yes
twice and watch it break apart. Then change theI_WANT_THE_BUG_TO_HAPPEN
static member tofalse
and repeat. The case with this member set totrue
is essentially the same as the most upvoted answer here. The most important part of this example is the extendedJTable
(MyAlternativeJTable
) which does the wrapping dynamically.The currently accepted answer to this question is widely used, but it is ill advised. You can reproduce it with lost of applications, including Netbeans 8.0.2 (which itself is Java based) while it is showing a sortable table, such as
Window > IDE Tools > Notifications
, where you'll also get the NPE reports, ironically. Just switch the Windows theme from Aero to Windows Classic (viaright-click Desktop > Personalize > Change the visuals and sounds on your computer
) on Windows 7.If you are using Glazed Lists and call
ca.odell.glazedlists.swing.TableComparatorChooser.install
, you are also affected. It injects its own custom renderer for sorting arrows.I stumbled upon this by coincidence while trying to find a solution for this question which I suspect is related.
其中
MSISDNTable
是您的表格where
MSISDNTable
is your table