Graphics object in JComboBox

Hi, I got the following code from the sun website. It uses something

called ImageIcon to put images into a combo box. I prefer to use

graphic objects (rect). Is there something called GraphicIcon,

and if so how would I modify the following code to use it?

Thanks in advance......

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class CustomComboBoxDemo extends JPanel {

ImageIcon images[];

public CustomComboBoxDemo() {

//Load the pet images

String[] petStrings = {"Bird", "Cat", "Dog", "Rabbit", "Pig"};

images = new ImageIcon[petStrings.length];

for (int i = 0; i < petStrings.length; i++) {

images = new ImageIcon("images/" + petStrings + ".gif");

images.setDescription(petStrings);

}

// Create the combo box

JComboBox petList = new JComboBox(images);

ComboBoxRenderer renderer= new ComboBoxRenderer();

renderer.setPreferredSize(new Dimension(200, 130));

petList.setRenderer(renderer);

petList.setMaximumRowCount(3);

// Layout the demo

setLayout(new BorderLayout());

add(petList, BorderLayout.NORTH);

setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

}

public static void main(String s[]) {

JFrame frame = new JFrame("CustomComboBoxDemo");

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {System.exit(0);}

});

frame.getContentPane().add(new CustomComboBoxDemo(),

BorderLayout.CENTER);

frame.pack();

frame.setVisible(true);

}

class ComboBoxRenderer extends JLabel implements ListCellRenderer {

public ComboBoxRenderer() {

setOpaque(true);

setHorizontalAlignment(CENTER);

setVerticalAlignment(CENTER);

}

public Component getListCellRendererComponent(

JList list,

Object value,

int index,

boolean isSelected,

boolean cellHasFocus) {

if (isSelected) {

setBackground(list.getSelectionBackground());

setForeground(list.getSelectionForeground());

} else {

setBackground(list.getBackground());

setForeground(list.getForeground());

}

ImageIcon icon = (ImageIcon)value;

setText(icon.getDescription());

setIcon(icon);

return this;

}

}

}

[2438 byte] By [aether1] at [2007-9-26 1:21:57]
# 1

Here's a suggestion.

Create a new java.awt.Image: Image img = createImage(16,16);

(this will create a 16x16-pixel image)

Get the Graphics object from it:Graphics g = img.getGraphics();

Draw on the graphics object:

g.drawLine(0,0,15,15);

g.drawLine(0,15,15,0);

Make an ImageIcon of the Image: ImageIcon icon=new ImageIcon(img);

The ImageIcon has now a cross in it.

jsalonen at 2007-6-29 0:58:19 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thats cool....when I modified the code with your ideas

it compiled....but the only way I know to test it is

to put it in an applet such as:

(unfortunately this didnt compile, it wouldn't let me

declare a CustomComboBoxDemo object, and it

didnt like my use of the getValue method I threw

into CustomComboBoxDemo)

import java.awt.*;

import java.applet.*;

import java.util.*;

import java.lang.String;

import java.awt.event.*;

public class newColorChoose extends java.applet.Applet implements ItemListener {

Button colorButton = new Button();

String mycolor;

CustomComboBoxDemo myCombobox = new CustomComboBoxDemo();

public void init() {

this.setLayout(null);

myCombobox.setBounds(0,0,140,24);

colorButton.setBounds(140,0,210,24);

add(myCombobox);

add(colorButton);

colorButton.setBackground(Color.white);

myCombobox.addItemListener(this);

}

public void itemStateChanged(ItemEvent e){

Color currentcolor = Color.decode(getValue());

colorButton.setBackground(currentcolor);

}

}

aether1 at 2007-6-29 0:58:19 > top of Java-index,Desktop,Core GUI APIs...
# 3
Didn't compile... Are you sure you had the class CustomComboBoxDemo in your classpath (= eg. in the same directory as the applet)?It's also suspicious to mix awt and swing components, though that wont affect compiling...
jsalonen at 2007-6-29 0:58:19 > top of Java-index,Desktop,Core GUI APIs...
# 4

Yes, I figured out it was a classpath problem too,

so I've been spending the last few hours trying

everything I can find to set and reset the classpath,

no luck. Finally I tried compiling with -classpath

and that seemed to work, I got back the prompt

without any errors, but when I went to look for

the class file...It wasn't there! Serached for it..

no luck! I'm totally stymied now and am begining

to wonder if there is a problem with my installation.

Do I have to install the J2 Runtime Environment as

well as the J2 SDK for this to work?

aether1 at 2007-6-29 0:58:19 > top of Java-index,Desktop,Core GUI APIs...
# 5
What is the value of classpath when you type the set command in dos prompt? It should contain the path ".", the current working directory. If it does not, add "." to your classpath; the commandset classpath=.;%classpath%should fix it temporarily.
jsalonen at 2007-6-29 0:58:19 > top of Java-index,Desktop,Core GUI APIs...
# 6

Thanks for the help but that didnt work. The value

of classpath when I type the set command is:

classpath=.;.;C:\Program Files\Photodeluxe 2.0\Adobe Connectables

