This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Selection of Panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Selection of Panel" Watch "Selection of Panel" New topic
Author

Selection of Panel

Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
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?


With Best Regards,
Shyam Prasad Murarka
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Sep 20, 2005
Posts: 862
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 ]

bitguru blog
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
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

Joined: May 02, 2005
Posts: 209
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

Joined: May 02, 2005
Posts: 209
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?
 
 
subject: Selection of Panel
 
Similar Threads
How session Info is sent to JavaScript (Client) here?
Selection of Panel
JTable Focus differences in Java version 1.4 vs 1.5+ when editing
Switching JPanels
Saving panel content?