Javafx 如何从组合框中删除选定元素
当我从组合框中选择元素时,它必须从元素列表中删除。所选元素不能显示,必须立即删除Javafx 如何从组合框中删除选定元素,javafx,combobox,Javafx,Combobox,当我从组合框中选择元素时,它必须从元素列表中删除。所选元素不能显示,必须立即删除 import javafx.application.Application; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.ComboBo
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class Test extends Application {
// Launch the application
public void start(Stage stage)
{
// Set title for the stage
stage.setTitle("creating combo box ");
// Create a tile pane
TilePane r = new TilePane();
// Create a label
Label description_label =
new Label("This is a combo box example ");
// Weekdays
String week_days[] =
{ "Monday", "Tuesday", "Wednesday",
"Thrusday", "Friday" };
// Create a combo box
ComboBox combo_box =
new ComboBox(FXCollections
.observableArrayList(week_days));
// Label to display the selected menuitem
Label selected = new Label("default item selected");
// Create action event
EventHandler<ActionEvent> event =
new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
selected.setText(combo_box.getValue() + " selected");
}
};
// Set on action
combo_box.setOnAction(event);
// Create a tile pane
TilePane tile_pane = new TilePane(combo_box, selected);
// Create a scene
Scene scene = new Scene(tile_pane, 200, 200);
// Set the scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
// Launch the application
launch(args);
}
}
当我从组合框中选择元素时,它必须从元素列表中删除。所选元素不能显示,必须立即删除
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class Test extends Application {
// Launch the application
public void start(Stage stage)
{
// Set title for the stage
stage.setTitle("creating combo box ");
// Create a tile pane
TilePane r = new TilePane();
// Create a label
Label description_label =
new Label("This is a combo box example ");
// Weekdays
String week_days[] =
{ "Monday", "Tuesday", "Wednesday",
"Thrusday", "Friday" };
// Create a combo box
ComboBox combo_box =
new ComboBox(FXCollections
.observableArrayList(week_days));
// Label to display the selected menuitem
Label selected = new Label("default item selected");
// Create action event
EventHandler<ActionEvent> event =
new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
selected.setText(combo_box.getValue() + " selected");
}
};
// Set on action
combo_box.setOnAction(event);
// Create a tile pane
TilePane tile_pane = new TilePane(combo_box, selected);
// Create a scene
Scene scene = new Scene(tile_pane, 200, 200);
// Set the scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
// Launch the application
launch(args);
}
}
导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.control.ComboBox;
导入javafx.scene.control.Label;
导入javafx.scene.layout.TilePane;
导入javafx.stage.stage;
公共类测试扩展了应用程序{
//启动应用程序
公众假期开始(阶段)
{
//为舞台设置标题
stage.setTitle(“创建组合框”);
//创建平铺窗格
TilePane r=新的TilePane();
//创建一个标签
标签说明\u标签=
新标签(“这是一个组合框示例”);
//平日
字符串周\天[]=
{“星期一”、“星期二”、“星期三”,
“星期四”、“星期五”};
//创建一个组合框
组合框组合框=
新组合框(FXCollections)
.可观察的工作清单(周/日);
//标签以显示所选菜单项
选择的标签=新标签(“选择的默认项目”);
//创建操作事件
事件处理程序事件=
新的EventHandler(){
公共无效句柄(ActionEvent e)
{
selected.setText(组合框.getValue()+“selected”);
}
};
//开始行动
组合框设置动作(事件);
//创建平铺窗格
TilePane平铺窗格=新的TilePane(组合框,选中);
//创造一个场景
场景=新场景(平铺窗格,200200);
//布景
舞台场景;
stage.show();
}
公共静态void main(字符串参数[])
{
//启动应用程序
发射(args);
}
}
输出:
由于在
组合框
正确处理自身更新之前调用了操作
,因此需要使用平台延迟删除。稍后运行
:
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
String value = combo_box.getValue();
if (value != null) {
selected.setText(value + " selected");
Platform.runLater(() -> {
combo_box.setValue(null);
combo_box.getItems().remove(value);
});
}
}
};
EventHandler event=neweventhandler(){
公共无效句柄(ActionEvent e){
字符串值=组合框.getValue();
if(值!=null){
selected.setText(值+“selected”);
Platform.runLater(()->{
组合框设置值(空);
combo_box.getItems().remove(值);
});
}
}
};
改进的格式问题到底是什么?Afaics,您的操作处理程序只会设置标签文本,在何处/如何删除该项?注意:调用runlater的时间是明确未指定的(将来的某个未指定的时间)-它可能会按预期的方式运行。。或者不是;)@kleopatra是的,但在这种情况下,可以安全地假设可能导致故障的事情(修改选择模型&value
properties)在当时已经完成。在这些修改完成之后,知道未来的未知时间就足够了……好奇:为什么——有关于这个的文件吗?