2017-02-26 150 views
-1

我正在爲一個學校項目編寫一個圖形用戶界面,佈局管理員讓我生氣,所以我切換到了JavaFX。我試圖用現在的示例數據填充TableView和TableColumns,但是我使用Observable List得到了一個奇怪的錯誤。JavaFX的異常錯誤ObservableList

錯誤,如下所示:

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> 

謝謝!

+1

哪條線是錯誤來自哪裏?你可以修改這只是直接相關的代碼?見[mcve]。 –

回答

2
private TableView<?> table; 

替換?具有確切類型表中的行的:Pizza

+0

哇,我覺得自己像個白癡......謝謝!那固定了那部分,但現在我變得另一個奇怪。 –

+0

新問題=新問題。再次,當您發佈新問題時,請將您的代碼減少到[mcve]。 –

+0

下次會做,我也想出了是什麼原因造成的。謝謝您的幫助。 –