473,834 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem rendering Combo Box with icon near the text

7 New Member
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.Compon ent;
import java.util.HashM ap;
import javax.swing.Ico n;
import javax.swing.Ima geIcon;
import javax.swing.JCo mboBox;
import javax.swing.JLa bel;
import javax.swing.JLi st;
import javax.swing.Lis tCellRenderer;
import javax.swing.UIM anager;
import javax.swing.Win dowConstants;



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

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

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

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

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

initComponents( );
}


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

getContentPane( ).setLayout(new java.awt.FlowLa yout());

setDefaultClose Operation(javax .swing.WindowCo nstants.DISPOSE _ON_CLOSE);
jComboBox1.setM odel(new javax.swing.Def aultComboBoxMod el(modelData));
getContentPane( ).add(jComboBox 1);

jComboBox1.setR enderer(new TypeListCellRen derer(icons));
setDefaultClose Operation( WindowConstants .DISPOSE_ON_CLO SE );

pack();
}

public static void main(String args[]) {

try {
UIManager.setLo okAndFeel(UIMan ager.getSystemL ookAndFeelClass Name());
java.awt.EventQ ueue.invokeLate r(new Runnable() {
public void run() {
new IconComboBox(ne w javax.swing.JFr ame(), true).setVisibl e(true);
}
});
}
catch (Exception e) {
// handle exception
}
}

private class TypeListCellRen derer extends JLabel implements ListCellRendere r {
HashMap<String, Icon> icons;

public TypeListCellRen derer(HashMap<S tring, Icon> icons) {
super();

this.icons = icons;
}

public Component getListCellRend ererComponent( 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.setLo okAndFeel(UIMan ager.getSystemL ookAndFeelClass Name());
Then the code works just fine.

Did someone knows how to solve this look and feel issue ?
Dec 13 '07 #1
5 4149
JosAH
11,448 Recognized Expert MVP
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
wajedali
7 New Member
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 Recognized Expert MVP
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
wajedali
7 New Member
thank you sir now its working.
Dec 13 '07 #5
JosAH
11,448 Recognized Expert MVP
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
1324
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 and forward I catch the onDrawItem event to draw the correct icon based on the properties of the item selected. It's working fine but if I do a stress test on the combo ( i.e. I click very quickly on the combo ) the combo seems blocked for a while...
14
704
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 element like this: "<fred/>" is the same as "<fred></fred>"
1
1517
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. Therefore, I have a two column drop down with the first field showing a Ref No. but the second column showing me what I really want but I can't tap into it " So near yet so far"
6
3771
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 corresponding to permissions to certain data classes. I want to put the permission values in combo boxes in the grid and instead of displaying the numeric values, have the combo box display a string that corresponds to the numeric value (i.e. No...
1
2473
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 images from that xml file which i'm not able to get. I'm giving piece of code of xml and xsl files.Could u please tell me how can I get that work done
1
1269
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 subfolders and files. When you click on thus created representation of a subfolder a new xmlhttprequest is send to the server to retrieve the subfolders and files for that subfolder. On the serverside I create a domdocument (php) which contains...
9
1696
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 copied across the database and website, with exact same permissions as the first server. The problem is that part of the php code is executing but others
1
3131
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 image elements that belong in the list but I've only posted 2 since it replicates the problem and minimizes the code to read. In FF, you should see a logo with two images (currently not HREF's) of the same size below it, and a copyright statement below...
2
3159
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 message as in alert box as " Internet Explorer cannot open the Internet site http://google.citycarrentals.com.au/viewalllocations.php . Operation aborted". It is working in Mozilla . Here i mentioned my code . I am facing this problem several...
0
9796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10786
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10503
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10544
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10214
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9326
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7754
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3079
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.