简体   繁体   中英

Unexpected change in background of header of custome jcombobox in swing

I try to make a custom jcombobox for my menu sidebar my jcombobox has JButton as items because I found it easy to insert and align icon and text. But my problem is when I choose a item in dropdown, the color of the header changes unexpectedly to the default color of component.

public class MyComboBoxRenderer extends JLabel implements ListCellRenderer<Object> {
    private String _title;
    private JButton header;
    public MyComboBoxRenderer(String title) {
        _title = title;
    }

    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value,
            int index, boolean isSelected, boolean hasFocus) {
        JButton item = new JButton();
        item.setOpaque(true);
        item.setBorderPainted(false);
        item.setFocusPainted(false);
        item.setBackground(isSelected ? Frame.LIGHTER_COLOR : Frame.LIGHT_COLOR ); 
        ImageIcon dot = new ImageIcon("image/icon/orange_dot.png");
        if (index == -1){
            item.setText(_title);
            header=item;
            return item; 
        }
        else{
            item.setIcon(dot);
            item.setIconTextGap(30);
            item.setText(value.toString());
            return item;
        }
        
    }
    
    public JButton getHeader(){
        return header;
    }
}
public static class MyComboBoxUI extends BasicComboBoxUI {
        final JButton button= new JButton(EXPAND_ARROW);
        protected void installDefaults() {
            super.installDefaults();
        }

        @Override
        protected JButton createArrowButton() {
            button.setContentAreaFilled(false);
            button.setBorder(null);
            return button;
        }

        @Override
        public void configureArrowButton() {
            super.configureArrowButton();
        }
        
        public JButton getArrowButton(){
            return button;
        }
    }
public class ComboBox extends JComboBox{
    public boolean isExpanded = false;
    private MyComboBoxRenderer renderer;
    public ComboBox(){
        super();
        this.setUI(new MyComboBoxUI());
    }
    public ComboBox(String[] list){
        super(list);
        renderer = new MyComboBoxRenderer("Lists Of Vocab");
        this.setUI(new MyComboBoxUI());
        this.setFont(Frame.I_FONT);
        this.setForeground(Frame.FONT_COLOR);
        this.setBackground(Frame.LIGHT_COLOR);
        this.setRenderer(renderer);
        MyComboBoxUI ui = (MyComboBoxUI) this.getUI();
        JButton arrowButton = ui.getArrowButton();
        arrowButton.addActionListener((ActionEvent e)->{
            isExpanded = !isExpanded;
            if(isExpanded == true){
                arrowButton.setIcon(COLLAPSE_ARROW);
            }
            else{
                arrowButton.setIcon(EXPAND_ARROW);
            }
        });
    }
    public MyComboBoxRenderer getRenderer(){
        return renderer;
    }

I add this combobox to the sideBar

    private void addCheckBoxToSideBar(){
        ComboBox lists = new ComboBox(listNames);
        lists.setAlignmentX(Component.LEFT_ALIGNMENT); // have to have
        lists.addItemListener(new ItemListener(){
            @Override
            public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                CardLayout cl = (CardLayout)(mainPane.getLayout());
                cl.show(mainPane, (String)e.getItem());[enter image description here][1]
            }
        }
    });

        sideBar.add(lists);
    }

This is first:

https://i.stack.imgur.com/ctVnT.png

But when I click to choose an item, it changes to default color:

https://i.stack.imgur.com/KkhhO.png

Those are what I tried but they didn't work:

  • to set the background of the header in the itemStateChanged
  • get the JTextfield of BasicComboBoxEditor of it

I wonder whether updating UI causes this problem but I don't understand much about it.

In your MyComboBoxUI , try overriding this method:

@Override
public void paintCurrentValueBackground(Graphics g,Rectangle bounds,boolean hasFocus)
{
 Color t = g.getColor();
 g.setColor(your_color);
 g.fillRect(bounds.x,bounds.y,bounds.width,bounds.height);
 g.setColor(t);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM