self render combo box
HI,
I have to make some self render combo box, with image drawing inside as item. Can some of you give me some examples?
The other problem is, if I make the combo size larger e.g. 180x70, the down triangle in right hand side become larger , it's quite ugly in UI....
any method can I set the triangle smaller, and let the item content larger ?
Thanks for help. It would be great if you can give me some example.
# 1
> I have to make some self render combo box, with image drawing inside as item.
The Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html]How to Use Combo Boxes[/url] shows you how to render an image.
> if I make the combo size larger e.g. 180x70, the down triangle in right hand side become larger
> It would be great if you can give me some example.
Actually you should post your code so we can see the problem.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.
# 2
Thanks for your reply camickr .
I will try to read the the Link you provided.
For the problem of ComboBox size, maybe I described bad... coz my english is not good.
> if I make the combo size larger e.g. 180x70, the down triangle in right hand side become larger
what I did is , my program UI needs a larger ComboBox to list some items, so I just resize my JcomboBox to larger, e.g to a rectangle size 180 x 80.
but I saw the dropdown symbo (the down triangle in right handside) is automatically change to larger, it seems always take the height as reference and form a square.
I want to make the triangle smaller and thinner... but don;t know how to do....
I hope his information will make you understand...
Thanks
# 3
Now I have an idea, Can JComboBox add JPanel as items?
I have made some panels before, they have already implemented OnPaint. and so that draw a correct content in my wish.
I would like to put these JPanel into Jcombo, is it possible ?
I already tried to implement ListCellRenderer... but don't know how to do ...
ComboBoxRenderer renderer = new ComboBoxRenderer();
renderer.setPreferredSize(new Dimension(150, 110));
jComboBox1.setRenderer(renderer);
FoldChildPanel p = new FoldChildPanel();
this.jComboBox1.addItem(p);
.........
........
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)
{
FoldChildPanel selectedObj = ((FoldChildPanel)value);
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
//selectedObj.paint(this.getGraphics());
return this;
}
}
# 4
> I would like to put these JPanel into Jcombo, is it possible ?
Read tha API. The addItem(...) method accepts and Object, so you can add any Object to a combo box.
> I already tried to implement ListCellRenderer... but don't know how to do ...
Well you can't add a JPanel to a JLabel so you just use the panel itself as the renderer:
class PanelRenderer implements ListCellRenderer
{
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus)
{
JPanel renderer = (JPanel)value;
// set background, foreground here if you want
return renderer
}
}
# 5
Thanks so much..
Now the panels can be added into the JCombo. But I still have some questions:
1. If my child panel items should be in size of 150x110, how can I make sure the visible area of Combo item is in this size?
I have tried to use setPreferredSize(), but still dose'nt work.
ComboBoxRenderer renderer = new ComboBoxRenderer();
renderer.setPreferredSize(new Dimension(150, 110));
jComboBox1.setRenderer(renderer);
jComboBox1.setMaximumRowCount(3);
2. How can I adjust / minimize the Arrow Button of JCombo?
It's too large if I sized the Combo to larger.
Thanks again for help.
# 6
Hi,
I have fixed the above 2 questions, I just found a method to customize the Arrow Button by using subClass of BasicComboBoxUI.
Now I got a problem on using my self renderer. The renderer has problem on drawing item if there is no item added into JCombo. Is there any mistakes I made in my renderer ?
Here is the code :
class ComboBoxRenderer extends JPanel implements ListCellRenderer
{
public ComboBoxRenderer()
{
}
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
ChildPanel selectedObj = ((ChildPanel) value);
selectedObj.SetSelected(isSelected); // control method
return selectedObj;
}
}
And also, How can I change the selection Color for this combo ?
I have tried to use the following code in my ComboBoxRenderer , but not works fine...
if (isSelected) {
setBackground(Color.pink);