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 ?
5 4148 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
sir i had tried with Label but it not working can u plz give me solution i.e.code
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
thank you sir now its working.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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>"
|
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"
|
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...
|
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
| |
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...
|
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
|
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...
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |