aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes update a JList based on an Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "update a JList based on an Array" Watch "update a JList based on an Array" New topic
Author

update a JList based on an Array

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
new problem.

I'm trying to update a JList based on an Array created from class ServerCore, specifically i'm setting array strings in the hope it will be updated by the JList, at the moment the JList stays static. Is there a way i can send Array bought to the JList and update it that way, just the when i customise my code on netbeans to "list = new JList(bought.toArray())" the GUI doesn't appear.

furthermore even though the GUI displays the cursor does not respond to it, nor does the box "sold" depress when clicked.

Thanks in advance.

Rob.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Sure. That's what the setListData(Object[]) method of JList is for. Of course you would have to call that on the JList which is already displayed in your GUI.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
tried adding:

soldBox.setListData(bought) in the updateData thread, Netbeans claims "no suitable method found".

when using soldBox.setListData(strings) the ListBox does not update, i think the GUI is crashing as i can't click the button "sold".

Edit:

The GUI is definaltly crashing, cursor won't change on it's edges and sometimes the GUI doesn't display properly. Commenting out the Thread "updateData" leaves the GUI working fine.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Lesson: Concurrency in Swing
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
so what's the best way to keep run the thread periodically. I've tried using doInBackground() and done() but the thread won't execute.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Rob Brew wrote:so what's the best way to keep run the thread periodically.


I would go back a couple of steps and ask why you needed a thread in the first place. I can't tell what the code is supposed to be doing or why it's doing it by looking at the code.

(There's also the question of why you didn't just use a Swing Timer instead of diving into the Swing threading swamp, but that would only be a question if you really needed something to happen independently once a second.)
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
I want to updateData class to update the JList with a set of items from an ArrayList. Will research swing timers.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Rob Brew wrote:I want to updateData class to update the JList with a set of items from an ArrayList.


That part I understood. The part about why you're doing it from a separate thread once every second, I didn't understand. That's why I asked about it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

If you need to do something on the event dispatcher thread on a regular interval, use a javax.swing.Timer.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Having problems with the timer, specifcally the line where i declare it. I have imported javax.swing.Timer;


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Are you asking for help with those problems, then?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

First of all, you need to start() the Timer.
Second, bough.toArray(strings) will create and return a new array if bought has more than 1 element.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Yes Paul i'm wondering why my initalisation of Timer fails. Thanks for all your help.

Thanks for the heads up Rob.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Rob Brew wrote:Yes Paul i'm wondering why my initalisation of Timer fails.


Then you ought to consider posting some details to explain to people in what way it "fails". You've been posting here long enough so that should be apparent to you by now. However if it isn't then I suggest reading our FAQ entry TellTheDetails.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
NetBeans gives an error message on line 36, "no suitable constructor found.

Will read the FAQ Paul.

Thanks all.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Your code:

Correct code:

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Genius. Thank you so much Paul, you're awesome
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Ok in the above code I need to synchronize methods around the array bought. I know i need to use:

synchronize (bought).

To do this, but if i do that do i need to synchronize the methods UpdateData and itemBought?
Also do i need to synchronize beingBought in ServerCore sc, referenced in line 119?

Do I use synchronized (bought) wherever i want a synchronized piece of code?

When i run the program i get this error:

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at argosserver.ServerGUI.itembought(ServerGUI.java:113)

ServerGUI:



the class it references is below:



how do i make this class synchronized?
Bit lost.

Thanks in advance.

Rob.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Synchronization isn't the problem. You can get a ConcurrentModificationException even when you use only one thread. The problem occurs when you are iterating over a collection (using Iterator), and during the iteration you modify the collection directly. The problem is somewhere here:

Since you copied the wrong code for the second part (you copied the same piece of code twice) I can't be sure, but something tells me that sc.beingBough() returns sc.BuyingQueue. If that's true you should use Iterator.remove:
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
thing is i want to remove it from sc.buyingQueue. ServerCore looks like this, do i need to synchronize it?

Also i want to send the JList an array of items "bought" and have it display bought.toString() in it's array, how do i achieve this?

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
anyone?
 
jQuery in Action, 2nd edition
 
subject: update a JList based on an Array