Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

update a JList based on an Array

 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lesson: Concurrency in Swing
 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to updateData class to update the JList with a set of items from an ArrayList. Will research swing timers.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to do something on the event dispatcher thread on a regular interval, use a javax.swing.Timer.
 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having problems with the timer, specifcally the line where i declare it. I have imported javax.swing.Timer;


 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking for help with those problems, then?
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Paul i'm wondering why my initalisation of Timer fails. Thanks for all your help.

Thanks for the heads up Rob.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NetBeans gives an error message on line 36, "no suitable constructor found.

Will read the FAQ Paul.

Thanks all.

 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code:

Correct code:

 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Genius. Thank you so much Paul, you're awesome
 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic