Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Selection of Panel

 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
In regard to my Chat application, I also wanted to add the feature where the list of users were listed with some info about each user such as his image and name, etc.
I want to give the user the capability of "selecting" a user info such that that particular region changes colour, giving it the impression of being selected.
I have till now tried listing the names of users in a JPanel. Can I attach mouse listeners to a JPanel to detect changes that occur there?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if I'm reading this right, create a user class that contains all the user's info,
picture/name/location/whateverElse
override the toString() method to return user's name, in the format you want to display it
add each user to a JList (display name from toString() will appear in list)
add a listSelectionListener to the list, on selection (in a separate area) the
user's info is displayed (pic, full name, location, whatever)
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can use a JList with a custom ListCellRenderer. This is what you would do for an interface like iChat's, where a user selection will highlight the name, the icon, and the color-dot icon thing all together.

In addition to SelectionListener, you could also add a MouseListener to detect double-clicks.
[ January 11, 2007: Message edited by: Brian Cole ]
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
Thanks so much for your suggestions. I shall read up the Tutorial on ListCellRenderer (no idea how to use it) on Sun right away. Hope its easy.
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
I read up a Tutorial on ListCellRenderer. There they showed me how to set an icon to a label and return it as a Component. I wanted to do heavy modification to the JList.
I thought of customizing a JPanel according to my needs and then returning it. But I ran into a problem.
When I simply add some labels to the JPanel and return it, it works.
But, when I try to use the paintComponent(Graphics g) method to draw an image and add a String to the JPanel, the JList just shows up blank.
Can you help me on how to overcome this problem?
[ January 13, 2007: Message edited by: Shyam Prasad Murarka ]
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
I have successfully displayed the information that I wanted to with the help of the following code:


UserBox is a class extending from JPanel where I customize it with the paintComponent() method.

The problem is that now the JList has lost the property of being selected. Is there any way that I can again make the JList selectable the default way without too much of custom-code?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic