JavaFX。动态手风琴
我有一个JavaFX项目,其中fxml和代码用于制作GUI 有一个GUI: 如您所见,有2个JavaFX。动态手风琴,java,javafx,Java,Javafx,我有一个JavaFX项目,其中fxml和代码用于制作GUI 有一个GUI: 如您所见,有2个JavaFX.TitledPane。它们是在fxml中创建的。在他们之后,我有一个JavaFX.Accordion。我从JavaFX.TextField获取管道数量并生成它们。有一些不同类型的JavaFX.TitledPane被放在手风琴中。你可以在截图上看到不同的类型 我有一个问题: 手风琴中的每个标题窗格都有第一个选择框。此选项框定义标题窗格的类型。因此,我们在#1屏幕截图上有默认标题窗格。当用户从
JavaFX.TitledPane
。它们是在fxml中创建的。在他们之后,我有一个JavaFX.Accordion
。我从JavaFX.TextField
获取管道数量并生成它们。有一些不同类型的JavaFX.TitledPane
被放在手风琴中。你可以在截图上看到不同的类型
我有一个问题:
手风琴中的每个标题窗格都有第一个选择框。此选项框定义标题窗格的类型。因此,我们在#1屏幕截图上有默认标题窗格。当用户从组合框中选择另一项时,我们需要重新创建标题窗格。此外,我们需要在标题窗格的文本框中保存数据,并在新标题窗格中恢复数据。请给我一些建议或是代码的一部分,它们能以正确的方式解决这个问题
我有这样的控制器类:
public class Controller {
@FXML
TextField textFieldNonConservatismCoef;
@FXML
TextField textFieldDiffusionCoef;
@FXML
TextField textFieldFlowSpeed;
@FXML
TextField textFieldRiverDepth;
@FXML
TextField textFieldRiverWidth;
@FXML
TextField textFieldSubstance;
@FXML
TextField textFieldProportion;
@FXML
TextField textFieldLAC;
@FXML
TextField textFieldConcentration;
@FXML
TextField textFieldNumberOfPipes;
@FXML
Accordion accordionPlant;
public void fillAccordion(int numberOfPipes){
accordionPlant.getPanes().add(createTitledPaneCoastalConcentratedPipe(0));
accordionPlant.getPanes().add(createTitledPaneCoastalSpreadPipe(1));
}
//Create the first type of a TitledPane
protected TitledPane createTitledPaneCoastalConcentratedPipe(int index){
TilePane tile = new TilePane(Orientation.HORIZONTAL, 5, 5);
Label typeLabel = new Label("Тип выпуска");
ChoiceBox<String> choiceBoxTypeOfPipe = createChoiceBoxTypeOfPipes();
choiceBoxTypeOfPipe.getSelectionModel().select(0);
choiceBoxTypeOfPipe.setPrefWidth(choiceBoxTypeOfPipe.USE_COMPUTED_SIZE);
choiceBoxTypeOfPipe.setPrefHeight(choiceBoxTypeOfPipe.USE_COMPUTED_SIZE);
VBox typeContainer = new VBox(typeLabel,choiceBoxTypeOfPipe);
Label bankLabel = new Label("Берег");
bankLabel.setId("textWithTooltip");
Tooltip.install(bankLabel, new Tooltip("Берег, с которого производится выпуск"));
ChoiceBox<String> choiceBoxCoast = createChoiceBoxCoast();
choiceBoxCoast.getSelectionModel().select(0);
VBox bankContainer = new VBox(bankLabel, choiceBoxCoast);
Label labelCoordinateX = new Label("Точка выпуска (км)");
Tooltip.install(labelCoordinateX, new Tooltip("Координата x точки выпуска относительно положения контрольного створа"));
labelCoordinateX.setId("textWithTooltip");
TextField textFieldCoordinate = new TextField();
VBox coordinateContainer = new VBox(labelCoordinateX, textFieldCoordinate);
tile.getChildren().addAll(typeContainer, bankContainer, coordinateContainer);
TitledPane titledPane = new TitledPane("Параметры выпуска " + (index+1), tile);
return titledPane;
}
//Create the second type of a TitledPane
protected TitledPane createTitledPaneCoastalSpreadPipe(int index){
TilePane tile = new TilePane(Orientation.HORIZONTAL, 5, 5);
Label typeLabel = new Label("Тип выпуска");
ChoiceBox<String> choiceBoxTypeOfPipe = createChoiceBoxTypeOfPipes();
choiceBoxTypeOfPipe.getSelectionModel().select(1);
VBox typeContainer = new VBox(typeLabel,choiceBoxTypeOfPipe);
Label bankLabel = new Label("Берег");
bankLabel.setId("textWithTooltip");
Tooltip.install(bankLabel, new Tooltip("Берег, с которого производится выпуск"));
ChoiceBox<String> choiceBoxCoast = createChoiceBoxCoast();
choiceBoxCoast.getSelectionModel().select(0);
VBox bankContainer = new VBox(bankLabel, choiceBoxCoast);
Label labelCoordinateX = new Label("Точка выпуска (км)");
labelCoordinateX.setId("textWithTooltip");
Tooltip.install(labelCoordinateX, new Tooltip("Координата x точки выпуска относительно положения контрольного створа"));
TextField textFieldCoordinate = new TextField();
VBox coordinateContainer = new VBox(labelCoordinateX, textFieldCoordinate);
Label labelLength = new Label("Распределенная часть (м)");
labelLength.setId("textWithTooltip");
Tooltip.install(labelLength, new Tooltip("Длина распределенной части выпуска (м)"));
TextField textFieldLength = new TextField();
VBox lengthContainer = new VBox(labelLength, textFieldLength);
tile.getChildren().addAll(typeContainer, bankContainer, coordinateContainer, lengthContainer);
TitledPane titledPane = new TitledPane("Параметры выпуска " + (index+1), tile);
return titledPane;
}
//creates choiceBox of pipes
protected ChoiceBox<String> createChoiceBoxTypeOfPipes(){
return new ChoiceBox(FXCollections.observableArrayList("Береговой сосредоточенный",
"Береговой распределенный", "Русловой сосредоточенный", "Русловой рассеивающий",
"Русловой рассеивающий с двумя ветками выпуска"));
}
现在还不清楚实际的问题是什么,或者为什么需要两种不同的方法 为什么不:
protected TitledPane createTitledPaneCoastalPipe(int index, boolean concentrate){
TilePane tile = new TilePane(Orientation.HORIZONTAL, 5, 5);
Label typeLabel = new Label("Тип выпуска");
ChoiceBox<String> choiceBoxTypeOfPipe = createChoiceBoxTypeOfPipes();
// remove this line:
// choiceBoxTypeOfPipe.getSelectionModel().select(1);
VBox typeContainer = new VBox(typeLabel,choiceBoxTypeOfPipe);
Label bankLabel = new Label("Берег");
bankLabel.setId("textWithTooltip");
Tooltip.install(bankLabel, new Tooltip("Берег, с которого производится выпуск"));
ChoiceBox<String> choiceBoxCoast = createChoiceBoxCoast();
choiceBoxCoast.getSelectionModel().select(0);
VBox bankContainer = new VBox(bankLabel, choiceBoxCoast);
Label labelCoordinateX = new Label("Точка выпуска (км)");
labelCoordinateX.setId("textWithTooltip");
Tooltip.install(labelCoordinateX, new Tooltip("Координата x точки выпуска относительно положения контрольного створа"));
TextField textFieldCoordinate = new TextField();
VBox coordinateContainer = new VBox(labelCoordinateX, textFieldCoordinate);
Label labelLength = new Label("Распределенная часть (м)");
labelLength.setId("textWithTooltip");
Tooltip.install(labelLength, new Tooltip("Длина распределенной части выпуска (м)"));
TextField textFieldLength = new TextField();
VBox lengthContainer = new VBox(labelLength, textFieldLength);
tile.getChildren().addAll(typeContainer, bankContainer, coordinateContainer);
choiceBoxTypeOfPipes.getSelectionModel().selectedIndexProperty().addListener(
(obs, oldIndex, newIndex) -> {
// may need different logic here: question is unclear
if (newIndex.intValue()==1) {
tile.getChildren().add(lengthContainer);
}
if (oldIndex.intValue()==1) {
tile.getChildren().remove(lengthContainer);
}
}
);
if (concentrate) {
choiceBoxTypeOfPipes.getSelectionModel().select(0);
} else {
choiceBoxTypeOfPipes.getSelectionModel().select(1);
}
TitledPane titledPane = new TitledPane("Параметры выпуска " + (index+1), tile);
return titledPane;
}
如果没有多余的代码,这不是你想要的吗?你问的不是很清楚。为什么不在创建时将侦听器添加到
选择框
,从而相应地修改其他控件?@James\u D,当然我们需要将侦听器添加到每个选择框。但我们需要了解以下内容:1)手风琴中标题窗格的索引2)如何重新创建标题窗格并恢复以前的数据为什么需要索引?除非我完全误解了这一点,否则您已经引用了正确的标题窗格
,方法与将侦听器添加到选项框
的方法相同。您还拥有对所有其他控件的引用,因此您可以从它们获取数据(如果需要),或者根据需要重新配置。现在还不清楚这里有什么问题。@James\u D您能提供重新创建当前标题窗格的解决方案吗?至少是素描。我会尽力解释问题所在。因为我觉得你说的事情很简单。但当我试图实现时,我忍受了fiasco@James_D非常感谢。对于我的问题,我需要另一种观点。你的解决方案对我的大脑来说是个好主意)哦,老兄,所有的巧妙都很简单!我只想从头开始创建TitlePanel
s。有更多3种类型的窗格,但我想我可以自己做。哦,如果我想将TextField
s的实例保存为类的属性。。。如果我们更改标题栏的类型,我们也应该删除它们。那么我们需要用什么呢<代码>链接列表
?或地图
?或者有没有一种简单的方法来销毁实例?因为,将来我将有一个方法Pipes[]createPipesFromGUI
,这样该方法将从字段中读取数据。
protected TitledPane createTitledPaneCoastalPipe(int index, boolean concentrate){
TilePane tile = new TilePane(Orientation.HORIZONTAL, 5, 5);
Label typeLabel = new Label("Тип выпуска");
ChoiceBox<String> choiceBoxTypeOfPipe = createChoiceBoxTypeOfPipes();
// remove this line:
// choiceBoxTypeOfPipe.getSelectionModel().select(1);
VBox typeContainer = new VBox(typeLabel,choiceBoxTypeOfPipe);
Label bankLabel = new Label("Берег");
bankLabel.setId("textWithTooltip");
Tooltip.install(bankLabel, new Tooltip("Берег, с которого производится выпуск"));
ChoiceBox<String> choiceBoxCoast = createChoiceBoxCoast();
choiceBoxCoast.getSelectionModel().select(0);
VBox bankContainer = new VBox(bankLabel, choiceBoxCoast);
Label labelCoordinateX = new Label("Точка выпуска (км)");
labelCoordinateX.setId("textWithTooltip");
Tooltip.install(labelCoordinateX, new Tooltip("Координата x точки выпуска относительно положения контрольного створа"));
TextField textFieldCoordinate = new TextField();
VBox coordinateContainer = new VBox(labelCoordinateX, textFieldCoordinate);
Label labelLength = new Label("Распределенная часть (м)");
labelLength.setId("textWithTooltip");
Tooltip.install(labelLength, new Tooltip("Длина распределенной части выпуска (м)"));
TextField textFieldLength = new TextField();
VBox lengthContainer = new VBox(labelLength, textFieldLength);
tile.getChildren().addAll(typeContainer, bankContainer, coordinateContainer);
choiceBoxTypeOfPipes.getSelectionModel().selectedIndexProperty().addListener(
(obs, oldIndex, newIndex) -> {
// may need different logic here: question is unclear
if (newIndex.intValue()==1) {
tile.getChildren().add(lengthContainer);
}
if (oldIndex.intValue()==1) {
tile.getChildren().remove(lengthContainer);
}
}
);
if (concentrate) {
choiceBoxTypeOfPipes.getSelectionModel().select(0);
} else {
choiceBoxTypeOfPipes.getSelectionModel().select(1);
}
TitledPane titledPane = new TitledPane("Параметры выпуска " + (index+1), tile);
return titledPane;
}
public void fillAccordion(int numberOfPipes){
accordionPlant.getPanes().add(createTitledPaneCoastalPipe(0, true));
accordionPlant.getPanes().add(createTitledPaneCoastalPipe(1, false));
}