Solved

positioning the text in list cell renderer

Posted on 2007-11-22
8
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video teaches viewers about errors in exception handling.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

726 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