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