• Post Reply Bookmark Topic Watch Topic
  • New Topic

A good way to construct a vertical list of JPanels.

 
Ishan Verma
Greenhorn
Posts: 4
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am working on writing a desktop based email client using swing. First some specifics: I need to show emails in a (dynamically updatable) vertical list. Each item in this vertical list would represent an email showing: (a) the subject, (b) a photo of the sender that I have locally available & a couple of buttons. So, I plan to create a JPanel containing a JLabel to show the Subject, The photo inside a JLabel & JButtons. I have the layout of this panel done and is not a problem.

My problem is with stacking all these JPanels (each representing an email) and showing them on the UI as a list. I am not sure of any way to do this. Moreover, when a new email is received, it has to be displayed at the top of the list (not at the bottom).

Specifically, I need a good way of displaying a vertical list of JPanels & be able to add new JPanels at the top of the list. I appreciate any thoughts or prior experiences implementing something like this, that you can share.

Thanks a bunch for any help you can offer.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is possible to use JPanel as a renderer. So your list could be either a JTable, or perhaps even a JList. Note that I haven't done it myself yet, but I believe it should be quite straightforward. Adding a new emails is straightforward with both components - assuming you already know how to do it in a JList/JTable.

The problem with this approach is twofold:

1) If you plan to use variable row heights, you need to manage them somehow. Just setting the height of the JPanel while rendering the contents is not going to help (actually, I'd say you must not change the dimensions while rendering). So you'll need to be able to devise the height of each row somehow (probably when creating the row) and set the row height in your component accordingly (JTable can do it, not sure whether a JList can).

2) The JPanel will be used just for rendering; that is, if you put buttons on it, they'll be drawn, but won't be clickable. Similarly tooltips over your components, if you set them, won't show up. It might be possible to get the buttons to work using the editing mechanism of the JTable, but I'm afraid it will be far from smooth. Should they be focusable using keyboard? (Um, perhaps I actually don't want to know the answer )

Alternatives:

1) Keep your components (individual panels) on a giant JPanel and put that into a JScrollView. The problem is that if you need to show many emails, you could perhaps encounter some memory problems. Also, navigation using keys (up, page up etc.) might be problematic, as could be the focus subsystem.

2) Use the same scheme as above, but only keep JPanels that are currently visible inside the JScrollView. All the problems of the above remain, plus you need to implement a management to create/destroy (or just move and reuse) the panels as the view scrolls inside the JPanel. You might be able to support many emails this way, though.

3) Try to find some 3rd party, rich table implementation in Swing. Even if you spend a few days looking for one, it might save you many weeks of work to get everything right with a solution of your own. Good components might not be free, though.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch!
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each item in this vertical list would represent an email showing: (a) the subject, (b) a photo of the sender that I have locally available & a couple of buttons.


Can't tell if you want all this data on a single row. If so then just use a JTable. The DefaultTableModel will allow you to insert rows anywhere in the table. Read the Swing tutorial on How to Use Tables for more information.

In order to handle the buttons you will need to create a custom renderer/editor. Check out Table Button Column for one way to do this.
 
Ishan Verma
Greenhorn
Posts: 4
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch, Martin & Rob. On first thought, I like the idea of using a JTable.

Regarding Martin's points:

1) If you plan to use variable row heights, ...


I plan to have the same height for each row. Eyeballing thru the JTable documentation, I am hoping I might be able to "setRowHeight(int rowHeight)" for the JTable itself and make the row bigger than normal, yet same for all rows in the table.

2) ... Should they be focusable using keyboard? ...


I don't need the buttons to be focusable using keyboard

At this point I can say that I am most definitely going to take a stab at getting this done using a JTable I'll start with the links Rob pointed out.

One thing I am unsure about is if there is a way to implement a 'Mouse Over' functionality for the JPanel inside each row of the Table. I intend to make the buttons visible only when the mouse is over the particular JPanel. Let me know if you guys think it might be possible to get this done.

Once again, Thanks a lot for sharing your views.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for the JPanel inside each row of the Table


You don't use panels when you use a JTable. Read the tutorial.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPanel can be used as a renderer as I've mentioned earlier. Getting the panels to "live" inside the table will be difficult, as I've also noted. Here is the (very basic) proof of the concept:


Edit: embedding the buttons in individual table cells as Rob has demonstrated will be much, much easier than to use a JPanel renderer. I'd like to also point out that Ishan got the weird idea of using JPanel from me.
 
Ishan Verma
Greenhorn
Posts: 4
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Martin for the code. Now I exactly understand what you meant by the following:

The JPanel will be used just for rendering; that is, if you put buttons on it, they'll be drawn, but won't be clickable...


I do understand that it will be easier to add a button into a single table cell, but I doubt if that will look good form the UX perspective. I do believe I should think harder on this. Given that I havn't coded a JTable in my life, I think I should dive right into the JTable tutorial before I think any more and get my hands dirty with code. And Martin's code will help me do just that.

I will keep you guys posted as I hit bumps (& I have no doubts that I will).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!