import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.basic.BasicComboBoxUI;
public class MyJComboBox extends JFrame implements ActionListener {
private JComboBox c = new JComboBox();
private JButton b = new JButton("Change");
private MyUI m = new MyUI();
public MyJComboBox() {
super("Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setAlwaysOnTop(true);
this.setLocationByPlatform(true);
c.setEditable(true);
c.setUI((ComboBoxUI) m);
m.deactivatePopupMenu();
b.addActionListener(this);
this.add(c, BorderLayout.NORTH);
this.add(b, BorderLayout.SOUTH);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
new MyJComboBox();
}
public void actionPerformed(ActionEvent e) {
if (m.isActive())
m.deactivatePopupMenu();
else
m.activatePopupMenu();
}
class MyUI extends BasicComboBoxUI {
private boolean isArrowButtonActive = false;
public MyUI() {
super();
}
public void activatePopupMenu() {
arrowButton.setEnabled(true);
arrowButton.addMouseListener(popup.getMouseListener());
isArrowButtonActive = true;
}
public void deactivatePopupMenu() {
arrowButton.removeMouseListener(popup.getMouseListener());
arrowButton.setEnabled(false);
isArrowButtonActive = false;
}
public boolean isActive() {
return isArrowButtonActive;
}
}
}