我有一个Java desctop应用程序,用于显示有关电车站的信息(如程序)。该程序从xml文件中读取信息并将其写入表中。我使用Scene Builder用TableView构建GUI,但是“ Passangers”列不显示整数值。可能是什么问题?
这是我的控制人:
public class HoursController implements Initializable {
private XMLStation station = new XMLStation();
private ObservableList<AbstractHour> observableList;
public static void showMessage(String message) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("");
alert.setHeaderText(message);
alert.showAndWait();
}
public static void showError(String message) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText(message);
alert.showAndWait();
}
public static FileChooser getFileChooser(String title) {
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialDirectory(new File("."));
fileChooser.getExtensionFilters().add(
new FileChooser.ExtensionFilter("XML-files (*.xml)", "*.xml"));
fileChooser.getExtensionFilters().add(
new FileChooser.ExtensionFilter("All files (*.*)", "*.*"));
fileChooser.setTitle(title);
return fileChooser;
}
@FXML TextField textFieldStation;
@FXML TextField textFieldText;
@FXML TextArea textAreaResults;
@FXML TableView<AbstractHour> tableViewHours;
@FXML TableColumn<AbstractHour, String> tableColumnTime;
@FXML TableColumn<AbstractHour, Integer> tableColumnPassAmount;
@FXML TableColumn<AbstractHour, String> tableColumnComment;
@Override
public void initialize(URL location, ResourceBundle resources) {
tableViewHours.setPlaceholder(new Label(""));
}
@FXML
public void doNew(ActionEvent event) {
station = new XMLStation();
observableList = null;
textFieldStation.setText("");
textFieldText.setText("");
textAreaResults.setText("");
tableViewHours.setItems(null);
tableViewHours.setPlaceholder(new Label(""));
}
@FXML
public void doOpen(ActionEvent event) {
FileChooser fileChooser = getFileChooser("Open XML-file");
File file;
if ((file = fileChooser.showOpenDialog(null)) != null) {
try {
station.readFromFile(file.getCanonicalPath());
textFieldStation.setText(station.getName());
textAreaResults.setText("");
tableViewHours.setItems(null);
updateTable();
}
catch (IOException e) {
showError("File not found");
}
catch (JAXBException e) {
showError("Wrong file format");
}
}
}
@FXML
public void doSave(ActionEvent event) {
FileChooser fileChooser = getFileChooser("Save XML-file");
File file;
if ((file = fileChooser.showSaveDialog(null)) != null) {
try {
updateSourceData();
station.writeToFile(file.getCanonicalPath());
showMessage("Results were succesfuly saved");
}
catch (Exception e) {
showError("Can't write to file");
}
}
}
@FXML
public void doExit(ActionEvent event) {
Platform.exit();
}
@FXML
public void doAdd(ActionEvent event) {
station.addHour("---", 0, "----");
updateTable();
}
@FXML
public void doRemove(ActionEvent event) {
if (observableList == null) {
return;
}
if (observableList.size() > 0) {
observableList.remove(observableList.size() - 1);
}
if (observableList.size() <= 0) {
observableList = null;
}
}
@FXML public void doSortByPassangers(ActionEvent event) {
updateSourceData();
station.sortByPassangers();
updateTable();
}
@FXML public void doSortByComments(ActionEvent event) {
updateSourceData();
station.sortByComments();
updateTable();
}
@FXML public void doAbout(ActionEvent event) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("About programm...");
alert.setHeaderText("Tram station (Best programm in the word\n from the best programmer in the word)");
alert.setContentText("Ver. 1.0");
alert.showAndWait();
}
@FXML public void nameChanged(ActionEvent event) {
station.setName(textFieldStation.getText());
}
@FXML public void doSearchByWord(ActionEvent event) {
updateSourceData();
textAreaResults.setText("");
for (int i = 0; i < station.hoursCount(); i++) {
AbstractHour h = station.getHour(i);
if (h.containsWord(textFieldText.getText())) {
showResults(h);
}
}
}
@FXML public void doSearchBySubstring(ActionEvent event) {
updateSourceData();
textAreaResults.setText("");
for (int i = 0; i < station.hoursCount(); i++) {
AbstractHour h = station.getHour(i);
if (h.containsSubstring(textFieldText.getText())) {
showResults(h);
}
}
}
private void showResults(AbstractHour hour) {
textAreaResults.appendText("Time: " + hour.getTime() + " року.\n");
textAreaResults.appendText("Passangers:" + hour.getPassAmount() + "\n");
textAreaResults.appendText("Comment:" + hour.getComment() + "\n");
textAreaResults.appendText("\n");
}
private void updateSourceData() {
station.clearHours();
for (AbstractHour c : observableList) {
station.addHour(c);
}
}
private void updateTime(CellEditEvent<AbstractHour, String> t) {
TablePosition<AbstractHour, String> pos = t.getTablePosition();
String newTime = t.getNewValue();
int row = pos.getRow();
AbstractHour h = t.getTableView().getItems().get(row);
h.setTime(newTime);
}
private void updatePass(CellEditEvent<AbstractHour, Integer> t) {
System.out.println(t.getNewValue());
AbstractHour h = t.getTableView().getItems().get(t.getTablePosition().getRow());
int newValue = t.getNewValue();
h.setPassAmount(newValue);
}
private void updateComment(CellEditEvent<AbstractHour, String> t) {
AbstractHour h = t.getTableView().getItems().get(t.getTablePosition().getRow());
h.setComment(t.getNewValue());
}
private void updateTable() {
List<AbstractHour> list = new ArrayList<AbstractHour>();
observableList = FXCollections.observableList(list);
for (int i = 0; i < station.hoursCount(); i++) {
list.add(station.getHour(i));
}
tableViewHours.setItems(observableList);
//tableViewHours.setItems(observableList);
tableColumnTime.setCellValueFactory(new PropertyValueFactory<>("time"));
tableColumnTime.setCellFactory(TextFieldTableCell.forTableColumn());
tableColumnTime.setOnEditCommit(t -> updateTime(t));
tableColumnPassAmount.setCellValueFactory(new PropertyValueFactory<>("passamount"));
tableColumnPassAmount.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
tableColumnPassAmount.setOnEditCommit(t -> updatePass(t));
tableColumnComment.setCellValueFactory(new PropertyValueFactory<>("comment"));
tableColumnComment.setCellFactory(TextFieldTableCell.forTableColumn());
tableColumnComment.setOnEditCommit(t -> updateComment(t));
}
}
这是我的fxml类:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane prefHeight="619.0" prefWidth="989.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="bilous.laba3.oneGUI.HoursController">
<top>
<MenuBar prefHeight="0.0" prefWidth="801.0" BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" onAction="#doNew" text="New" />
<MenuItem mnemonicParsing="false" onAction="#doOpen" text="Open" />
<MenuItem mnemonicParsing="false" onAction="#doSave" text="Save" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" onAction="#doExit" text="Exit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" onAction="#doAdd" text="Add Row" />
<MenuItem mnemonicParsing="false" onAction="#doRemove" text="Delete last row" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Actions">
<items>
<MenuItem mnemonicParsing="false" onAction="#doSortByPassangers" text="Sort by passangers" />
<MenuItem mnemonicParsing="false" onAction="#doSortByComments" text="Sort by comments" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" onAction="#doAbout" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<left>
<AnchorPane prefHeight="587.0" prefWidth="233.0" BorderPane.alignment="CENTER">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Station:" />
<TextField fx:id="textFieldStation" layoutX="13.0" layoutY="41.0" onAction="#nameChanged" prefHeight="31.0" prefWidth="198.0" />
<Label layoutX="14.0" layoutY="122.0" text="Text for searching" />
<TextField fx:id="textFieldText" layoutX="12.0" layoutY="149.0" prefHeight="31.0" prefWidth="200.0" />
<Button layoutX="11.0" layoutY="192.0" mnemonicParsing="false" onAction="#doSearchByWord" prefHeight="31.0" prefWidth="201.0" text="Search by word" />
<Button layoutX="11.0" layoutY="234.0" mnemonicParsing="false" onAction="#doSearchBySubstring" prefHeight="31.0" prefWidth="201.0" text="Search by substring" />
<TextArea fx:id="textAreaResults" layoutX="12.0" layoutY="282.0" prefHeight="200.0" prefWidth="200.0" text=" " AnchorPane.bottomAnchor="105.0" AnchorPane.leftAnchor="12.0" AnchorPane.rightAnchor="21.0" AnchorPane.topAnchor="282.0" />
</children>
</AnchorPane>
</left>
<center>
<TableView fx:id="tableViewHours" editable="true" prefHeight="587.0" prefWidth="749.0" BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="tableColumnTime" editable="true" prefWidth="222.0" text="Time" />
<TableColumn fx:id="tableColumnPassAmount" editable="true" minWidth="0.0" prefWidth="170.0" text="Passangers" />
<TableColumn fx:id="tableColumnComment" editable="true" prefWidth="361.0" text="Comment" />
</columns>
</TableView>
</center>
</BorderPane>
答案 0 :(得分:0)
正确的解决方案:
tableColumnTest.setCellValueFactory(new PropertyValueFactory<>("passAmount"));
passAmount
是AbstractHour的必需(整数)值。