This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Lists" Watch "Lists" New topic
Author

Lists

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
What compnnet should I use in Swing to create a box which holds a list of strings that constantly gets added to? I want to be able to move up and down too using the right edge sliding option.
Thanks.

Paul
Bill Liteplo
Ranch Hand

Joined: Oct 16, 2002
Posts: 88
Probably JList is your best bet. But that depends on what you want to do with it.
Do you want to be able to select one of the items?
Do you want the list to always show as many items as it can?
Do you wnat to be able to edit the list on the fly?
You can get by with JTextArea for all of these, except the first question. But it sounds like what you need is JList. Just update the list when the data change.
Bill
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
I don't want to be able to edit the list - but I may want to be able to select one of the entries on it.
Thanks.
Bill Liteplo
Ranch Hand

Joined: Oct 16, 2002
Posts: 88
Well, JList is your man.
Bill
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
I'm afraid I just don't seem to know enough about Awing to get started. When I create a JList, nothing shows up unless I add text to it. I need a nice big empty box sitting there if I have no data in it. I've looked at JTextArea but it looks like a box for adding straight forward text over multiple lines. I suppose I want something like a JTable with one column.
I'll give that a try.
Paul
Bill Liteplo
Ranch Hand

Joined: Oct 16, 2002
Posts: 88
You should create your own ListModel (for example, extend DefaultListModel) and set that as the model of your JList.
When I create a JList, I get a big empty box. I can't type in it (it is not editable, which was one of my questions). If you want to edit the data on the fly, you should set the list cell renderer to return a JTextField instead of the default (which is an uneditable JLabel). This will make it editable.
Alternatively, you can use a JTable with one column, in which case you should set up a TableModel to broker your data.

Having fun yet...?
Bill
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
I've just been playing with java.awt.List and it's looking half-decent so far - without having to do much to it. To add text I just call the add() method. What's your opinion on using this class? Childish or sensible...
Paul
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
don't mix swing and awt. you should stick with swing unless you're forced to use awt.
the problem why your JList appears to small is that you have to use the right Layout Management for your container. the Layout Manager will strech the list to fit into the place you want. see the Swing Tutorial on Layout Managers.
I prefer GridBagLayout but this might seem a bit complex on the first sight.
here is some sample code you could try:

chantal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lists