• Post Reply Bookmark Topic Watch Topic
  • New Topic

adding icon and text to a combobox  RSS feed

 
Ranch Hand
Posts: 114
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some what difficult to know where to start with this question, as i have an answer but not one that i can implement in netbeans which is probably the main question

there are a few good tutorials on this subject however the all have one thing in common the combobox if created at runtime and not by and ide

the problem is that netbeans creates the combobox but then there seems there is no way of applying the necessary code to make it work



the code generated by netbeans



I have managed to get the text portion into the combobox by selection custom code to give


can some please tell me how ai get arround this problem more over explain what i need to do to
 
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

peter m hayward wrote:some what difficult to know where to start with this question, as i have an answer but not one that i can implement in netbeans which is probably the main question

there are a few good tutorials on this subject however the all have one thing in common the combobox if created at runtime and not by and ide

the problem is that netbeans creates the combobox but then there seems there is no way of applying the necessary code to make it work



the code generated by netbeans



I have managed to get the text portion into the combobox by selection custom code to give


can some please tell me how ai get arround this problem more over explain what i need to do to



This page explains it:

https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer

What you need to do is write a ComboBoxRenderer which extends JLabel  and implements ListCellRenderer. This will mean the JComboBox will use the JLabel as its renderer. JLabel has a setIcon method which sets its icon, so use that to set the icon.

 
peter m hayward
Ranch Hand
Posts: 114
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Simkulette wrote:

peter m hayward wrote:some what difficult to know where to start with this question, as i have an answer but not one that i can implement in netbeans which is probably the main question

there are a few good tutorials on this subject however the all have one thing in common the combobox if created at runtime and not by and ide

the problem is that netbeans creates the combobox but then there seems there is no way of applying the necessary code to make it work



the code generated by netbeans



I have managed to get the text portion into the combobox by selection custom code to give


can some please tell me how ai get arround this problem more over explain what i need to do to



This page explains it:

https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer

What you need to do is write a ComboBoxRenderer which extends JLabel  and implements ListCellRenderer. This will mean the JComboBox will use the JLabel as its renderer. JLabel has a setIcon method which sets its icon, so use that to set the icon.



thanks for trying to help the tutorial only refers to comboboxes that are not created by netbeans

however after reading lots of articles on the subject i now have a working solution which i am now posting and should do so before now

the answer is to put this custom code inside the combobox by select custom code in neatbeans


the array contains the names of the jpegs
then you can use ComboBoxRenderer like this


you can also add a resource folder to the project and put the jpegs there instead of on the C drive
and use getSource(); to pull in each jpeg from folder

hope this helps other trying to do this in netbeans which is not covered in The Java™ Tutorials









 
Rancher
Posts: 2852
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never do I/O in a renderer.

What you want to do is create a custom Object that contains 2 properties:

1. text
2. icon

Then you add this object to the combo box.

The renderer will then use the text/icon from this object to set the text/icon of your renderer.

Regarding NetBeans, my advice is to NOT use the IDE to generate your code. You are spending time learning the IDE and not Java. Also, if you ever move to a different IDE the code will not be maintainable in the other IDE.

So learn to write your own classes and just use the IDE as a tool for compiling, debugging etc.

 
peter m hayward
Ranch Hand
Posts: 114
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:Never do I/O in a renderer.

What you want to do is create a custom Object that contains 2 properties:

1. text
2. icon

Then you add this object to the combo box.

The renderer will then use the text/icon from this object to set the text/icon of your renderer.

Regarding NetBeans, my advice is to NOT use the IDE to generate your code. You are spending time learning the IDE and not Java. Also, if you ever move to a different IDE the code will not be maintainable in the other IDE.

So learn to write your own classes and just use the IDE as a tool for compiling, debugging etc.





Thank you for your reply can you expand upon the Never do I/O in a renderer as this is only a replacement for the resources folder in the tutorial is that not using IO ?

with regards to a custom object are you able to expand upon this also as i though rightly or wrongly that public Component was the object ?

please try to help me better understand the issues and what should and should not be done and why




 
Rob Camick
Rancher
Posts: 2852
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you for your reply can you expand upon the Never do I/O in a renderer



Why would you keep reading the image every time the user clicks on the combo box???

If you have 3 items then you do 3 I/O's. If you have 10 item's you do 10 I/o's. That is not very efficient.

The point is to read the image once, at the start of your class (or when you create the custom object as I suggested).

i though rightly or wrongly that public Component was the object



