Je suis en train d'écrire une interface graphique pour un projet d'école et Layout Managers m'a énervé alors je suis passé à JavaFX. Je tente de remplir un TableView et TableColumns avec des exemples de données pour l'instant, mais je reçois une erreur étrange avec la liste Observable.Erreur inhabituelle avec JavaFX ObservableList
erreur comme suit:
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 ?>
Mon code actuel:
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;
}
}
Et mon fichier en cours de 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>
Merci!
De quelle ligne provient l'erreur? Pouvez-vous réduire cela au code directement pertinent? Voir [mcve]. –