Add Divider to JComboBox

Hello,

I was wondering how I can add a divider after I add each symbol to this JComboBox? Is JSeparator a good idea?

Please help out.

import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JSeparator;

publicclass MyComboBoxDividerTestextends JComboBox{

private DefaultComboBoxModel myModel =new DefaultComboBoxModel();

public MyComboBoxDividerTest()

{

setModel(myModel);

setEditable(true);

}

publicvoid generateComboBox()

{

ArrayList list =new ArrayList();

list.add("symbol1");

list.add("symbol2");

list.add("symbol3");

list.add("symbol4");

list.add("symbol5");

JFrame frame =new JFrame("My ComboBox");

for(int i=0;i<list.size();i++)

{

myModel.addElement(list.get(i));

//TODO: ADD SEPARATOR HERE (I think)

}

JComboBox comboBox =new JComboBox(myModel);

frame.getContentPane().add(comboBox);

frame.setSize(200,55);

frame.setVisible(true);

}

publicstaticvoid main(String[] args){

// TODO Auto-generated method stub

MyComboBoxDividerTest test =new MyComboBoxDividerTest();

test.generateComboBox();

}

}

>

[2618 byte] By [programmer_girla] at [2007-11-27 8:48:27]
# 1

You dont need to add a JSeparator at all. This is not a model level issue but a renderer level issue. In other words you'll have to modify you JComboBox cell renderer instead of its model

From the ListCellRenderer documentation with modifications. Check Out the commented addition:

class MyCellRenderer extends JLabel implements ListCellRenderer {

public MyCellRenderer() {

setOpaque(true);

}

public Component getListCellRendererComponent(JList list,

Object value,

int index,

boolean isSelected,

boolean cellHasFocus) {

setText(value.toString());

// you can draw the line using a MatteBorder

// you may need to implement a CompoundBorder to get add spacing around the text using an EmptyBorder

setBorder( new MatteBorder(0,0,2,0, Color.black) ); //

Color background;

Color foreground;

// check if this cell represents the current DnD drop location

JList.DropLocation dropLocation = list.getDropLocation();

if (dropLocation != null

&& !dropLocation.isInsert()

&& dropLocation.getIndex() == index) {

background = Color.BLUE;

foreground = Color.WHITE;

// check if this cell is selected

} else if (isSelected) {

background = Color.RED;

foreground = Color.WHITE;

// unselected, and not the DnD drop location

} else {

background = Color.WHITE;

foreground = Color.BLACK;

};

setBackground(background);

setForeground(foreground);

return this;

}

}

// once you have the Renderer, connect it to your JComboBox

JComboBox box = new JComboBox();

box.setRenderer( new MyCellRenderer() );

Cheers, ICE

icewalker2ga at 2007-7-12 20:55:30 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks so much! I tried it and it works.Could you also let me know how I can add this line only after the last element? That's the next thing I need to do.Thanks!
programmer_girla at 2007-7-12 20:55:30 > top of Java-index,Desktop,Core GUI APIs...
# 3

Simply do a check using an IF condition for the last row before setting the border. Here is an example to replace the line of code

// check if the current row (index / cell) being painted is the last

if(index == list.getModel().getRowCount() - 1) {

setBorder( new MatteBorder(0,0,2,0, Color.black) );

}

ICE

icewalker2ga at 2007-7-12 20:55:30 > top of Java-index,Desktop,Core GUI APIs...
# 4

Hello again, thanks I tried your solution and it helped.

But now I realize, what I actually want to do is separate symbols that start with a Character (A-Za-z) and symbols that start with numbers in brackets (e.g. [1]). Is setBorder still a good way to do this or do you suggest something else?

Thanks!

programmer_girla at 2007-7-12 20:55:30 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thanks to those of you who looked at this. I actually figured it out, I was staring at the solution the whole time. Thank you ICE!Message was edited by: programmer_girl
programmer_girla at 2007-7-12 20:55:30 > top of Java-index,Desktop,Core GUI APIs...
# 6
> Thanks to those of you who looked at this. I> actually figured it out, I was staring at the> solution the whole time. Thank you ICE!You are going to give ice some Dukes, right?
petes1234a at 2007-7-12 20:55:30 > top of Java-index,Desktop,Core GUI APIs...
# 7
hehe of course :)
programmer_girla at 2007-7-12 20:55:30 > top of Java-index,Desktop,Core GUI APIs...
# 8

Hello again,

So I actually have another question. How do you put the divider right above the first symbol? This is what my comboBox needs to look like at the end of the day. I don't think you can check with the index. But eventually, when I open up the renderer, I need to see the divider on top of all the symbols added.

Thanks for all your help. Looking forward to hearing from you.

programmer_girla at 2007-7-12 20:55:30 > top of Java-index,Desktop,Core GUI APIs...
# 9

> I don't think you can check with the index

And why cant you check with the index? You only need to check for the first item and paint the border above it.

// this can be in CellRenderer code

if(index == 0) {

setBorder( new MatteBorder(2,0,0,0, Color.black) );

}

ICE

icewalker2ga at 2007-7-12 20:55:30 > top of Java-index,Desktop,Core GUI APIs...
# 10
That's what I did and it painted the border below the first index.
programmer_girla at 2007-7-12 20:55:30 > top of Java-index,Desktop,Core GUI APIs...
# 11

ok I see how it works now. In the MatteBorder I used to have 0,0,2,0 instead of 2,0,0,0. Now it works, but there is one more problem. If I enter new symbols to the symbolsComboBox, I want the divider to stay where it was before, not move to top of the new index=0.

What do you suggest I do?

Thanks.

programmer_girla at 2007-7-12 20:55:30 > top of Java-index,Desktop,Core GUI APIs...
# 12

> If I enter new symbols to the symbolsComboBox, I want the divider

> to stay where it was before, not move to top of the new index=0.

Then in that case you are going to have to keep track of which items you want the divider placed above. This can be done using a Vector or some other Collection implementation in your cell renderer. Hence, if an item in the ComboBox list is also present in the Collection, then you paint the border above it.

I really dont know how your code and application looks like so I cant give you the best solution for this problem

ICE

icewalker2ga at 2007-7-12 20:55:30 > top of Java-index,Desktop,Core GUI APIs...