“Unexpected change in background of header of custom JComboBox in Swing”

huangapple go评论153阅读模式
英文:

Unexpected change in background of header of custome jcombobox in swing

问题

我尝试为我的菜单侧边栏创建一个自定义的下拉框(jcombobox)。
我的下拉框中的项是JButton,因为我发现这样更容易插入和对齐图标和文本。
但我的问题是,当我选择一个下拉项时,标题的颜色意外地更改为组件的默认颜色。

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;
	}
}

我将这个下拉框添加到了侧边栏中:

private void addCheckBoxToSideBar(){
	ComboBox lists = new ComboBox(listNames);
	lists.setAlignmentX(Component.LEFT_ALIGNMENT); // 必须设置
	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());
			}
		}
	});

	sideBar.add(lists);
}

这是初始状态:

初始状态图片

但当我点击选择一个项时,它会变成默认颜色:

选择项后图片

以下是我尝试但未成功的方法:

  • itemStateChanged中设置标题的背景颜色
  • 获取BasicComboBoxEditor的JTextfield

我想知道是否更新UI引起了这个问题,但我对此不太了解。

英文:

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&lt;Object&gt; {
	private String _title;
	private JButton header;
	public MyComboBoxRenderer(String title) {
		_title = title;
	}

	@Override
	public Component getListCellRendererComponent(JList&lt;?&gt; 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(&quot;image/icon/orange_dot.png&quot;);
		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(&quot;Lists Of Vocab&quot;);
		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)-&gt;{
			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.

答案1

得分: 0

在你的 MyComboBoxUI 中,尝试覆盖这个方法:

@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);
}
英文:

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);
}

huangapple
  • 本文由 发表于 2020年8月1日 11:28:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63201468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定