max_dub
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
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;
}
Set the renderer to have a BorderLayout instead of a FlowLayout. Add your caption SOUTH
add:
setHorizontalTextPosition( JLabel.CEN TER);
setHorizontalTextPosition(
ASKER
that adds the label to the center of the image and when i used
setHorizontalTextPosition( JLabel.BOT TOM);
its throws java.lang.IllegalArgumentE xception: horizontalTextPosition
setHorizontalTextPosition(
its throws java.lang.IllegalArgumentE
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
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you'd be even better off subclassing DefaultListCellRenderer
ASKER
thanks objects, i have now both versions :) working