JComboBox.isTableCellEditor
विधि सूची के माध्यम से तीर आंदोलन के लिए काम करता है, लेकिन KeySelectionManager द्वारा समर्थित टाइप-आगे के लिए काम नहीं करता है। यानी आप अभी भी उपयोगकर्ता के प्रत्येक गैर-नेविगेशन कुंजी के लिए एक्शनवेन्ट प्राप्त करते हैं, क्योंकि JComboBox उन पात्रों को खोजने के लिए व्याख्या करता है, हालांकि मॉडल को उपयोगकर्ता के इच्छित चयन में स्थानांतरित करने के लिए (या करीब जाना)।
इस समाधान यह माउस क्लिक के लिए कार्रवाई आदेश, जो मेरे लिए एक ठीक समझौता था बदल जाता है कि क्योंकि जीयूआई के प्रवाह उपयोगकर्ता बलों फोकस कॉम्बो बॉक्स से दूर बदलने के लिए में एक दोष है
मैंने एक विशेष कीलिस्टर बनाने का अंत किया, जो कॉम्बो बॉक्स के डिफ़ॉल्ट एक्शन कमांड को comboBoxChanged
से comboBoxMovement
पर बदलने पर निर्भर करता है। यहाँ कोड की पंक्ति मैं अपने कॉम्बो बॉक्स के बाद की जरूरत है सभी आरंभ नहीं हो जाता है:
setExplicitSelectionManager(myComboBox);
... और यहां विधि और उसके निहित वर्ग कि सभी काम कर रहा है:
private void setExplicitSelectionManager(JComboBox comboBox) {
class ExplicitSelectionManager implements KeyListener, FocusListener {
private JComboBox src;
private KeyListener superKeyListener;
ExplicitSelectionManager(JComboBox src) {
this.src = src;
// we like what the default key listener does, but not the action command
// it uses for ActionEvents it fires for plain text type-ahead characters
this.superKeyListener = src.getKeyListeners()[0]; // we only have one
src.removeKeyListener(superKeyListener); // will be replace right away, below
}
@Override
public void keyTyped(KeyEvent e) {
// basic combo box has no code in keyTyped
}
@Override
public void keyPressed(KeyEvent e) {
// in the default JComboBox implementation, the KeySelectionManager is
// called from keyPressed. I'm fine with the implementation of
// the default, but I don't want it firing ActionEvents that will cause
// model updates
src.setActionCommand("comboBoxMovement");
this.superKeyListener.keyPressed(e);
src.setActionCommand("comboBoxChanged");
if (e.getKeyCode() == 10) {
src.setSelectedIndex(src.getSelectedIndex());
}
}
@Override
public void keyReleased(KeyEvent e) {
// basic combo box has no code in keyReleased
}
@Override
public void focusGained(FocusEvent e) {
}
@Override
// this will also give us the event we want, if the user decides to Tab out of
// the combo box, instead of hitting Enter
public void focusLost(FocusEvent e) {
src.setSelectedIndex(src.getSelectedIndex());
}
}
ExplicitSelectionManager newSelectionManager = new ExplicitSelectionManager(comboBox);
comboBox.addKeyListener(newSelectionManager);
comboBox.addFocusListener(newSelectionManager);
}
... और यहाँ की गई कार्रवाई विधि है
private void comboBoxActionPerformed(java.awt.event.ActionEvent evt) {
JComboBox source = (JComboBox) evt.getSource();
// "comboBoxChanged" is the default,
// so any normal JComboBox can also use this action listener
if (evt.getActionCommand().equals("comboBoxChanged")) {
updateModel(source.getName(), (String) source.getSelectedItem());
}
}
हाँ, यह वही है जो मैं प्राप्त करना चाहता था। आपका बहुत बहुत धन्यवाद। – toomyem
ऐसा लगता है कि यह PgUp/PgDn के लिए काम नहीं करता है। –