473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Problem rendering Combo Box with icon near the text

Hi

I am having trouble rendering a combo box with icon near the text. Below is a simple code which demonstrates my problem.

import java.awt.Component;
import java.util.HashMap;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.WindowConstants;



public class IconComboBox extends javax.swing.JDialog {
private JComboBox jComboBox1;
private String[] modelData;
private HashMap<String, Icon> icons = new HashMap<String, Icon>();

/** Creates new form IconComboBox */
public IconComboBox(java.awt.Frame parent, boolean modal) {
super(parent, modal);

ImageIcon img1 = new ImageIcon("1.png");
ImageIcon img2 = new ImageIcon("2.png");

modelData = new String[] { "Item 1", "Item 2", "Item 3", "Item 4" };

icons.put(modelData[0],img1);
icons.put(modelData[1],img1);
icons.put(modelData[2],img2);
icons.put(modelData[3],img2);

initComponents();
}


private void initComponents()
{
jComboBox1 = new javax.swing.JComboBox();

getContentPane().setLayout(new java.awt.FlowLayout());

setDefaultCloseOperation(javax.swing.WindowConstan ts.DISPOSE_ON_CLOSE);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(modelData));
getContentPane().add(jComboBox1);

jComboBox1.setRenderer(new TypeListCellRenderer(icons));
setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );

pack();
}

public static void main(String args[]) {

try {
UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new IconComboBox(new javax.swing.JFrame(), true).setVisible(true);
}
});
}
catch (Exception e) {
// handle exception
}
}

private class TypeListCellRenderer extends JLabel implements ListCellRenderer {
HashMap<String, Icon> icons;

public TypeListCellRenderer(HashMap<String, Icon> icons) {
super();

this.icons = icons;
}

public Component getListCellRendererComponent( JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus ) {
String type = (String) value;
setText( type );
setIcon( icons.get( type ) );
return this;
}
}
}

When i run this program i will see the combo box with only the icon displayed (no text).
Now if will remark the following line:
UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
Then the code works just fine.

Did someone knows how to solve this look and feel issue ?
Dec 13 '07 #1
5 4124
JosAH
11,448 Expert 8TB
Why don't you put JLabels in the combobox model? A label can contain text as
well as icons and display them nicely so you don't have to do anything then.

kind regards,

Jos
Dec 13 '07 #2
sir i had tried with Label but it not working can u plz give me solution i.e.code
Dec 13 '07 #3
JosAH
11,448 Expert 8TB
sir i had tried with Label but it not working can u plz give me solution i.e.code
I wrote 'JLabel' (a Swing component), not 'Label' (an AWT component). Never mix
the two (Swing and AWT).

kind regards,

Jos
Dec 13 '07 #4
thank you sir now its working.
Dec 13 '07 #5
JosAH
11,448 Expert 8TB
thank you sir now its working.
Good, please note that it's always worth the trouble to see if there's a tutorial for
the topic present in Sun's tutorial sections. The API documentation also mentions
the link to such a tutorial section if present.

kind regards,

Jos
Dec 13 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: skender | last post by:
Hi everybody, i have a problem with comboboxes. Here's the problem: i need to show a combobox with a list containing a text and a icon. To do this in the first step I add my objects to the combo...
14
by: Tony LaPaso | last post by:
Hi all, I'm not sure if this is an HTML issue or CSS issue but it's weird. I've been working w/XML for 5+ years and it's always been true that you can use a shorthand notation for an empty...
1
by: Alex | last post by:
Acc97 on a subform I have a series of Combo box's which because of my inability to think ahead! I have had to use a field on the last one which I did not intend to use, and bound to it....
6
by: Ron L | last post by:
I have a dataset whose source is a SQL 2k stored procedure that I am trying to display in a datagrid. This datasource has 4 columns that I am interested in here, a text column and 3 value columns...
1
by: louismanukonda | last post by:
I'm newbie to xml I have an xml grammer file contain image files, i have to display the contents with an xsl file. The issue is when i mouseover the images i need to display the corresponding...
1
by: Pugi! | last post by:
I want to create a virtual filesystem. When you open page you see a number of 'folders' represented by a name and an icon. When you click on the icon an xmlhttprequest is send retrieving the...
9
by: Dave | last post by:
Hi guys, I have just set up a duplicate server running: apache 2.54, mysql 5.04 and php 5.04 This is the same setup as as the server we are using now, apart from the hardware inside. I have...
1
by: rirby2 | last post by:
Hi everyone, I'm currently having a rendering problem in IE7 (haven't even tried the lower IEs yet), vs. what I see in FF2. The simplified site can be found at solerasd.com . (There are more...
2
by: swethak | last post by:
Hi, I am getting the problem the problem with google map in Internet Explorer. This map worked fine in mozilla . When i opened the same map in Internet Explorer i am getting the error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.