そのため、押された後に新しいユーザー(別のクラスから取得された名前)をリストに追加するボタンがあります。ビルドして実行した後、手動でボタンをクリックしてボタンを押すと、正常に動作します。問題は、別のメソッドを介して ActionEvent メソッドを呼び出すと、System.out.println テキストが出力されますが、新しいエントリがリストに追加されないことです。
助言がありますか?
ボタンを押すと呼び出されるコードは次のとおりです(「<--」でマークされた行は、手動でボタンを押した場合にのみ機能するように見える行です):
public void actionPerformed(ActionEvent e) {
listModel.insertElementAt(name, index); // <--
System.out.println("finished running action");
}
私のコードのより完全なバージョンは次のとおりです。
public void actionPerformed(ActionEvent e) {
System.out.println("ran action");
addAuthor();
System.out.println("authornamefinalfunc name: " + name);
//Reset the text field.
employeeName.requestFocusInWindow();
//employeeName.setText("");
//Select the new item and make it visible.
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
System.out.println("ran action final");
}
private void addAuthor()
{
String name = Global.s;
int index = list.getSelectedIndex(); //get selected index
if (index == -1) { //no selection, so insert at beginning
index = 0;
} else { //add after the selected item
index++;
}
listModel.insertElementAt(name, index);
}