The Artima Developer Community
Sponsored Link

Java Answers Forum
Need Help wit JList

4 replies on 1 page. Most recent reply: Nov 7, 2003 12:57 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Ennio Bozzetti

Posts: 4
Nickname: ennio
Registered: Oct, 2003

Need Help wit JList Posted: Nov 5, 2003 5:53 PM
Reply to this message Reply
Advertisement
I need to display my ArrayQueue in a JList
can some one help me write the toString method.

I have one, but is not going on the next line on the JList, It goes next to each other. This code works in DOS, but not using GUI, can some one help me please...



public String toString() {
String result = "";
int index = head;
for(int i = 0; i <length; i++){
result += queue[index] + "\n";
index = (index +1)%queue.length;
}
return result;
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Need Help wit JList Posted: Nov 5, 2003 6:32 PM
Reply to this message Reply
You need to iterate through your collection and add each item to the list separately. The list will not automatically split things up based on newline characters (and it shouldn't, because imagine what a mess it would be if you added one thing to the list and as a result it had a 100 more things in it).

By the way, when you build up a string from a collection, you'll get much better performance by using a StringBuffer:
public String toString()
{
   StringBuffer result = new StringBuffer();
 
   for( int i = head; i < queue.length; i++ )
   {
      if( i > head )
         result.append("\n");
      result.append( queue[i] );
   }
 
   return result.toString();
}

Ennio Bozzetti

Posts: 4
Nickname: ennio
Registered: Oct, 2003

Re: Need Help wit JList Posted: Nov 5, 2003 6:48 PM
Reply to this message Reply
Do I need to iterate on my list when I'm adding the data?
I'm going to post my code where I add to the JList.



private class ButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent e) {

if (buySellBox.getSelectedItem() == "Buy") {
String quantityStr = quantityFld.getText();
String priceStr = priceFld.getText();
try {
int quantitInt = Integer.parseInt(quantityStr);
int priceInt = Integer.parseInt(priceStr);
q.enqueue(quantitInt + " shares at " + priceInt);
dlm.addElement(q); <-- here I'm adding
quantityFld.setText("");
priceFld.setText("");
}
catch (NumberFormatException ex) {}
}
else {
}
}
}

Ennio Bozzetti

Posts: 4
Nickname: ennio
Registered: Oct, 2003

Re: Need Help wit JList Please Posted: Nov 6, 2003 5:14 AM
Reply to this message Reply
Can some one give me a help how to do the iterator, I just need to display it on my JList... and I'm try to look at the hole nite and I could do it...
thx

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Need Help wit JList Please Posted: Nov 7, 2003 12:57 PM
Reply to this message Reply
I assume dlm is a DefaultListModel object. Yes, you need to go through your list and add each item to the list model. Why are you using a home-made list instead of an ArrayList or LinkedList? If it is for a homework assignment, have you been asked to also provide an iterator? It might be that will be the next assignment, so in the mean time, you could just step through your list of items, adding each to the model.

If the homework assignment was to make a full-fledge implementation of a List, then your list should implement the java.utils.List interface, which would include the method iterator() (why not getIterator()? I don't know).

This means, of course that you also need to create a companion class that implements the Iterator interface. Your iterator class would likely be an inner class of your collection class.

Then later on in the course, you might focus on making the whole thing thread-safe.

Later still, you'll throw the whole thing out, because the built-in collections are going to be a lot more effient, complete and tested (by millions of users!).

Flat View: This topic has 4 replies on 1 page
Topic: Could Someone Help? Previous Topic   Next Topic Topic: Socket

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use