javafx tableview没有显示使用scenebuilder创建的数据表

时间:2017-06-21 08:01:48

标签: javafx tableview scenebuilder

您好我有javafx tableview问题。我尝试过解决方案,但我无法解决问题。有行,可以在行之间切换但数据未显示。我不明白问题出在哪里。你能帮我解决这个问题吗?

这是用户类:

public enum Task {
    ADMINISTRATOR("Yetkili Dispeçer"),
    DISPATCHER("Dispeçer"),
    MAINTENANCE_PERSON("Bakim Sorumlusu"),
    OBSERVER("Gözlemci");

    private String taskEnumString;

    Task(String aTaskEnumString){
        this.taskEnumString = aTaskEnumString;
    }

    public String getTaskEnumString(){return taskEnumString;}
}

//Görev property tanimi
private SimpleStringProperty aTask;

public String getTask(){
    return aTask.get();
}

public void setTask(String task){
    aTask.set(task);
}

public StringProperty taskProperty(){
    return aTask;
}

//Username property tanimi
private SimpleStringProperty aUsername;

public String getUsername(){
    return aUsername.get();
}

public void setUsername(String username){
    aUsername.set(username);
}

public StringProperty usernameProperty(){
    return aUsername;
}

//Password property tanimi
private SimpleStringProperty aPassword;

public String getPassword(){
    return aPassword.get();
}

public void setPassword(String password){
    aPassword.set(password);
}

public StringProperty passwordProperty(){
    return aPassword;
}

//Firstname property tanimi
private SimpleStringProperty aFirstname;

public String getFirstname(){
    return aFirstname.get();
}

public void setFirstname(String firstname){
    aFirstname.set(firstname);
}

public StringProperty firstnameProperty(){
    return aFirstname;
}

//Lastname property tanimi
private SimpleStringProperty aLastname;

public String getLastname(){
    return aLastname.get();
}

public void setLastname(String lastname){
    aLastname.set(lastname);
}

public StringProperty lastnameProperty(){
    return aLastname;
}

//Lastname property tanimi
private SimpleBooleanProperty anIsActive;

public boolean getIsActive(){
    return anIsActive.get();
}

public void setIsActive(boolean isActive){
    anIsActive.set(isActive);
}

public BooleanProperty isActiveProperty(){
    return anIsActive;
}

public User(String taks, String username, String password, String firstName, String lastName, boolean isActive){
    this.aTask = new SimpleStringProperty(taks);
    this.aUsername = new SimpleStringProperty(username);
    this.aPassword = new SimpleStringProperty(password);
    this.aFirstname = new SimpleStringProperty(firstName);
    this.aLastname = new SimpleStringProperty(lastName);
    this.anIsActive = new SimpleBooleanProperty(isActive);
}

这是控制器类:

@FXML private javafx.scene.control.TableView<User> userTableView;

