简体   繁体   English

JList文本对齐

[英]JList text alignment

I have a JList with items that I want to show two values. 我有一个JList其中包含要显示两个值的项目。 Is there a way to have it show a string name and then have a right justified string to show a value. 有没有办法让它显示一个字符串名称,然后有一个右对齐的字符串来显示一个值。 Looking something like this: 看起来像这样:

Title__________________120
Title2_________________135

Is it possible to pass in two string to an item and have the first string display on the left and the second one on the right? 是否可以将两个字符串传递给一个项目,并且第一个字符串显示在左侧,第二个字符串显示在右侧?

Sure, implement a custom renderer . 当然,实现一个自定义渲染器 You might return a JPanel with BorderLayout as the rendering component, with the LHS text in the WEST , and the RHS text in the EAST . 您可能会返回一个以BorderLayout作为呈现组件的JPanel ,在WEST使用LHS文本,在EAST使用RHS文本。

Another way is to shove HTML into the default renderer (a JLabel ), using an HTML table that stretches across 100% of the width. 另一种方法是使用可跨越宽度100%的HTML表将HTML推送到默认渲染器( JLabel )中。 Though the custom renderer would be a better choice for a number of reasons (eg not presuming the type of the default renderer is a label). 尽管出于多种原因(例如,不假定默认渲染器的类型为标签),自定义渲染器将是一个更好的选择。


BTW - perhaps you should consider using a JTable for this kind of functionality. 顺便说一句-也许您应该考虑将JTable用于此类功能。 No hacks or custom classes needed. 无需黑客或自定义类。


..does the jtable allow selecting items? ..jtable允许选择项目吗?

Of course! 当然! Here is an example taken directly from How to Use Tables in the tutorial. 这是直接从教程中的“ 如何使用表”中获取的示例。 'Jane' is selected. 选择“简”。

选中行的表

A table is a little more effort to set up and get right, but it is well worth the effort. 桌子需要花费更多的精力来设置和正确摆放,但是值得付出努力。

Would a JTable perform just as a JList .. JTable是否会像JList一样执行..

No, the table ultimately provides more functionality. 不,该表最终提供了更多功能。 But the things it does which a list can also do, work (for the user) in much the same way. 但是列表也可以完成的工作(对于用户)以几乎相同的方式起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM