I'm trying to write a scorekeeping app in Java that allows a server application to send scores to client applications. The clients will then display a list of teams, team IDs and their scores, sorted by score. Of course I could just use a swing JTable to display everything, but I want to achieve a unique effect: I want the text dynamically reorder itself every time a team moves up in the list. That is, it want to animate the text around the screen. I would also need to be able to update the contents of the labels after being added. What would be the best way to achieve this? Is there any component that allows you to add other components to it and place/move them freely?
3 Answers
JTable is a JComponent so you can set desired LayoutManager and add JLabels above the JTable. Then move/reorder them to achieve desired effect. See SwingWorker as well.

- 56,971
- 9
- 68
- 98
-
SwingWorker looks like a great way to manage the reordering, but how can I actually achieve the animation? I don't want the JLabels to change with new strings; I want the JLabels themselves to move, pixel by pixel, across the screen. How can this be done? – BitFiber Nov 21 '11 at 05:53
You could use a JTable and change the contents of the rows as teams move up. Or you could arrange a series of labels and change the text whenever you want. To change the value displayed for a JLabel you simply use the JLabel.setText("new value"); method.
I've never done this but I think you need to use a panel with a 'null' layout manager. Meaning you are responsible for absolutely positioning your elements
http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
You would need some kind of SwingWorker or Timer running to update the gui layout. Make sure you actually make the GUI changes on the EventThread, which you can do by using SwingUtilities.invokeLater();

- 1,543
- 9
- 18
-
Thanks! I'm having trouble forcing my container to update after its components have been shifted, but I'm sure I'll figure it out... – BitFiber Nov 21 '11 at 06:20
-
try calling revalidate() and repaint() after updating the layout – Eric Rosenberg Nov 21 '11 at 06:21
-
1no, don't use a null-layout, even if you want/need to do the positioning yourself, see f.i. the use of Rob's (aka @camickr ) DragLayout in http://stackoverflow.com/questions/8158913/java-swing-place-image-in-jlabel-to-above-second-jlabel-map-player-indicator – kleopatra Nov 21 '11 at 09:08
-
1Some alternative are mentioned [here](http://stackoverflow.com/questions/8207632/animated-table-of-strings/8210568#8210568). – trashgod Nov 21 '11 at 10:50