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

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

Unexpected change in background of header of custome jcombobox in swing

问题

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

  1. public class MyComboBoxRenderer extends JLabel implements ListCellRenderer<Object> {
  2. private String _title;
  3. private JButton header;
  4. public MyComboBoxRenderer(String title) {
  5. _title = title;
  6. }
  7. @Override
  8. public Component getListCellRendererComponent(JList<?> list, Object value,
  9. int index, boolean isSelected, boolean hasFocus) {
  10. JButton item = new JButton();
  11. item.setOpaque(true);
  12. item.setBorderPainted(false);
  13. item.setFocusPainted(false);
  14. item.setBackground(isSelected ? Frame.LIGHTER_COLOR : Frame.LIGHT_COLOR );
  15. ImageIcon dot = new ImageIcon("image/icon/orange_dot.png");
  16. if (index == -1){
  17. item.setText(_title);
  18. header=item;
  19. return item;
  20. }
  21. else{
  22. item.setIcon(dot);
  23. item.setIconTextGap(30);
  24. item.setText(value.toString());
  25. return item;
  26. }
  27. }
  28. public JButton getHeader(){
  29. return header;
  30. }
  31. }
  1. public static class MyComboBoxUI extends BasicComboBoxUI {
  2. final JButton button = new JButton(EXPAND_ARROW);
  3. protected void installDefaults() {
  4. super.installDefaults();
  5. }
  6. @Override
  7. protected JButton createArrowButton() {
  8. button.setContentAreaFilled(false);
  9. button.setBorder(null);
  10. return button;
  11. }
  12. @Override
  13. public void configureArrowButton() {
  14. super.configureArrowButton();
  15. }
  16. public JButton getArrowButton(){
  17. return button;
  18. }
  19. }
  1. public class ComboBox extends JComboBox {
  2. public boolean isExpanded = false;
  3. private MyComboBoxRenderer renderer;
  4. public ComboBox() {
  5. super();
  6. this.setUI(new MyComboBoxUI());
  7. }
  8. public ComboBox(String[] list) {
  9. super(list);
  10. renderer = new MyComboBoxRenderer("Lists Of Vocab");
  11. this.setUI(new MyComboBoxUI());
  12. this.setFont(Frame.I_FONT);
  13. this.setForeground(Frame.FONT_COLOR);
  14. this.setBackground(Frame.LIGHT_COLOR);
  15. this.setRenderer(renderer);
  16. MyComboBoxUI ui = (MyComboBoxUI) this.getUI();
  17. JButton arrowButton = ui.getArrowButton();
  18. arrowButton.addActionListener((ActionEvent e) -> {
  19. isExpanded = !isExpanded;
  20. if(isExpanded == true){
  21. arrowButton.setIcon(COLLAPSE_ARROW);
  22. }
  23. else{
  24. arrowButton.setIcon(EXPAND_ARROW);
  25. }
  26. });
  27. }
  28. public MyComboBoxRenderer getRenderer(){
  29. return renderer;
  30. }
  31. }

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

  1. private void addCheckBoxToSideBar(){
  2. ComboBox lists = new ComboBox(listNames);
  3. lists.setAlignmentX(Component.LEFT_ALIGNMENT); // 必须设置
  4. lists.addItemListener(new ItemListener(){
  5. @Override
  6. public void itemStateChanged(ItemEvent e) {
  7. if (e.getStateChange() == ItemEvent.SELECTED) {
  8. CardLayout cl = (CardLayout)(mainPane.getLayout());
  9. cl.show(mainPane, (String)e.getItem());
  10. }
  11. }
  12. });
  13. sideBar.add(lists);
  14. }

这是初始状态:

初始状态图片

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

