tags:

views:

664

answers:

2

Hi all,

I would like to create JComboBox control similar with the URL textbox of Firefox. Does anyone know how to customize the textfield of the JComboBox. I want to add some icons on the ALIGN.HORIZONTAL_RIGHT near the arrow button of the JComboBox

Thanks, Minh

+4  A: 

To change how a component renders, you generally work with what are called Renderers.

For combobox, look at how to create a custom combobox renderer. Just a quick glance, but for your case, a simple configuration of DefaultListCellRenderer may be enough, since you can set the JLabel properties to position the text to the image. If not, just extend from it.

Remember also that you have to set a model that includes the icon so that the combobox renderer can get it - might want to do a custom ComboBoxModel too, depending on your data object.

aberrant80
Hi aberrant80,Thanks for your quick answer. I checked out the DefaultListCellRenderer, that will help me to customize the list item like Firefox.But the other one to add the icon in the JComboBox textbox, i will use custom painting to draw an icon that should be on the right of the control.http://java.sun.com/docs/books/tutorial/uiswing/painting/index.htmlThanks,Minh
Minh
@minh, for the text box, you should see what you can do by creating a custom editor. there is some mention in the tutorial @aberrant80 posted.
akf
DefaultListCellRenderer is essentially a JLabel. JLabel supports an icon in addition to the normal text. My guess is that it should simply retrieving the renderer and then setting the appropriate icon-to-text positioning/alignment andit might work.
aberrant80
it seems that @minh wants an editable combo box. the renderer will only solve the common case of displaying the uneditable list data. the problem still exists for the text editor. that is where my custom editor suggestion is coming from.
akf
Ah right, I missed that. Minh, in the Sun's tutorial link I provided, just one section above, it explains about editable comboboxes. Doing setEditable(true) just might also achieve what you want, if you want it to be simply editable.
aberrant80
A: 

Hi aberrant80, akf

Thanks for your very detail explanation. Actually i will combine DefaultListCellRenderer and add the icon to the combo box like following code

import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main extends JFrame {
    public Main() {
        // Create icon "C"
        JButton jb = new JButton("C");
        jb.setMargin(new Insets(0, 0, 0, 0));
        jb.setBounds(245, 2, 18, 18);

        // Create combo box
        String[] languages = new String[]{"Java", "C#", "PHP"};
        JComboBox jc = new JComboBox(languages);
        // jc.setEditable(true);
        jc.add(jb);

        getContentPane().add(jc);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(new Dimension(300, 58));
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final Main main = new Main();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                main.setVisible(true);
            }
        });
    }
}

But when i put jc.setEditable(true); the combo editor hided my icon. I'm thinking another way to simulate Firefox awesome bar. Do you have any idea for this?

Thanks for your help,

Minh

Minh