Solved

positioning the text in list cell renderer

Posted on 2007-11-22
8
347 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet requestdispatcher include and forward differences 1 39
login form jsp example 2 35
Crystal Reports Licensing Questions 4 34
Java class and jar 3 20
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

809 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