JComboBox를 확장하는 클래스 (UIPromptComboBox
)를 쓰고 있습니다. 콤보 박스는 편집 가능하며 클래스의 한 응용 프로그램에 대해서는 ActionListener
컨트롤을 사용하여 구현됩니다.편집 가능한 JComboBox가 포커스를 잃을 때 ActionListener를 실행합니다.
현재 콤보 상자를 편집하면 ActionListener
이 좋습니다. 그러나이 ActionListener
또한 콤보 상자를 선택 취소 할 때 해고되며 두 이벤트를 구별 할 수 없으며 콤보 상자가 선택 취소 된 경우에도 실행하려고하지 않습니다. 여러 객체를 일으키는 인해 초점을 잃는 JComboBox에
public class UIPromptComboBox extends JComboBox {
public UIPromptComboBox(UIPrompt[] items) {
super(items);
this.setEditable(true);
}
public UIPromptComboBox() {
this.setEditable(true);
this.setEnabled(false);
}
/**
* returns either the selected UI prompt or a new prompt using the example
* text
*
* @param exampleText only used if new prompt is created
* @return UI prompt selected
*/
public UIPrompt getUIPrompt(String exampleText) {
UIPrompt uIPrompt = null;
Object returnedItem = this.getSelectedItem();
if (returnedItem instanceof UIPrompt) {
uIPrompt = (UIPrompt) returnedItem;
} else if (returnedItem instanceof String) {
uIPrompt = new UIPrompt((String) returnedItem, exampleText);
}
return uIPrompt;
}
public void addNewUIPrompt(UIPrompt newPrompt) {
ActionListener[] actionListerners = this.getActionListeners();
this.removeActionListener(this);
this.addItem(newPrompt);
this.setSelectedItem(newPrompt);
for (ActionListener al : actionListerners) {
this.addActionListener(al);
}
}
/**
* Used for displaying a report value sentence
* i.e. a string that is not associated with UI Prompts
* @param newText report value sentence
*/
public void setText(String newText) {
this.removeAllItems();
this.addItem(newText);
this.setSelectedItem(newText);
}
/**
* For when the UI prompts can be added on construction
*
* @param currentUIs list of UI promts
*/
public void addItems(UIPrompt[] currentUIs) {
this.removeAllItems();
DefaultComboBoxModel boxModel = new DefaultComboBoxModel(currentUIs);
this.setModel(boxModel);
}
}
다중 소성 연장
구현 클래스
private void addUIField() {
// Initialise and place combobox
this.myGuiTextField = new UIPromptComboBox();
myGuiTextField.setSize(COMBO_WIDTH, defaultHeight);
GuiUtils.positionControl(myPanel, myGuiTextField, myTop, PROMPT_X_LOC);
//Add action listener
myGuiTextField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("comboBoxEdited")) {
newUIcreated((UIPromptComboBox) evt.getSource());
}
}
private void newUIcreated(UIPromptComboBox alteredGuiTextField) {
try {
UIPrompt uip = alteredGuiTextField.getUIPrompt(((PowerPointTextItem) myPPTRef).getValue());
if (!simInfo.isInPrompts(uip)) {
simInfo.addUIPrompt(uip);
alteredGuiTextField.addNewUIPrompt(uip);
}
} catch (MissingPowerpointItem ex) {
Exceptions.printStackTrace(ex);
}
}
});
}
클래스 만들어 목록에 추가한다. 나는 ActionListener
을 잘못 구현했다고 생각합니다. 귀하의 도움에 감사드립니다
콤보 상자를 선택 취소하면 이벤트가 실행됩니다. 지금 정확히 무슨 일이 일어나고 무슨 일이 일어나고 있는지 이해하지 못했습니다. – XtremeBaumer
@XtremeBaumer, 제 질문을 읽어 주셔서 감사합니다. 내가 선택 취소하는 대신 콤보 상자를 편집 한 후 사용자가 Enter 키를 눌렀을 때 ActionListener가 시작되는 것이 좋습니다. 현재 문제는 입력이 눌러 졌을 때와 다른 코드가 반복적으로 반복되는 다른 콤보 박스가 선택 될 때 발생한다는 것입니다. – emmistar