选择项后图片

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

  • 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.

  1. public class MyComboBoxRenderer extends JLabel implements ListCellRenderer&lt;Object&gt; {
  2. private String _title;
  3. private JButton header;
  4. public MyComboBoxRenderer(String title) {
  5. _title = title;
  6. }
  7. @Override
  8. public Component getListCellRendererComponent(JList&lt;?&gt; list, Object value,
  9. int index, boolean isSelected, boolean hasFocus) {
  10. JButton item = new JButton();
  11. item.setOpaque(true);
  12. item.setBorderPainted(false);
  13. item.setFocusPainted(false);
  14. item.setBackground(isSelected ? Frame.LIGHTER_COLOR : Frame.LIGHT_COLOR );
  15. ImageIcon dot = new ImageIcon(&quot;image/icon/orange_dot.png&quot;);
  16. if (index == -1){
  17. item.setText(_title);
  18. header=item;
  19. return item;
  20. }
  21. else{
  22. item.setIcon(dot);
  23. item.setIconTextGap(30);
  24. item.setText(value.toString());
  25. return item;
  26. }
  27. }
  28. public JButton getHeader(){
  29. return header;
  30. }
  31. }
  1. public static class MyComboBoxUI extends BasicComboBoxUI {
  2. final JButton button= new JButton(EXPAND_ARROW);
  3. protected void installDefaults() {
  4. super.installDefaults();
  5. }
  6. @Override
  7. protected JButton createArrowButton() {
  8. button.setContentAreaFilled(false);
  9. button.setBorder(null);
  10. return button;
  11. }
  12. @Override
  13. public void configureArrowButton() {
  14. super.configureArrowButton();
  15. }
  16. public JButton getArrowButton(){
  17. return button;
  18. }
  19. }
  1. public class ComboBox extends JComboBox{
  2. public boolean isExpanded = false;
  3. private MyComboBoxRenderer renderer;
  4. public ComboBox(){
  5. super();
  6. this.setUI(new MyComboBoxUI());
  7. }
  8. public ComboBox(String[] list){
  9. super(list);
  10. renderer = new MyComboBoxRenderer(&quot;Lists Of Vocab&quot;);
  11. this.setUI(new MyComboBoxUI());
  12. this.setFont(Frame.I_FONT);
  13. this.setForeground(Frame.FONT_COLOR);
  14. this.setBackground(Frame.LIGHT_COLOR);
  15. this.setRenderer(renderer);
  16. MyComboBoxUI ui = (MyComboBoxUI) this.getUI();
  17. JButton arrowButton = ui.getArrowButton();
  18. arrowButton.addActionListener((ActionEvent e)-&gt;{
  19. isExpanded = !isExpanded;
  20. if(isExpanded == true){
  21. arrowButton.setIcon(COLLAPSE_ARROW);
  22. }
  23. else{
  24. arrowButton.setIcon(EXPAND_ARROW);
  25. }
  26. });
  27. }
  28. public MyComboBoxRenderer getRenderer(){
  29. return renderer;
  30. }

I add this combobox to the sideBar

  1. private void addCheckBoxToSideBar(){
  2. ComboBox lists = new ComboBox(listNames);
  3. lists.setAlignmentX(Component.LEFT_ALIGNMENT); // have to have
  4. lists.addItemListener(new ItemListener(){
  5. @Override
  6. public void itemStateChanged(ItemEvent e) {
  7. if (e.getStateChange() == ItemEvent.SELECTED) {
  8. CardLayout cl = (CardLayout)(mainPane.getLayout());
  9. cl.show(mainPane, (String)e.getItem());[enter image description here][1]
  10. }
  11. }
  12. });
  13. sideBar.add(lists);
  14. }

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 中,尝试覆盖这个方法:

  1. @Override
  2. public void paintCurrentValueBackground(Graphics g, Rectangle bounds, boolean hasFocus) {
  3. Color t = g.getColor();
  4. g.setColor(your_color);
  5. g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
  6. g.setColor(t);
  7. }
英文:

In your MyComboBoxUI, try overriding this method :

  1. @Override
  2. public void paintCurrentValueBackground(Graphics g,Rectangle bounds,boolean hasFocus)
  3. {
  4. Color t = g.getColor();
  5. g.setColor(your_color);
  6. g.fillRect(bounds.x,bounds.y,bounds.width,bounds.height);
  7. g.setColor(t);
  8. }

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:

确定