下面列出了javax.swing.JComponent#addKeyListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void installUI(JComponent c) {
c.addFocusListener(repaintFocusListener);
c.addMouseListener(mouseListener);
c.addMouseMotionListener(mouseListener);
c.addKeyListener(keyListener);
JColorWell well = (JColorWell) c;
ColorChangeListener ccl = new ColorChangeListener(c);
listenerMap.put(well, ccl);
well.getColorSelectionModel().addChangeListener(ccl);
ShowColorPaletteActionListener colorPaletteActionListener = new ShowColorPaletteActionListener();
c.putClientProperty(DOUBLE_CLICK_ACTION_PROPERTY,
colorPickerActionListener);
c.putClientProperty(SPACE_KEY_ACTION_PROPERTY,
colorPickerActionListener);
c.putClientProperty(SINGLE_CLICK_ACTION_PROPERTY,
colorPaletteActionListener);
c.putClientProperty(DOWN_KEY_ACTION_PROPERTY,
colorPaletteActionListener);
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
c.addPropertyChangeListener(JPalette.PROPERTY_COLORS,
propertyLayoutListener);
c.addPropertyChangeListener(PaletteUI.PROPERTY_HIGHLIGHT,
propertyRepaintListener);
c.setLayout(new PaletteLayoutManager());
c.setRequestFocusEnabled(true);
c.addMouseListener(mouseListener);
c.addFocusListener(focusListener);
c.addKeyListener(keyListener);
c.setFocusable(true);
Fields fields = getFields((JPalette) c, true);
fields.install();
c.setBorder(new CompoundBorder(new LineBorder(new Color(0xB0B0B0)),
new FocusedBorder(getDefaultBorder())));
relayoutCells((JPalette) c);
}
/**
* Creates a new ComboBoxCellEditor.
* @param comboBox the comboBox that should be used as the cell editor.
*/
public ComboBoxCellEditor(final JComboBox comboBox) {
this.comboBox = comboBox;
handler = new Handler();
// Don't do this:
// this.comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
// it probably breaks various things
// hitting enter in the combo box should stop cellediting
JComponent editorComponent = (JComponent) comboBox.getEditor().getEditorComponent();
editorComponent.addKeyListener(handler);
// remove the editor's border - the cell itself already has one
editorComponent.setBorder(null);
// editor component might change (e.g. a look&feel change)
// the new editor component needs to be modified then (keyListener, border)
comboBox.addPropertyChangeListener(handler);
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
c.addKeyListener(keyListener);
c.addMouseListener(mouseListener);
layout.addPropertyChangeListener(layoutListener);
}
private static void addFocusArrowListener(JComponent jc) {
/**
* Check to see if someone already added this kind of listener:
*/
KeyListener[] listeners = jc.getKeyListeners();
for (int a = 0; a < listeners.length; a++) {
if (listeners[a] instanceof FocusArrowListener)
return;
}
// Add our own:
jc.addKeyListener(new FocusArrowListener());
}
@Override
public void installUI(JComponent slider) {
slider.addMouseListener(this);
slider.addMouseMotionListener(this);
slider.addFocusListener(focusListener);
slider.addKeyListener(keyListener);
slider.addComponentListener(compListener);
slider.addPropertyChangeListener(propertyListener);
slider.addPropertyChangeListener(THUMB_SHAPE_PROPERTY,
thumbShapeListener);
calculateGeometry();
}
@Override
public void installUI(JComponent slider) {
slider.addMouseListener(this);
slider.addMouseMotionListener(this);
slider.addFocusListener(focusListener);
slider.addKeyListener(keyListener);
slider.addComponentListener(compListener);
slider.addPropertyChangeListener(propertyListener);
slider.addPropertyChangeListener(THUMB_SHAPE_PROPERTY, thumbShapeListener);
calculateGeometry();
}
@Override
public void setProperty( String sProperty, Object oValue )
{
// InteractiveRenderer(iv) is only for Swing
if ( sProperty.equals( IDeviceRenderer.UPDATE_NOTIFIER ) && iv != null )
{
_iun = (IUpdateNotifier) oValue;
iv.setUpdateNotifier( _iun );
_lhmAllTriggers.clear( );
Object obj = _iun.peerInstance( );
if ( obj instanceof JComponent )
{
JComponent jc = (JComponent) obj;
if ( _eh != null )
{
// We can't promise to remove all the old swtEventHandler
// due to SWT limitation here, so be sure to just attach the
// update_notifier only to one renderer.
jc.removeMouseListener( _eh );
jc.removeMouseMotionListener( _eh );
jc.removeKeyListener( _eh );
jc.removeFocusListener( _eh );
}
_eh = new SwingEventHandler( iv,
_lhmAllTriggers,
_iun,
getULocale( ) );
jc.addMouseListener( _eh );
jc.addMouseMotionListener( _eh );
jc.addKeyListener( _eh );
jc.addFocusListener( _eh );
}
}
super.setProperty( sProperty, oValue );
}
public void propertyChange(PropertyChangeEvent e) {
if (e.getPropertyName().equals("editor")) {
ComboBoxEditor editor = comboBox.getEditor();
if (editor!=null && editor.getEditorComponent()!=null) {
JComponent editorComponent = (JComponent) comboBox.getEditor().getEditorComponent();
editorComponent.addKeyListener(this);
editorComponent.setBorder(null);
}
}
}
private void addField(String label, JComponent comp, String variable) {
if (!(comp instanceof JCheckBox)) {
this.add(new JLabel(label), new GridBagConstraints(0, row, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
}
if (comp instanceof JTextArea) {
this.add(new JScrollPane(comp), new GridBagConstraints(1, row, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 100, 50));
}
else if (comp instanceof JCheckBox) {
this.add(comp, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
}
else if (comp instanceof CheckBoxList) {
this.add(comp, new GridBagConstraints(1, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 50));
}
else {
this.add(comp, new GridBagConstraints(1, row, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
}
valueMap.put(variable, comp);
row++;
comp.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyChar() == KeyEvent.VK_ENTER){
if(listener != null){
listener.enterPressed();
}
}
}
});
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
c.setFocusable(true);
c.addKeyListener(keyListener);
}
protected void addBag(JComponent comp, GridBagLayout gridbag,
GridBagConstraints c) {
gridbag.setConstraints(comp, c);
add(comp);
comp.addKeyListener(this);
}