You are adding 3 strings to the combo box. So each string is only one piece of information.

I suggested you add a custom object which contains 2 pieces of information. Then when you render the object you use both pieces of data.

See Combo Box With Hidden Data. The example shows how to add a custom object to the combo box. The concept is slightly different in that one piece of data in the object is used for display in the combo box and the other for processing in the program. In your case both pieces of data will be used for display.
 
peter m hayward
Ranch Hand
Posts: 114
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:

Thank you for your reply can you expand upon the Never do I/O in a renderer



Why would you keep reading the image every time the user clicks on the combo box???

If you have 3 items then you do 3 I/O's. If you have 10 item's you do 10 I/o's. That is not very efficient.

The point is to read the image once, at the start of your class (or when you create the custom object as I suggested).

i though rightly or wrongly that public Component was the object



You are adding 3 strings to the combo box. So each string is only one piece of information.

I suggested you add a custom object which contains 2 pieces of information. Then when you render the object you use both pieces of data.

See Combo Box With Hidden Data. The example shows how to add a custom object to the combo box. The concept is slightly different in that one piece of data in the object is used for display in the combo box and the other for processing in the program. In your case both pieces of data will be used for display.


thank you i will follow your suggestion
 
Author
Ranch Hand
Posts: 959
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

peter m hayward wrote:can you expand upon the Never do I/O in a renderer



Because I/O is slow and renderers are intended to be fast. And even if I/O weren't slow, it would still be wasteful to call createImageIcon() more than once per image. And a renderer can and will be called many many times for each item in your list.

Mr. Camick's suggestion to make type of your ComboBox be a compound object that holds both text and an icon is sound. That's a good solid way to write your program.

Here's another way, where the type of the ComboBox can remain a simple String:

This way you load all your images up front, like this:
 
Brian Cole
Author
Ranch Hand
Posts: 959
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

peter m hayward wrote:can you expand upon the Never do I/O in a renderer


Because I/O is slow and renderers are intended to be fast. And even if I/O weren't slow, it would still be wasteful to call createImageIcon() more than once per image. And a renderer can and will be called many many times for each item in your list.

Mr. Camick's suggestion to make type of your ComboBox be a compound object that holds both text and an icon is sound. That's a good solid way to write your program.

Here's another way, where the type of the ComboBox can remain a simple String:

This way you load all your images up front, like this:
 
peter m hayward
Ranch Hand
Posts: 114
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brian Cole wrote:

peter m hayward wrote:can you expand upon the Never do I/O in a renderer



Because I/O is slow and renderers are intended to be fast. And even if I/O weren't slow, it would still be wasteful to call createImageIcon() more than once per image. And a renderer can and will be called many many times for each item in your list.

Mr. Camick's suggestion to make type of your ComboBox be a compound object that holds both text and an icon is sound. That's a good solid way to write your program.

Here's another way, where the type of the ComboBox can remain a simple String:

This way you load all your images up front, like this:


thank you for you reply it would seem that i am out of my depth and have a lot to read up on. I am still trying to full understand Mr Camick's suggestion i guess i have a lack of understand how to create such objects. it is quite possible that i need to take up a course / tutorials on the subject any suggestions
 
peter m hayward
Ranch Hand
Posts: 114
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please don't quote the whole of a preceding post; only quote the relevant parts.


sorry did not relise, can you tell where is the best place to get tutorials form, it was 2007 when did this a university and feel i need to recap on stuff and expand my knowledge of JAVA
 
Rob Camick
Rancher
Posts: 2852
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can you tell where is the best place to get tutorials form



You were already given that information in the first answser. You were given a link to the Swing tutorial that explained what a renderer is.

For general Java tutorials start with: Java Tutorials.

I am still trying to full understand Mr Camick's suggestion i guess i have a lack of understand how to create such objects.



This is generally one of the first things you do when learning Java. You create a class that holds data and you have getter and setter methods to access the data.

 
peter m hayward
Ranch Hand
Posts: 114
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:

can you tell where is the best place to get tutorials form



You were already given that information in the first answser. You were given a link to the Swing tutorial that explained what a renderer is.

For general Java tutorials start with: Java Tutorials.

I am still trying to full understand Mr Camick's suggestion i guess i have a lack of understand how to create such objects.



This is generally one of the first things you do when learning Java. You create a class that holds data and you have getter and setter methods to access the data.


this just goes to show if you dont do it for a while you forget things of course i now get it just kept thinking about an object and not connecting with the class so i will go trough the two part process and create a class (object) and get back to you
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!