Is this wrong? I used to have jdk1.8.1 and had the classpath

set right for that but ever since I downloaded 1.3.1 my class

path has been messed up.

aether1 at 2007-6-29 0:58:19 > top of Java-index,Desktop,Core GUI APIs...
# 7
No, it's not wrong. Your classpath seems just fine. What is the error message you keep getting?
jsalonen at 2007-6-29 0:58:19 > top of Java-index,Desktop,Core GUI APIs...
# 8

The error messages I'm getting are:

C:\>set classpath=%classpath%;.

C:\>cd jdk1.3.1

C:\jdk1.3.1>javac newColorChoose.java

newColorChoose.java:26: cannot resolve symbol

symbol : method addItemListener (newColorChoose)

location: class CustomComboBoxDemo

myCombobox.addItemListener(this);

^

newColorChoose.java:31: cannot resolve symbol

symbol : method getValue ()

location: class newColorChoose

Color currentcolor = Color.decode(getValue());

^

2 errors

Your point about the AWT applet not being compatible with swing is well

taken and in fact my java book agrees with you. As such I'm going

to try to solve the problem by combining both source codes into

one and use jApplet instead. This may be a temporary solution and

as far as my classpath goes, I may run into the same problem in the

future but I guess I will deal with that when I come to it. Thanks

for all your help js....you are definately a knowledgeable programmer!

Best Regards...aether1

aether1 at 2007-6-29 0:58:19 > top of Java-index,Desktop,Core GUI APIs...
# 9

Ah. Now I see. The compiler can't find inexistent methods. CustomComboBoxDemo is a subclass of JPanel and the methods addItemListener isn't defined anywhere. OTOH, the method getValue() isn't defined anywhere in the applet either...

Solution to problem 1: add the following method to CustomComboBoxDemo:public void addItemListener(ItemListener itmlsnr) {

myCombobox.addItemListener(itmlsnr);

}

What is getValue supposed to do?

jsalonen at 2007-6-29 0:58:19 > top of Java-index,Desktop,Core GUI APIs...
# 10

Actually I omitted that part of the code by accident. Here

is the full code of CustomComboBoxDemo as it now stands...

(the getValue method is at the end):

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class CustomComboBoxDemo extends JPanel {

String mycolor;

public CustomComboBoxDemo() {

//

String[] colorString = {"FF0000", "009900", "0000FF", "CCFF00", "CC9900"};

ImageIcon myIcon[] = new ImageIcon[colorString.length];

for (int i = 0; i < colorString.length; i++){

Image myImage = createImage(10,20);

Graphics g = myImage.getGraphics();

g.drawRect(0,0,20,10);

Color CurrentColor = Color.decode(colorString);

g.setColor(CurrentColor);

g.fillRect(0,0,20,10);

myIcon = new ImageIcon(myImage);

}

// Create the combo box

JComboBox colorList = new JComboBox(myIcon);

ComboBoxRenderer renderer= new ComboBoxRenderer();

renderer.setPreferredSize(new Dimension(50, 20));

colorList.setRenderer(renderer);

colorList.setMaximumRowCount(10);

// Layout the demo

setLayout(new BorderLayout());

add(colorList, BorderLayout.NORTH);

setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

}

public static void main(String s[]) {

JFrame frame = new JFrame("Pick a Color!");

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {System.exit(0);}

});

frame.getContentPane().add(new CustomComboBoxDemo(),

BorderLayout.CENTER);

frame.pack();

frame.setVisible(true);

}

class ComboBoxRenderer extends JLabel implements ListCellRenderer {

public ComboBoxRenderer() {

setOpaque(true);

setHorizontalAlignment(CENTER);

setVerticalAlignment(CENTER);

}

public Component getListCellRendererComponent(

JList list,

Object value,

int index,

boolean isSelected,

boolean cellHasFocus) {

if (isSelected) {

setBackground(list.getSelectionBackground());

setForeground(list.getSelectionForeground());

} else {

setBackground(list.getBackground());

setForeground(list.getForeground());

}

ImageIcon icon = (ImageIcon)value;

mycolor = icon.getDescription();

setText(icon.getDescription());

setIcon(icon);

return this;

}

}

public String getValue(){

return mycolor;

}

}

This is intended to go in a html form page. Using javascript, I know that

I can access the getValue method to find what value a user chose. Its

fairly simple but if you ever have need for it it looks like this:

<script LANGUAGE="JavaScript">

function getColor(){

var color = document.newColorChoose.getValue();

return true;

}

</script>

aether1 at 2007-6-29 0:58:19 > top of Java-index,Desktop,Core GUI APIs...
# 11
Well, thenColor currentcolor = Color.decode(getValue());should beColor currentcolor = Color.decode(myCombobox.getValue());in the applet.Are you sure that the script should work?
jsalonen at 2007-6-29 0:58:19 > top of Java-index,Desktop,Core GUI APIs...
# 12
Yes, I've tested it and the script will work, but onlyif there is a method in the applet called getValue.........
aether1 at 2007-6-29 0:58:19 > top of Java-index,Desktop,Core GUI APIs...