• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Is there a way to make every item of a JList have different widths?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone!
I'm trying to display Strings in rows and columns. Since they differ in length and represent parts of a continuous text, I would like each component to only take up the size it needs to show the whole String.

So far, I managed to load the individual items into the list, limit the number of rows and make them wrap horizontally. I now wrote a custom ListCellRenderer as I figured this could solve my problem but it doesn't work the way I had hoped. The two Strings that are part of each item are neither centered nor displayed above one another (in fact, they are not readable at all because they seem to be drawn outside the cell) and they all have the same size. What am I doing wrong?

This is what my Renderer looks like:
 
Rancher
Posts: 3051
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the context of what you are trying to do.

Components like JList or JTable, show data in rows/columns, but the widths of columns are based on the largest column.

That is you can't have one row with 5 columns and the other with 4 columns.

Maybe you can draw an "ascii picture" that demonstrates what you are trying to achieve.

Otherwise the only suggestion I have is to use a JPanel with the Wrap Layout.

This will allow you to add individual components (each would represent an element) to the panel each being display at its preferred size.

The components will automatically wrap to a new line as required.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I definitely agree with the idea of a picture of what the JList should be displaying.

As for "centering", after looking at your paintComponent method it looks like you're trying to have a cell which contains two lines of text (word and tag) which are both centered between the left and right borders of the cell. I would implement that by putting two JLabels in your JPanel, one above the other. A FlowLayout or BoxLayout could be used to do that. Set their fonts to the getCurrentFont() value. Then put the "word" in one and the "tag" in the other. The JLabel will automatically centre the text and compute the width required based on the font and the contents of the text. You wouldn't need to do any of the FontMetrics calculations, the JLabels would already do that for you.
 
F. Freleaux
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that this is a bit confusing, sorry for that. Here is a bit more context.

As I already mentioned, the elements are part of a larger text. More specifically, a tagged corpus coming from an automatic tagger. Each element consists of the actual word, an upper row, and its corresponding tag (for example "noun"), a row underneath that (but both should be part of the same component). They are displayed in a fixed number of rows and columns. I want a user to be able to select a component, display its information in another panel and enable the user to edit it there to correct various mistakes that the tagger sometimes makes. Again, since the words differ a lot in length, it really disrupts the flow of the text if the components displaying them are all the same size. Originally, I used a normal panel to lay them out and it worked fine but I struggled with keeping track of the currently selected component. I figured a ListSelectionListener could help me with that.

The paint thing comes from this earlier panel variant where I couldn't get the MouseListener to work because of the child components. Even when using JLabels, the result still looks weird and every component has the same width though.

What I had hoped to get is what the attached picture shows (which is the panel version). The lowered border shows the current selection and the darker gray panels are uneditable. A MouseListener worked fine for me as long as the view always stayed the same but when you jump between parts of the corpus or simply scroll up or down (that's what the buttons are for), it gets quite awkward to change the borders and get the information from the right component manually. So, since JLists apparently don't work like that, do you maybe have a recommondation on how I could implement this neatly?
example.jpg
[Thumbnail for example.jpg]
 
Rob Camick
Rancher
Posts: 3051
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to me your "panel version" is working reasonably well. Maybe you just need a few tweaks?

You won't get anything that flexible from a single Swing component.

it gets quite awkward to change the borders and get the information from the right component manually.



You add a MouseListener to the component. In the MouseEvent you use the getSource(). method to get the component.

This will work whether the panel is scrolled or not.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't even try using JList for that output.

You might consider writing a customized LayoutManager to lay out those panels. My idea is, the whole thing would be contained in a JPanel which uses this LayoutManager. The manager would put as many of the small panels into a row as possible, using their minimum widths. Then (optional for first attempt) it would justify them to take up the whole row. After that it would go on to put the next lot of small panels into the next row, justify that, and so on. You wouldn't need to have a fixed number of rows, you could put the big panel into a JScrollPane where the scrolling unit is a row.

For all I know someone has written such a thing and open-sourced it, but I haven't looked for one.
 
F. Freleaux
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing is that I would like one component in view to be selected at all times because this additional panel I mentioned contains information about the index of the element, which is rather important information. Also, it would just look nicer and not be as confusing for the user as you would always know what you're currently looking at. It just seems more intuitiv to me.

Whenever I change anything about the view (move it, update elements etc.), the previous element's information is still of course in that panel but either the selection is gone or it's not coming from the current view.

It's unfortunate that there isn't a way to implement this more easily. I somehow assumed that this is a more common problem. I've honestly shied away from using a JScrollPane because we're talking about several millions of components and I thought it would be problematic to display and scroll through them smoothly.

A custom layoutmanager would be nice little extra. Right now, I made it so that the user can change the number of rows and columns within a certain range.


Thanks a lot to you two for trying to figure this out with me.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

F. Freleaux wrote:I've honestly shied away from using a JScrollPane because we're talking about several millions of components and I thought it would be problematic to display and scroll through them smoothly.



I definitely agree with that.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!