I have no idea why I get this error message:
Cannot resolve method 'setCellValueFactory' in 'TableColumn'
But when I change to this code in initialize with a test variable, the error message get away:
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
javafx.scene.control.TableColumn kol2 = null;
kol2.setCellValueFactory(new PropertyValueFactory<Tabellmodell,String>("name"));
}
Does anyone know what the problem is with my original code?
package com.example.oppgave;
import javax.swing.table.TableColumn;
import com.example.oppgave.Modell.Ansatt;
import com.example.oppgave.Modell.Tabellmodell;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;
public class AnsattController implements Initializable {
@FXML
private Label tilbakemelding;
@FXML
private TextField innputNavn;
@FXML
private TextField innputId;
@FXML
private TableView<Tabellmodell> ansattTabell;
@FXML
private TableColumn kolname;
@FXML
private TableColumn kolid;
ObservableList<Tabellmodell> oblist= FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
kolname.setCellValueFactory(new PropertyValueFactory<Tabellmodell,String>("name"));
}
private void listAnsatteTabell(){
int c;
try{
Db nyDb= new Db();
Connection tilkobling=nyDb.dbnyTilkobling();
Statement stmt=tilkobling.createStatement();
System.out.println("Connected to the database");
String sql = "SELECT id, navn from ansatt;";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
oblist.add(new Tabellmodell(rs.getString("id"),rs.getString("navn")));
}
ansattTabell.setItems(oblist);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.oppgave.AnsattController">
<children>
<TextField fx:id="innputNavn" layoutX="61.0" layoutY="110.0" />
<TextField fx:id="innputId" layoutX="61.0" layoutY="156.0" />
<Label layoutX="26.0" layoutY="114.0" text="Navn" />
<Label layoutX="26.0" layoutY="160.0" text="Id" />
<Button fx:id="regAnsatt" layoutX="61.0" layoutY="200.0" mnemonicParsing="false" onAction="#regNyAnsatt" text="Registrer" />
<TableView fx:id="ansattTabell" editable="true" layoutX="306.0" layoutY="89.0" prefHeight="200.0" prefWidth="273.0">
<columns>
<TableColumn prefWidth="75.0" text="ID" fx:id="kolid"/>
<TableColumn prefWidth="191.0" text="Navn" fx:id="kolname"/>
</columns>
</TableView>
<Label fx:id="tilbakemelding" layoutX="26.0" layoutY="72.0" />
<Label layoutX="212.0" layoutY="39.0" text="Ansatt">
<font>
<Font size="27.0" />
</font>
</Label>
</children>
</Pane>
I also seem to get a error message when the @FXML variable and the x:id in the fxml is the same