Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lists

 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, JList is your man.
Bill
 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic