• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

positioning the text in list cell renderer

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
max_dub
Asked:
max_dub
  • 4
  • 3
1 Solution
 
CEHJCommented:
Set the renderer to have a BorderLayout instead of a FlowLayout. Add your caption SOUTH
0
 
objectsCommented:
add:

setHorizontalTextPosition(JLabel.CENTER);
0
 
max_dubAuthor Commented:
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!

 
max_dubAuthor Commented:
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
 
max_dubAuthor Commented:
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
 
objectsCommented:
> 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
 
objectsCommented:
you'd be even better off subclassing DefaultListCellRenderer
0
 
max_dubAuthor Commented:
thanks objects, i have now both versions :) working
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now