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?
Sure, implement a custom renderer. You might return a
JPanel
withBorderLayout
as the rendering component, with the LHS text in theWEST
, and the RHS text in theEAST
.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.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.
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.