Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JList text alignment

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?

like image 703
mbreen Avatar asked Feb 12 '12 16:02

mbreen


1 Answers

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.

like image 157
Andrew Thompson Avatar answered Sep 29 '22 08:09

Andrew Thompson