我试图在初始化组合框的默认值时触发一个动作,但我只能找到在使用ComboBox的OnAction中设置的方法print()更改值时触发动作的方法。
这是我的FXML文件的主持人:
package tre;
import java.net.URL;
import java.util.ResourceBundle;
import javax.swing.event.ChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
public class presenter implements Initializable {
public @FXML ComboBox<String> combo;
private final ObservableList<String> statistic = FXCollections.observableArrayList("Logged In", "Anonymous",
"Logged Off");
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
combo.setItems(statistic);
combo.setValue("Anonymous");
//TODO When Anonymous is displayed as default value in the combo box, it will print it.
}
public @FXML void print(){
String sel = combo.getSelectionModel().getSelectedItem();
System.out.println(sel);
}
}
有解决方案吗?感谢