Solved

positioning the text in list cell renderer

Posted on 2007-11-22
8
348 Views
Last Modified: 2013-11-23
part of code for cell rendering is shown below:

its working fine.. i set the image as Icon and corresponding image name

but currently, it shows the text name next (on right side) to image ..

how can i show the text image name to appear below the image instead of being next to it
public Component getListCellRendererComponent(JList list, Object value,
											int index, boolean isSelected, boolean cellHasFocus) {
 
		setVerticalTextPosition(BOTTOM);
		setFont(getFont().deriveFont(Font.PLAIN));
		setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
		
		setIcon(((ListItem)value).getThumbIcon());
		setText(((ListItem)value).getName());
		
		if (isSelected) {
			setBorder(BorderFactory.createLineBorder(Color.blue, 2));
		} else {
			setBorder(BorderFactory.createLineBorder(list.getBackground(), 2));
		}
		return this;
	}

Open in new window

0
Comment
Question by:max_dub
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20335333
Set the renderer to have a BorderLayout instead of a FlowLayout. Add your caption SOUTH
0
 
LVL 92

Expert Comment

by:objects
ID: 20336576
add:

setHorizontalTextPosition(JLabel.CENTER);
0
 

Author Comment

by:max_dub
ID: 20338664
that adds the label to the center of the image and when i used

setHorizontalTextPosition(JLabel.BOTTOM);

its throws java.lang.IllegalArgumentException: horizontalTextPosition

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:max_dub
ID: 20339016
i suppose becoz my renderer class extends jLabel and when i setIcon and setText .. it adds them separately .. not sure how to change the position of text to show below the image
0
 

Author Comment

by:max_dub
ID: 20339504
ok, my solution which worked .. i extended the rendered using JPanel instead of JLabel
public Component getListCellRendererComponent(JList list, Object value,
											int index, boolean isSelected, boolean cellHasFocus) {
		
//		setIcon(((ListItem)value).getThumbIcon());
//		setText(((ListItem)value).getName());
		
	iconLabel.setIcon(((ListItem)value).getThumbIcon());
	descriptionLabel.setText(((ListItem)value).getName());
	
	if (isSelected) {
	setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
	} else {
	setBorder(BorderFactory.createLineBorder(list.getBackground(), 2));
	}
	return this;
}

Open in new window

0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 20340831
> that adds the label to the center of the image and when i used
> setHorizontalTextPosition(JLabel.BOTTOM);

you need to set *both* horizontal and vertical

setHorizontalTextPosition(JLabel.CENTER);
setVerticalTextPosition(BOTTOM);

> ok, my solution which worked .. i extended the rendered using JPanel instead of JLabel

unecessary, and makes things compilicated.
a label will do it just fine (have used it many times myself).
0
 
LVL 92

Expert Comment

by:objects
ID: 20340900
you'd be even better off subclassing DefaultListCellRenderer
0
 

Author Comment

by:max_dub
ID: 20345777
thanks objects, i have now both versions :) working
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question