我想改變組合框的開箱即用的行爲,凍結它後點擊鼠標右鍵(檢測哪個按鈕被點擊是容易的,所以這不是問題的關鍵),並選擇開放的JPopupMenu istead該條目。開放的JPopupMenu從打開的JComboBox
那麼 - 如何禁用選擇條目並使用自定義行爲呢?
我試圖通過添加鼠標監聽到所有的ComboBox組件開始,但沒有成功 - 什麼都沒有改變
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class MainClass {
public static void main(final String args[]) {
final String labels[] = { "A", "B", "C", "D", "E" };
JFrame frame = new JFrame("Selecting JComboBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox comboBox = new JComboBox(labels);
frame.add(comboBox, BorderLayout.SOUTH);
frame.setSize(400, 200);
frame.setVisible(true);
for (Component c : comboBox.getComponents()) {
c.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("cli");
super.mouseClicked(e);
}
public void mousePressed(MouseEvent e) {
System.out.println("pre");
super.mousePressed(e);
}
});
}
}
}
你想實現當在組合框中選擇一個值究竟是什麼? –