Link to home
Start Free TrialLog in
Avatar of max_dub
max_dubFlag for Ireland

asked on

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Set the renderer to have a BorderLayout instead of a FlowLayout. Add your caption SOUTH
add:

setHorizontalTextPosition(JLabel.CENTER);
Avatar of max_dub

ASKER

that adds the label to the center of the image and when i used

setHorizontalTextPosition(JLabel.BOTTOM);

its throws java.lang.IllegalArgumentException: horizontalTextPosition

Avatar of max_dub

ASKER

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
Avatar of max_dub

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you'd be even better off subclassing DefaultListCellRenderer
Avatar of max_dub

ASKER

thanks objects, i have now both versions :) working