我正在为一个学校项目编写一个GUI,而布局管理员很生气,所以我切换到了JavaFX。我现在尝试使用示例数据填充TableView和TableColumns,但是我在Observable List中遇到了一个奇怪的错误。
错误如下:
Error:(84, 27) java: method addAll in interface javafx.collections.ObservableList<E> cannot be applied to given types;
required: javafx.scene.control.TableColumn<capture#1 of ?,?>[]
found: javafx.scene.control.TableColumn<Pizza,java.lang.String>,javafx.scene.control.TableColumn<Pizza,java.lang.String>,javafx.scene.control.TableColumn<Pizza,java.lang.String>
reason: varargs mismatch; javafx.scene.control.TableColumn<Pizza,java.lang.String> cannot be converted to javafx.scene.control.TableColumn<capture#1 of ?,?>
Error:(85, 33) java: incompatible types: javafx.collections.ObservableList<Pizza> cannot be converted to javafx.collections.ObservableList<capture#2 of ?>
我当前的代码:
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.cell.PropertyValueFactory;
import java.net.URL;
import java.util.ResourceBundle;
public class GUIController implements Initializable {
//<editor-fold desc="A Bunch of FXML stuff(Leave Folded if you want to live)">
@FXML
private RadioButton small;
@FXML
private ToggleGroup Size;
@FXML
private RadioButton medium;
@FXML
private RadioButton large;
@FXML
private CheckBox cheese;
@FXML
private CheckBox pepper;
@FXML
private CheckBox olives;
@FXML
private CheckBox pine;
@FXML
private CheckBox tomato;
@FXML
private CheckBox mushroom;
@FXML
private TableView<?> table;
@FXML
private TableColumn<Pizza, String> tableSize;
@FXML
private TableColumn<Pizza, String> tableDesc;
@FXML
private TableColumn<Pizza, String> tablePrice;
@FXML
private Button submit;
@FXML
private Button admin;
//</editor-fold>
@Override
public void initialize(URL location, ResourceBundle resources) {
//ADD CODE HERE!!!
tableStuff();
}
public void tableStuff(){
tableDesc = new TableColumn<>("Description");
tableSize = new TableColumn<>();
tablePrice = new TableColumn<>();
tableSize.setCellValueFactory(new PropertyValueFactory<>("size"));
tableDesc.setCellValueFactory(new PropertyValueFactory<>("desc"));
tablePrice.setCellValueFactory(new PropertyValueFactory<>("Price"));
table = new TableView<>();
table.getColumns().addAll(tableSize,tableDesc,tablePrice);
table.setItems(tableFill());
}
public ObservableList<Pizza> tableFill() { //TODO: create input into pizzas
ObservableList<Pizza> pizzas = FXCollections.observableArrayList();
//char size, Boolean cheese, Boolean pepperoni, Boolean olives, Boolean pineapple, Boolean tomatoes, Boolean mushrooms
pizzas.add(new Pizza("S", "Stuff", "$1.1 Mil."));
pizzas.add(new Pizza("S", "Stuff", "$1.1 Mil."));
return pizzas;
}
}
我目前的FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="734.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="GUIController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="550.0" minHeight="10.0" prefHeight="329.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="536.0" minHeight="10.0" prefHeight="444.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="536.0" minHeight="10.0" prefHeight="96.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<GridPane prefHeight="342.0" prefWidth="600.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="54.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="54.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="84.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="109.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="159.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="178.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="178.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="178.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<RadioButton fx:id="small" mnemonicParsing="false" selected="true" text="Small" GridPane.rowIndex="1">
<toggleGroup>
<ToggleGroup fx:id="Size" />
</toggleGroup>
<GridPane.margin>
<Insets left="100.0" />
</GridPane.margin>
</RadioButton>
<RadioButton fx:id="medium" mnemonicParsing="false" text="Medium" toggleGroup="$Size" GridPane.rowIndex="2">
<GridPane.margin>
<Insets left="100.0" />
</GridPane.margin>
</RadioButton>
<RadioButton fx:id="large" mnemonicParsing="false" text="Large" toggleGroup="$Size" GridPane.rowIndex="3">
<GridPane.margin>
<Insets left="100.0" />
</GridPane.margin>
</RadioButton>
<Label text="Pizza Size" textAlignment="CENTER" GridPane.halignment="CENTER" GridPane.valignment="BOTTOM">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<CheckBox fx:id="cheese" mnemonicParsing="false" prefWidth="76.0" text="Cheese" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="100.0" />
</GridPane.margin>
</CheckBox>
<CheckBox fx:id="pepper" mnemonicParsing="false" text="Pepperoni" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets left="100.0" />
</GridPane.margin>
</CheckBox>
<CheckBox fx:id="olives" mnemonicParsing="false" text="Black Olives" GridPane.columnIndex="1" GridPane.rowIndex="3">
<GridPane.margin>
<Insets left="100.0" />
</GridPane.margin>
</CheckBox>
<CheckBox fx:id="pine" mnemonicParsing="false" text="Pineapple" GridPane.columnIndex="1" GridPane.rowIndex="4">
<GridPane.margin>
<Insets left="100.0" />
</GridPane.margin>
</CheckBox>
<CheckBox fx:id="tomato" mnemonicParsing="false" text="Tomatoes" GridPane.columnIndex="1" GridPane.rowIndex="5">
<GridPane.margin>
<Insets left="100.0" />
</GridPane.margin>
</CheckBox>
<CheckBox fx:id="mushroom" mnemonicParsing="false" text="Mushrooms" GridPane.columnIndex="1" GridPane.rowIndex="6">
<GridPane.margin>
<Insets left="100.0" />
</GridPane.margin>
</CheckBox>
<Label text="Pizza Toppings" textAlignment="CENTER" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="BOTTOM">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
</children>
</GridPane>
<TableView fx:id="table" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1">
<columns>
<TableColumn fx:id="tableSize" prefWidth="75.0" text="Size" />
<TableColumn fx:id="tableDesc" prefWidth="434.0" text="Description" />
<TableColumn fx:id="tablePrice" prefWidth="75.0" text="Price" />
</columns>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</TableView>
<GridPane GridPane.rowIndex="2">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="194.0" minWidth="10.0" prefWidth="69.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="413.0" minWidth="10.0" prefWidth="413.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="143.0" minWidth="10.0" prefWidth="69.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button fx:id="submit" mnemonicParsing="false" prefHeight="31.0" prefWidth="184.0" text="Submit Order" GridPane.columnIndex="1" GridPane.halignment="CENTER" />
<Button fx:id="admin" mnemonicParsing="false" prefHeight="25.0" prefWidth="30.0" styleClass="buttonWithImage" stylesheets="@Style.css" GridPane.columnIndex="2" GridPane.halignment="CENTER" />
</children>
</GridPane>
</children>
</GridPane>
谢谢!
答案 0 :(得分:2)
private TableView<?> table;
将?
替换为表格行的确切类型:Pizza
。