Solved

positioning the text in list cell renderer

Posted on 2007-11-22
8
336 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 87
Unexpected HttpURLConnection connection behavior 2 52
Java / Linux and Regular Expressions 11 48
Java Loop 6 25
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now