为什么我的TableView没有刷新或自动更新?

时间:2015-10-30 09:06:55

标签: javafx javafx-2 javafx-8 auto-update

Simply when i add a textfield and clicked on add button my value is not getting inserted to tableview

我尝试了很多,但是当点击添加按钮时,我在文本字段中输入的值没有被填充到tableview。请给我一个解决方案,只要我点击添加按钮就可以将值填充到tableview中!

html, 
body {
    width: 1200px !important;
}

.hidden{display:none;visibility:hidden}
.visible-phone{display:none!important}
.visible-tablet{display:none!important}
.hidden-desktop{display:none!important}
.visible-desktop{display:inherit!important}

2 个答案:

答案 0 :(得分:4)

我不会谈论您的代码的(很多)问题,并且保持答案简短,解决原始问题。

您需要将用户输入的数据添加到支持TableView的ObservableList。

Class Application:
    def __init__(self):
        self.tcl = Tkinter.Tcl()

    def eval(self, cmd):
        return self.tcl.eval(cmd)

    def stop(self):
        # How can I interrupt a thread which is blocking in
        # self.eval() from here?
        pass

答案 1 :(得分:1)

示例刷新tableview代码:

<强> Cloud.java

public class Person {

    private final StringProperty firstName;
    private final StringProperty lastName;


    /**
     * Default constructor.
     */
    public Person() {
        this(null, null);
    }

    /**
     * Constructor with some initial data.
     * 
     * @param firstName
     * @param lastName
     */
    public Person(String firstName, String lastName) {
        this.firstName = new SimpleStringProperty(firstName);
        this.lastName = new SimpleStringProperty(lastName);


    }

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String firstName) {
        this.firstName.set(firstName);
    }

    public StringProperty firstNameProperty() {
        return firstName;
    }

    public String getLastName() {
        return lastName.get();
    }

    public void setLastName(String lastName) {
        this.lastName.set(lastName);
    }

    public StringProperty lastNameProperty() {
        return lastName;
    }


}

<强> Person.java

      public class addcontroller {

            @FXML
            private TextField firstNameField;
            @FXML
            private TextField lastNameField;



            private Stage dialogStage;
            private Person person;
           // private boolean okClicked = false;



            public void setDialogStage(Stage dialogStage) {
                this.dialogStage = dialogStage;
            }

            /**
             * Sets the person to be edited in the dialog.
             * 
             * @param person
             */
            public void setPerson(Person person) {
                this.person = person;}


            @FXML
            private void handleOk() {

                    person.setFirstName(firstNameField.getText());
                    person.setLastName(lastNameField.getText());

                    dialogStage.close();

            }
        }

<强> addcontroller.java

 public class rootcontroller {
            @FXML
            private TableView<Person> personTable;
            @FXML
            private TableColumn<Person, String> firstNameColumn;
            @FXML
            private TableColumn<Person, String> lastNameColumn;



            // Reference to the main application.
            private Cloud mainApp;

            /**
             * The constructor.
             * The constructor is called before the initialize() method.
             */
            public rootcontroller() {
            }
            @FXML
            private void initialize() {
                // Initialize the person table with the two columns.
                firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
                lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
               }

            public void setMainApp(Cloud mainApp) {
                this.mainApp = mainApp;
                personTable.setItems(mainApp.getPersonData());
            }
            @FXML
            private void handleNewPerson() {
                Person tempPerson = new Person();
                System.out.println("1");
                mainApp.showPersonEditDialog(tempPerson);
             mainApp.getPersonData().add(tempPerson);

            }
           }

<强> rootcontroller.java

 <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cloud.rootcontroller">
       <children>
          <TableView fx:id="personTable" layoutX="215.0" layoutY="106.0" prefHeight="200.0" prefWidth="200.0">
            <columns>
              <TableColumn fx:id="firstNameColumn" prefWidth="75.0" text="name" />
              <TableColumn fx:id="lastNameColumn" prefWidth="75.0" text="country" />
            </columns>
          </TableView>
          <Button layoutX="415.0" layoutY="334.0" mnemonicParsing="false" onAction="#handleNewPerson" text="add" />
       </children>
    </AnchorPane>

<强> root.fxml

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cloud.addcontroller">
   <children>
      <TextField fx:id="firstNameField" layoutX="97.0" layoutY="113.0" />
      <TextField fx:id="lastNameField" layoutX="259.0" layoutY="113.0" />
      <Button layoutX="451.0" layoutY="113.0" mnemonicParsing="false" onAction="#handleOk" text="save" />
   </children>
</AnchorPane>

<强> add.fxml

class Parent < ActiveRecord::Base
  has_many :children
end