JList text alignment

怎甘沉沦 提交于 2019-12-19 07:36:52

问题


I have a JList with items that I want to show two values. 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?


回答1:


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.

Another way is to shove HTML into the default renderer (a JLabel), using an HTML table that stretches across 100% of the width. Though the custom renderer would be a better choice for a number of reasons (e.g. not presuming the type of the default renderer is a label).


BTW - perhaps you should consider using a JTable for this kind of functionality. No hacks or custom classes needed.


..does the jtable allow selecting items?

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 ..

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.



来源:https://stackoverflow.com/questions/9250470/jlist-text-alignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!