Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically rearrange layout with Swing

I am building a desktop app with Swing with similar functionality to Twitter. I have a "feed" page where "tweets" are displayed.

I have the "tweets" in a JPanel and want to dynamically display new ones coming in by placing new "tweets" at the top of the JPanel and moving older ones down. I was trying to do this with MigLayout by using jpanel.add(tweet, "cell 0 0, wrap") however this didn't work as intended and in order to get it to display with the layout I wanted I had to call jpanel.revalidate().

This is not ideal as there could be many tweets coming in every second and redrawing the panel can be quite slow. Is there anyway I can add new "tweets" to the top of the panel without redrawing?

like image 439
nomel7 Avatar asked Dec 01 '22 21:12

nomel7


1 Answers

Although dynamic layouts are possible, as shown here and here, I'd use a one-column JTable with a TableModel that reflects the pushdown stack effect you want, perhaps one having an internal Queue<Tweet>. JTable rendering is efficient, and the component works well in a JScrollPane.

like image 195
trashgod Avatar answered Dec 08 '22 19:12

trashgod