JList text alignment

后端 未结 1 959
你的背包
你的背包 2021-01-13 05:50

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

1条回答
  •  醉梦人生
    2021-01-13 06:27

    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.

    Table with row 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.

    0 讨论(0)
提交回复
热议问题