连接两个选择框或组合框
Connecting two Choice Boxes or Combo Boxes
有没有办法连接两个选择框或组合框。哪个并不重要。
我想更改第二个框(整数)中的项目,具体取决于在第一个框(字符串)中选择的内容。
例如:
方框一:
纸杯蛋糕
饼干
框二:
如果是纸杯蛋糕,那么数字 1,2,3, .... ,28,29,30.
如果是 cookie,则数字 1,2,3, .... ,27,28.
否则是空盒子。
数字可以通过数组放入。我遇到的问题是如何使用事件处理程序更改第二个框中的内容。
谢谢。
根据您的要求,可以采用不同的方法。一个可以是:
@Override
public void start( Stage stage )
{
final Map<String, ObservableList<Integer>> map = new HashMap<>();
map.put( "cupcakes", FXCollections.observableArrayList( 1,2,3,4,5,6));
map.put( "cookies", FXCollections.observableArrayList(11,12,13,14,15,16));
final ComboBox<String> comboOne = new ComboBox<>();
comboOne.getItems().addAll(
"cupcakes",
"cookies",
"empty box"
);
final ComboBox<Integer> comboTwo = new ComboBox<>();
comboOne.getSelectionModel().selectedItemProperty().addListener( new ChangeListener<String>()
{
@Override
public void changed( ObservableValue<? extends String> observable, String oldValue, String newValue )
{
comboTwo.setItems(
map.containsKey( newValue ) ? map.get( newValue ) : FXCollections.emptyObservableList()
);
}
} );
VBox grid = new VBox( 20 );
grid.getChildren().addAll( comboOne, comboTwo );
Scene scene = new Scene( grid, 450, 250 );
stage.setScene( scene );
stage.show();
}
有没有办法连接两个选择框或组合框。哪个并不重要。 我想更改第二个框(整数)中的项目,具体取决于在第一个框(字符串)中选择的内容。
例如:
方框一:
纸杯蛋糕
饼干
框二:
如果是纸杯蛋糕,那么数字 1,2,3, .... ,28,29,30.
如果是 cookie,则数字 1,2,3, .... ,27,28.
否则是空盒子。
数字可以通过数组放入。我遇到的问题是如何使用事件处理程序更改第二个框中的内容。
谢谢。
根据您的要求,可以采用不同的方法。一个可以是:
@Override
public void start( Stage stage )
{
final Map<String, ObservableList<Integer>> map = new HashMap<>();
map.put( "cupcakes", FXCollections.observableArrayList( 1,2,3,4,5,6));
map.put( "cookies", FXCollections.observableArrayList(11,12,13,14,15,16));
final ComboBox<String> comboOne = new ComboBox<>();
comboOne.getItems().addAll(
"cupcakes",
"cookies",
"empty box"
);
final ComboBox<Integer> comboTwo = new ComboBox<>();
comboOne.getSelectionModel().selectedItemProperty().addListener( new ChangeListener<String>()
{
@Override
public void changed( ObservableValue<? extends String> observable, String oldValue, String newValue )
{
comboTwo.setItems(
map.containsKey( newValue ) ? map.get( newValue ) : FXCollections.emptyObservableList()
);
}
} );
VBox grid = new VBox( 20 );
grid.getChildren().addAll( comboOne, comboTwo );
Scene scene = new Scene( grid, 450, 250 );
stage.setScene( scene );
stage.show();
}