我很难找到任何文件,所以提前抱歉这个愚蠢的问题。我想填充一个用FXML声明的选项框,其中的项目是ArrayList的一部分。
这是我的FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="application.LMUController"> <columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="600.0" minWidth="10.0" prefWidth="551.0" /> </columnConstraints> <rowConstraints>
<RowConstraints maxHeight="346.0" minHeight="10.0" prefHeight="222.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="316.0" minHeight="10.0" prefHeight="178.0" vgrow="SOMETIMES" /> </rowConstraints> <children>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="312.0" minWidth="0.0" prefWidth="0.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="443.0" minWidth="10.0" prefWidth="443.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" valignment="CENTER" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label contentDisplay="TOP" text="List Price" GridPane.halignment="RIGHT" GridPane.rowIndex="3">
<GridPane.margin>
<Insets right="8.0" />
</GridPane.margin>
</Label>
<Label contentDisplay="TOP" text="City" GridPane.halignment="RIGHT" GridPane.rowIndex="2">
<GridPane.margin>
<Insets right="8.0" />
</GridPane.margin>
</Label>
<Label contentDisplay="TOP" text="Address" GridPane.halignment="RIGHT" GridPane.rowIndex="1">
<GridPane.margin>
<Insets right="8.0" />
</GridPane.margin>
</Label>
<Label contentDisplay="TOP" text="Comps CSV" GridPane.halignment="RIGHT" GridPane.rowIndex="4">
<GridPane.margin>
<Insets right="8.0" />
</GridPane.margin>
</Label>
<FlowPane GridPane.columnIndex="1" GridPane.rowIndex="1">
<children>
<TextField prefWidth="308.0" />
</children>
</FlowPane>
<FlowPane GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">
<children>
<ChoiceBox id="choiceCity" prefWidth="150.0">
<FlowPane.margin>
<Insets right="8.0" />
</FlowPane.margin>
</ChoiceBox>
<Button id="btnCitiesRefresh" mnemonicParsing="false" text="Refresh" onAction="#btnCitiesRefresh_action">
<FlowPane.margin>
<Insets right="8.0" />
</FlowPane.margin>
</Button>
<Button id="btnLmuPreview" mnemonicParsing="false" text="Preview" />
</children>
</FlowPane>
<FlowPane GridPane.columnIndex="1" GridPane.rowIndex="3">
<children>
<TextField id="btnListPrice" prefWidth="150.0" />
</children>
</FlowPane>
<FlowPane prefHeight="62.0" prefWidth="279.0" GridPane.columnIndex="1" GridPane.rowIndex="4">
<children>
<TextField id="txtCompsCSVPath" editable="false" prefWidth="200.0">
<FlowPane.margin>
<Insets right="8.0" />
</FlowPane.margin>
</TextField>
<Button id="btnCompsCSVBrowse" mnemonicParsing="false" text="Browse" />
</children>
</FlowPane>
</children>
</GridPane>
<GridPane GridPane.halignment="LEFT" GridPane.rowIndex="1">
<GridPane.margin>
<Insets right="8.0" />
</GridPane.margin>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="91.0" minHeight="10.0" prefHeight="89.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="61.0" minHeight="10.0" prefHeight="39.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<FlowPane prefHeight="43.0" prefWidth="592.0" GridPane.rowIndex="1" GridPane.valignment="CENTER">
<children>
<Button id="btnSaveExcel" mnemonicParsing="false" text="Export to Excel">
<FlowPane.margin>
<Insets left="8.0" />
</FlowPane.margin>
</Button>
</children>
</FlowPane>
</children>
</GridPane> </children> </GridPane>
这是我的控制器:
package application;
import java.awt.Choice;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.MenuBar;
import javafx.scene.input.InputEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import localMarketUpdate.*;
public class LMUController implements Initializable{
LMUParser lmuParser;
ChoiceBox choiceCity;
@Override
public void initialize(URL location, ResourceBundle resources) {
lmuParser = new LMUParser();
}
@FXML
private void btnCitiesRefresh_action(ActionEvent event)
{
ArrayList<String> cities = lmuParser.getCities();
ObservableList<String> list = FXCollections.observableArrayList(cities);
for (int i = 0; i < cities.size(); i++){
list.add(i, cities.get(i));
//list.add(i, cities.get(i));
}
choiceCity.setItems(FXCollections.observableArrayList("One","Two","Three"));
}
}
我收到错误:
choiceCity.setItems(FXCollections.observableArrayList("One","Two","Three"));
引起:java.lang.NullPointerException at application.LMUController.btnCitiesRefresh_action(LMUController.java:47) ......还有58个
这里的最终目标是通过填充ArrayList中的项来使ChoiceBox。这是如何完成的,我做错了什么?
答案 0 :(得分:1)
应该这样做:
private void AddButton(CommandBar popupCommandBar)
{
bool isFound = false;
foreach (var commandBarButton in popupCommandBar.Controls.OfType<CommandBarButton>())
{
if (commandBarButton.Tag.Equals("HELLO_TAG"))
{
isFound = true;
Debug.WriteLine("Found existing button. Will attach a handler.");
commandBarButton.Click += eventHandler;
break;
}
}
if (!isFound)
{
var commandBarButton = (CommandBarButton)popupCommandBar.Controls.Add
(MsoControlType.msoControlButton, missing, missing, missing, true);
Debug.WriteLine("Created new button, adding handler");
commandBarButton.Click += eventHandler;
commandBarButton.Caption = "Hello !!!";
commandBarButton.FaceId = 356;
commandBarButton.Tag = "HELLO_TAG";
commandBarButton.BeginGroup = true;
}
}
// add the button to the context menus that you need to support
AddButton(applicationObject.CommandBars["Text"]);
AddButton(applicationObject.CommandBars["Table Text"]);
AddButton(applicationObject.CommandBars["Table Cells"]);
但要实现这一点,ArrayList<String> cities = lmuParser.getCities();
ObservableList<String> list = FXCollections.observableArrayList(cities);
choiceCity.setItems(list);
实际上必须注入,因为您使用ChoiceBox
属性来指定id
的id属性,因此不会发生这种情况。使用fxml命名空间中的Node
属性。同样最好使用id
的类型参数并使用ChoiceBox
修饰符:
private
FXML文件:
将fxml名称空间的名称空间前缀添加到@FXML
private ChoiceBox<String> choiceCity;
:
ChoiceBox