[java] Fun with lists

Started by
4 comments, last by andrewk3652 19 years, 3 months ago
Hi, I have a bunch of data. Data is represented iconically by an image of about 40x40 pixels. Each icon like that belongs to a certain category. There can be several categories. So, I want to show that in as little screen space as possible, plus when the scrolling is inevitable, I want to only scroll vertically and not horizontally. Basically, suppose we have a single piece of data that looks like this:

+-+
| |
+-+
Then suppose I have two categories with one piece of data each, and one category with three pieces of data. Then, I'd like to arrange data like this maybe:

+-----+ +-----+
| +-+ | | +-+ |
| | | | | | | |
| +-+ | | +-+ |
+-----+ +-----+
+-------------+
| +-+ +-+ +-+ |
| | | | | | | |
| +-+ +-+ +-+ |
+-------------+
Now, the problem here is that the panel that displays these data can be resized and any time, *and* data itself changes constantly. So, I want the layout thing to be flexible. (Plus, I want to be able to select categories and individual data.) Soooo. What I am doing now is essentially using a big JList of categories and each category is a JList of pieces of data. It almost works, but not quite. The problem is that if I give the list of categories VERTICAL layout orientation, then the above example looks like this:

+-------------+
|     +-+     |
|     | |     |
|     +-+     |
+-------------+
+-------------+
|     +-+     |
|     | |     |
|     +-+     |
+-------------+
+-------------+
| +-+ +-+ +-+ |
| | | | | | | |
| +-+ +-+ +-+ |
+-------------+
That sort of wastes a bunch of space, and requires a *lot* of vertical scrolling if I have a lot of categories with few pieces of data in each. On the other hand, suppose I give the list of categories HORIZONTAL_WRAP layout orientation. Then it gives the following results:

+-----+ +-----+
| +-+ | | +-+ |
| | | | | | | |
| +-+ | | +-+ |
+-----+ +-----+
+-----+ 
| +-+ | 
| | | | 
| +-+ | 
| +-+ | 
| | | | 
| +-+ | 
| +-+ | 
| | | | 
| +-+ | 
+-----+ 
That's okay in quite a few cases, but suppose I only have one category with say ten elements each. Then, it all is squished into one column and again we have lots of scrolling to do and lots of screen space wasted. So, it seems to me that the layout JList uses doesn't quite work here. However, before I get down to writing my own layout manager, I was wondering if anyone has any ideas how to do this better. All input much appreciated. Vovan
Vovan
Advertisement
Sun's layout managers are all sh**. Welcome to the painful world of "we wrote crap in 1997 and even now we still can't be bothered to fix it".

Quote:Original post by vovansim
Soooo. What I am doing now is essentially using a big JList of categories and each category is a JList of pieces of data. It almost works, but not quite. The problem is that if I give the list of categories VERTICAL layout orientation, then the above example looks like this:


The problem is that Sun's layout managers were written by a lazy person.

Instead of doing a useful, intelligent algorithm that would require some effrot by the coder, they just went for the "I can't be bothered, I'll do the simplest thing that doesn't work but which I could sort of almost pretend works".

What happens is that a lot of layout managaers ask for the minimum size of a PARTICULAR dimension (note: not in the "I'm a moron who works at sun and doesn't know the meaning of the word dimension" sense, but in the "english has this word that's been around for centuries" sense), and ignores everything else.

So, a borderlayout will ask the NORTH component "what's your minimum height?", and provide ONLY that.

Unofruntately, Sun's OWN WIDGETS are implemented so that for different current widths they return *DIFFERENT* minimum heights, and when the layout asks they *ASSUME* they can have as much widht as they want.

So, they return the min height, get it, and then disappear of the edge of the screeen. This is broken in every version of java 1.2, 1.3, 1.4 (and, I've heard, 5, although I haven't tried it there yet).

I'm 99% sure you're getting the same problem with your final layout: the layout manager is asking "what's your minimum possible width?" and the third item is giving an honest answer: if it's *FORCED* to, it can go that narrow.

You have two solutions:

1. Get pissed off with Sun and write your own layout managers from scratch, using your BRAIN. This is annoying, hard work, and shouldn't be necessary :(.

2. override the methods in your components for getMinSize, getPrefSize, getMaxSize and intelligently calculate the actual sizes given the current situations.

Number 2 will, usually, require hacking around with your other classes in a very very non-OOP way which is ugly and hard to maintain, because the idiots desining layout managers were too stupid to understand what *minimal amount of information* you need to do layout properly. Hint: when asked "how big are you?" you need to be given some information about how big the asker would LIKE you to be. This is well known, it's been around for many years, but apparently was too complex for the sun staff to comprehend...Sob.

I've done both methods in the past. Both work. The second method is horrible to maintain and gives you headaches fixing bugs in.The first method is easy to maintain but you can never use the sun layout managers again, and you have to write the whole layout system from scratch. I'm meaning to open-source some of my laout managers some day, but not soon - I just don't have enough time to clean them up, document them, etc :(.
PS: yes, I am bitter.

Thousands of people across the world waste hours of their lives every week because of the laziness / incompetence of just a couple of people at Sun. Those individuals probably cost the world economy millions of dollars a year in lost productivity, and god only knows how much stress, pain, and suffering of people going "why the hell won't this work?", especially if near to a deadline.

And it could all have been so much simpler, with so little extra effort by the original developers >:(.
I assume you have checked this, but just in case...

Have you set the layout policy of the JLists -within- the category JList (the ones that hold your actual data)? I haven't tried anything like this myself, but in the example you gave for the HORIZONTAL_WRAP it looks like the inner JLists are using the default VERTICAL alignment. Maybe changing these to also use HORIZONTAL_WRAP (if you haven't already) will get a bit closer to what you are looking for.

Good luck!
Hi,

Quote:Original post by Anonymous Poster
I assume you have checked this, but just in case...

Have you set the layout policy of the JLists -within- the category JList (the ones that hold your actual data)? I haven't tried anything like this myself, but in the example you gave for the HORIZONTAL_WRAP it looks like the inner JLists are using the default VERTICAL alignment. Maybe changing these to also use HORIZONTAL_WRAP (if you haven't already) will get a bit closer to what you are looking for.

Good luck!


Thanks for this and the above replies, AP (or APs as the case may be).

Indeed, that is what is puzzling me about the second example I gave in the original post: the specified layout for the inner list is also horizontal wrap, definitely not the default vertical. Hence, it would make sense it me if the layout worked very closely to what I want it to do. Unfortunately, like I said above, I think, it looks like the outer list takes the minimum size of the inner list, and so even if there is plenty of space left over on the right-hand side, the inner list is still squished all into one column, no matter what layout the inner list uses.

Vovan
Vovan
The fact is, Sun never meant you to code GUIs by hand beyond the simple test. Beyond that, they expected you to use NetBeans or a similar tool.
=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================

This topic is closed to new replies.

Advertisement