2017-04-11 101 views
1

我正在編寫一個簡單的swing程序,我正在創建一個JFrame並向其中添加一個自定義JComboBox。JComboBox的工具提示位置箭頭按鈕

public class CustomJComboBox<T> extends JComboBox<T> { 

public static void main(String[] args) { 
    new CustomJComboBox<>().initUI(); 
} 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

@Override 
public Point getToolTipLocation(MouseEvent event) { 
    System.out.println("getToolTipLocation called"); 
    return super.getToolTipLocation(event); 
} 

private void initUI() { 

    JComboBox<String> box = new CustomJComboBox<>(); 

    box.addItem("Item 1"); 
    box.addItem("Item 2"); 

    box.setToolTipText("TooTip"); 

    JFrame frame = new JFrame(); 

    frame.setBounds(0, 0, 300, 300); 

    frame.add(box); 

    frame.setVisible(true); 

} 

} 

當我保持鼠標移到組合框被覆蓋getToolTipLocation()方法被調用,但是當我保持鼠標下拉箭頭按鈕,該方法不叫作爲其單獨的JButton。

有沒有辦法控制arrowbutton的工具提示位置呢?

回答

1

嘗試增加MouseMotionListener其中鼠標事件傳送到父JComboBox,到ArrowButton

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
//import javax.swing.plaf.metal.*; 

public class CustomJComboBoxTest { 
    public JComponent makeUI() { 
    JComboBox<String> box = new JComboBox<String>() { 
     private transient MouseAdapter handler; 
     @Override public Point getToolTipLocation(MouseEvent e) { 
     System.out.println("getToolTipLocation called"); 
     return super.getToolTipLocation(e); 
     } 
     @Override public void updateUI() { 
     for (Component c : getComponents()) { 
      if (c instanceof JButton) { 
      ((JButton) c).removeMouseListener(handler); 
      ((JButton) c).removeMouseMotionListener(handler); 
      } 
     } 
     super.updateUI(); 
     handler = new ComboBoxMouseEventHelper(); 
     for (Component c : getComponents()) { 
      if (c instanceof JButton) { 
      ((JButton) c).addMouseListener(handler); 
      ((JButton) c).addMouseMotionListener(handler); 
      } 
     } 
//   //TEST: 
//   setUI(new BasicComboBoxUI() { 
//   @Override protected JButton createArrowButton() { 
//    //JButton button = super.createArrowButton(); 
//    boolean iconOnly = true; 
//    JButton button = new MetalComboBoxButton(comboBox, new MetalComboBoxIcon(), 
//    iconOnly, currentValuePane, listBox) { 
//    @Override public Point getToolTipLocation(MouseEvent e) { 
//     System.out.println("ArrowButton: getToolTipLocation called"); 
//     return super.getToolTipLocation(e); 
//    } 
//    }; 
//    button.setMargin(new Insets(0, 1, 1, 3)); 
//    button.setName("ComboBox.arrowButton"); 
//    return button; 
//   } 
//   }); 
     } 
    }; 
    box.addItem("Item 1"); 
    box.addItem("Item 2"); 
    box.setToolTipText("TooTip"); 

    JPanel p = new JPanel(new BorderLayout()); 
    p.setBorder(BorderFactory.createEmptyBorder(60, 20, 60, 20)); 
    p.add(box); 
    return p; 
    } 
    public static void main(String[] args) { 
    EventQueue.invokeLater(() -> { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     f.getContentPane().add(new CustomJComboBoxTest().makeUI()); 
     f.setSize(320, 240); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    }); 
    } 
} 

class ComboBoxMouseEventHelper extends MouseAdapter { 
    private void dispatchEvent(MouseEvent e) { 
    Component s = e.getComponent(); 
    Container c = SwingUtilities.getAncestorOfClass(JComboBox.class, s); 
    if (c instanceof JComboBox) { 
     ((JComboBox) c).dispatchEvent(SwingUtilities.convertMouseEvent(s, e, c)); 
    } 
    } 
// @Override public void mouseClicked(MouseEvent e) { 
// dispatchEvent(e); 
// } 
    @Override public void mouseEntered(MouseEvent e) { 
    dispatchEvent(e); 
    } 
    @Override public void mouseExited(MouseEvent e) { 
    dispatchEvent(e); 
    } 
// @Override public void mousePressed(MouseEvent e) { 
// dispatchEvent(e); 
// } 
// @Override public void mouseReleased(MouseEvent e) { 
// dispatchEvent(e); 
// } 
    @Override public void mouseMoved(MouseEvent e) { 
    dispatchEvent(e); 
    } 
// @Override public void mouseDragged(MouseEvent e) { 
// dispatchEvent(e); 
// } 
}