public void initialize() throws SQLException {
    try {
        addUsers();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public void clickAddUser() throws Exception{

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/addUser.fxml"));
    Parent root =  fxmlLoader.load();
    Stage stage = new Stage();
    stage.setScene(new Scene(root));
    stage.show();
}

//tablewiev'e kullanici eklenmesi
public void addUsers() throws SQLException {
    data = FXCollections.observableArrayList();

    try {
        List<User> userList = Main.dbManager.getUserList();

        if(userList.size() != 0){
            for (int i=0; i<userList.size(); i++){
                User aUser = userList.get(i);
                data.add(aUser);
            }
        }

        if(userTableView != null)
            userTableView.setItems(data);

    } catch (SQLException e) {
        e.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?> <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="525.0" prefWidth="792.0" style="-fx-background-color: linear-gradient(lightgray,gray);
-fx-padding: 10 10 10 10;" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.userManagementController">    <children>
      <Pane layoutX="16.0" layoutY="14.0" prefHeight="56.0" prefWidth="759.0" style="-fx-border-color: #000000; -fx-border-width:
0.05px; -fx-padding: 10;" >
         <children>
            <Label layoutX="14.0" layoutY="6.0" prefHeight="17.0" prefWidth="114.0" text="KULLANICI ADI" />
            <TextField layoutX="14.0" layoutY="23.0" prefHeight="25.0" prefWidth="361.0" />
            <Label layoutX="399.0" layoutY="6.0" prefHeight="17.0" prefWidth="83.0" text="AKTIF/PASIF" />
            <Button layoutX="399.0" layoutY="23.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="95.0" text="Aktif" />
            <Button layoutX="493.0" layoutY="23.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="95.0" text="Pasif" />
            <Button fx:id="addUser" layoutX="612.0" layoutY="23.0" mnemonicParsing="false" onAction="#clickAddUser" prefHeight="25.0" prefWidth="133.0" style="-fx-background-color: linear-gradient(dodgerblue,blue); -fx-padding: 3px 10px 3px 10px;
-fx-background-radius: 30,30,29,28;" text="Add User" textFill="#f8f6f6" />
         </children>
      </Pane>
      <TableView fx:id="userTableView" layoutX="16.0" layoutY="77.0" prefHeight="433.0" prefWidth="759.0">
        <columns>
          <TableColumn fx:id="tbcProfile" prefWidth="75.0" />
            <TableColumn fx:id="tbcUsername" prefWidth="175.0" text="Kullanici Adi" />
          <TableColumn fx:id="tbcTask" prefWidth="123.0" text="Görev" />
            <TableColumn fx:id="tbcName" minWidth="0.0" prefWidth="97.0" text="Adi" />
            <TableColumn fx:id="tbcSurname" prefWidth="129.0" text="Soyadi" />
            <TableColumn fx:id="tbcIsActive" prefWidth="79.0" text="Aktif/Pasif" />
            <TableColumn fx:id="tbcEdit" prefWidth="79.0" />
        </columns>
      </TableView>    </children>  </Pane>

我改变了这样的代码,但我遇到了同样的问题。我检查过数据列表不为空。

公共类UserManagementController {

public ObservableList<UserTableListObject> data;
@FXML private javafx.scene.control.TableView<UserTableListObject> userTableView;

TableColumn<UserTableListObject, String> isActiveColumn;
TableColumn<UserTableListObject, String> userNameColumn;
TableColumn<UserTableListObject, String> taskColumn;
TableColumn<UserTableListObject, String> firstNameColumn;
TableColumn<UserTableListObject, String> lastNameColumn;
TableColumn<UserTableListObject, String> editDeleteColumn;


public void initialize() throws SQLException {
    try {
        initializeColumns();
        addUsers();
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

public void initializeColumns()
{
    isActiveColumn = new TableColumn<UserTableListObject, String>("Aktif/Pasif");
    isActiveColumn.setMinWidth(100);
    isActiveColumn.setCellValueFactory(new PropertyValueFactory<UserTableListObject, String>("isActiveCol"));

    userNameColumn = new TableColumn<UserTableListObject, String>("Kullanici Adi");
    userNameColumn.setMinWidth(200);
    userNameColumn.setCellValueFactory(new PropertyValueFactory<UserTableListObject, String>("userNameCol"));

    taskColumn = new TableColumn<UserTableListObject, String>("Görev");
    taskColumn.setMinWidth(200);
    taskColumn.setCellValueFactory(new PropertyValueFactory<UserTableListObject, String>("taskCol"));

    firstNameColumn = new TableColumn<UserTableListObject, String>("Adi");
    firstNameColumn.setMinWidth(200);
    firstNameColumn.setCellValueFactory(new PropertyValueFactory<UserTableListObject, String>("firstNameCol"));

    lastNameColumn = new TableColumn<UserTableListObject, String>("Soyadi");
    lastNameColumn.setMinWidth(200);
    lastNameColumn.setCellValueFactory(new PropertyValueFactory<UserTableListObject, String>("lastNameCol"));

    editDeleteColumn = new TableColumn<UserTableListObject, String>("Düzenle/Sil");
    editDeleteColumn.setMinWidth(200);
    editDeleteColumn.setCellValueFactory(new PropertyValueFactory<UserTableListObject, String>("editDeleteCol"));
}

public void clickAddUser() throws Exception{

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/addUser.fxml"));
    Parent root =  fxmlLoader.load();
    Stage stage = new Stage();
    stage.setScene(new Scene(root));
    stage.show();
}

//tablewiev'e kullanici eklenmesi
public void addUsers() throws SQLException {
    data = FXCollections.observableArrayList();
    try {
        List<User> userList = Main.dbManager.getUserList();

        if(userList.size() != 0){
            for (int i=0; i<userList.size(); i++){
                User aUser = userList.get(i);
                data.add(new UserTableListObject( aUser.getIsActive(), aUser.getUsername(),aUser.getTask() ,aUser.getFirstname(), aUser.getLastname()));
            }
        }

        userTableView.setItems(data);
        userTableView.getColumns().addAll(isActiveColumn,userNameColumn,taskColumn,firstNameColumn,lastNameColumn, editDeleteColumn);



    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}

1 个答案:

答案 0 :(得分:2)

我解决了这个问题,这些是我的代码和fxml文件。

@FXML private javafx.scene.control.TableView<UserTableListObject> userTableView;

@FXML private TableColumn<UserTableListObject, String> isActiveColumn;
@FXML private TableColumn<UserTableListObject, String> userNameColumn;
@FXML private TableColumn<UserTableListObject, String> taskColumn;
@FXML private TableColumn<UserTableListObject, String> firstNameColumn;
@FXML private TableColumn<UserTableListObject, String> lastNameColumn;


public void initialize() throws SQLException {
   try {
        addUsers();
    } catch (SQLException e) {
        e.printStackTrace();
    }


}

public void clickAddUser() throws Exception{

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/addUser.fxml"));
    Parent root =  fxmlLoader.load();
    Stage stage = new Stage();
    stage.setScene(new Scene(root));
    stage.show();
}

//tablewiev'e kullanici eklenmesi
public void addUsers() throws SQLException {
    ObservableList<UserTableListObject> data = FXCollections.observableArrayList();

    try {
        List<User> userList = Main.dbManager.getUserList();

     if(userList.size() != 0){
            for (int i=0; i<userList.size(); i++){
                User aUser = userList.get(i);
                data.add(new UserTableListObject(aUser.getIsActive(), aUser.getUsername(),aUser.getTask() ,aUser.getFirstname(), aUser.getLastname()));
            }
        }

        isActiveColumn.setMinWidth(200);
        isActiveColumn.setCellValueFactory(new PropertyValueFactory<UserTableListObject, String>("isActiveCol"){
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<UserTableListObject, String> param) {
                return new ReadOnlyObjectWrapper(param.getValue().getIsActive());
            }
        });

        userNameColumn.setMinWidth(200);
        userNameColumn.setCellValueFactory(new PropertyValueFactory<UserTableListObject, String>("userNameCol"){
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<UserTableListObject, String> param) {
                return new ReadOnlyObjectWrapper(param.getValue().getUsername());
            }
        });

        taskColumn.setMinWidth(200);
        taskColumn.setCellValueFactory(new PropertyValueFactory<UserTableListObject, String>("taskCol"){
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<UserTableListObject, String> param) {
                return new ReadOnlyObjectWrapper(param.getValue().getTask());
            }
        });

        firstNameColumn.setMinWidth(200);
        firstNameColumn.setCellValueFactory(new PropertyValueFactory<UserTableListObject, String>("firstNameCol"){
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<UserTableListObject, String> param) {
                return new ReadOnlyObjectWrapper(param.getValue().getFirstName());
            }
        });

        lastNameColumn.setMinWidth(200);
        lastNameColumn.setCellValueFactory(new PropertyValueFactory<UserTableListObject, String>("lastNameCol"){
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<UserTableListObject, String> param) {
                return new ReadOnlyObjectWrapper(param.getValue().getLastName());
            }
        });

        userTableView.setItems(data);

    } catch (SQLException e) {
        e.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?>
>     
>     <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="782.0"
> prefWidth="1174.0" style="-fx-background-color:
> linear-gradient(lightgray,gray); -fx-padding: 10 10 10 10;"
> xmlns="http://javafx.com/javafx/8.0.111"
> xmlns:fx="http://javafx.com/fxml/1"
> fx:controller="sample.UserManagementController">
>        <children>
>           <Pane layoutX="16.0" layoutY="14.0" prefHeight="56.0" prefWidth="1147.0" style="-fx-border-color: #000000; -fx-border-width:
> 0.05px; -fx-padding: 10;">
>              <children>
>                 <Label layoutX="14.0" layoutY="6.0" prefHeight="17.0" prefWidth="114.0" text="KULLANICI ADI" />
>                 <TextField layoutX="14.0" layoutY="23.0" prefHeight="25.0" prefWidth="622.0" />
>                 <Label layoutX="774.0" layoutY="5.0" prefHeight="17.0" prefWidth="83.0" text="AKTIF/PASIF" />
>                 <Button layoutX="774.0" layoutY="23.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="95.0"
> text="Aktif" />
>                 <Button layoutX="869.0" layoutY="23.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="95.0"
> text="Pasif" />
>                 <Button fx:id="addUser" layoutX="1000.0" layoutY="23.0" mnemonicParsing="false" onAction="#clickAddUser"
> prefHeight="25.0" prefWidth="133.0" style="-fx-background-color:
> linear-gradient(dodgerblue,blue); -fx-padding: 3px 10px 3px 10px;
> -fx-background-radius: 30,30,29,28;" text="Add User" textFill="#f8f6f6" />
>              </children>
>           </Pane>
>           <TableView fx:id="userTableView" layoutX="14.0" layoutY="78.0" prefHeight="690.0" prefWidth="1147.0">
>              <columns>
>                 <TableColumn fx:id="isActiveColumn" prefWidth="75.0" text="Aktif/Pasif" />
>                 <TableColumn fx:id="userNameColumn" prefWidth="75.0" text="Kullanici Adi" />
>                 <TableColumn fx:id="taskColumn" prefWidth="75.0" text="Görev" />
>                 <TableColumn fx:id="firstNameColumn" prefWidth="75.0" text="Adi" />
>                 <TableColumn fx:id="lastNameColumn" prefWidth="75.0" text="Soyadi" />
>              </columns></TableView>
>        </children>
>     </Pane>