在JComboBox箭头JButton上附加动作事件

时间:2022-06-22 12:04:50

I try to attach action Events on the JCombobox arrow JButton.

我尝试在JCombobox箭头JButton上附加动作事件。

So I make a custom ComboBoxUI:

所以我做了一个自定义的ComboBoxUI:

public class CustomBasicComboBoxUI extends BasicComboBoxUI {

    public static CustomBasicComboBoxUI createUI(JComponent c) {
        return new CustomBasicComboBoxUI ();
    }

    @Override
    protected JButton createArrowButton() {
        JButton button=super.createArrowButton();
        if(button!=null) {
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    // arrow button clicked
                }
            });
        }
        return button;
    }
}

The problem with this is that the look of combobox is different, seem to be an old look. Why? I only add a listener to the same arrow button...

这个问题是组合框的外观不同,看起来很古老。为什么?我只在同一个箭头按钮上添加一个监听器......

Thank.

谢谢。

1 个解决方案

#1


4  

Perhaps the problem is due to your expecting a JComboBox isn't a BasicComboBoxUI but one of another look and feel, perhaps a MetalComboBoxUI.

也许问题是由于您期望JComboBox不是BasicComboBoxUI而是另一种外观和感觉,也许是MetalComboBoxUI。

Rather than create a new CustomBasicComboBoxUI object, could you extract the JButton component from an existing JComboBox object? i.e.,

您可以从现有的JComboBox对象中提取JButton组件,而不是创建新的CustomBasicComboBoxUI对象吗?即,

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ComboBoxArrowListener {
   private static void createAndShowUI() {
      String[] data = {"One", "Two", "Three"};
      JComboBox combo = new JComboBox(data);
      JPanel panel = new JPanel();
      panel.add(combo);

      JButton arrowBtn = getButtonSubComponent(combo);
      if (arrowBtn != null) {
         arrowBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               System.out.println("arrow button pressed");
            }
         });
      }

      JFrame frame = new JFrame("ComboBoxArrowListener");
      frame.getContentPane().add(panel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   private static JButton getButtonSubComponent(Container container) {
      if (container instanceof JButton) {
         return (JButton) container;
      } else {
         Component[] components = container.getComponents();
         for (Component component : components) {
            if (component instanceof Container) {
               return getButtonSubComponent((Container)component);
            }
         }
      }
      return null;
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

#1


4  

Perhaps the problem is due to your expecting a JComboBox isn't a BasicComboBoxUI but one of another look and feel, perhaps a MetalComboBoxUI.

也许问题是由于您期望JComboBox不是BasicComboBoxUI而是另一种外观和感觉,也许是MetalComboBoxUI。

Rather than create a new CustomBasicComboBoxUI object, could you extract the JButton component from an existing JComboBox object? i.e.,

您可以从现有的JComboBox对象中提取JButton组件,而不是创建新的CustomBasicComboBoxUI对象吗?即,

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ComboBoxArrowListener {
   private static void createAndShowUI() {
      String[] data = {"One", "Two", "Three"};
      JComboBox combo = new JComboBox(data);
      JPanel panel = new JPanel();
      panel.add(combo);

      JButton arrowBtn = getButtonSubComponent(combo);
      if (arrowBtn != null) {
         arrowBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               System.out.println("arrow button pressed");
            }
         });
      }

      JFrame frame = new JFrame("ComboBoxArrowListener");
      frame.getContentPane().add(panel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   private static JButton getButtonSubComponent(Container container) {
      if (container instanceof JButton) {
         return (JButton) container;
      } else {
         Component[] components = container.getComponents();
         for (Component component : components) {
            if (component instanceof Container) {
               return getButtonSubComponent((Container)component);
            }
         }
      }
      return null;
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}