2016-10-12 37 views
0

目標:
顯示數據,下面,表內缺少代碼表內顯示的數據

"Jacob", "Smith", "[email protected]" 
"Isabella", "Johnson", "[email protected]" 
"Ethan", "Williams", "[email protected]" 
"Emma", "Jones", "[email protected]" 
"Michael", "Brown", "[email protected]" 

問題:
什麼代碼,我在爲了顯示內的數據丟失表?

信息:

  • 我在JavaFX的是新的。
  • 我使用NetBeans
<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="787.0" prefWidth="1086.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> 
    <top> 
     <MenuBar BorderPane.alignment="CENTER"> 
     <menus> 
      <Menu mnemonicParsing="false" text="File"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="Close" /> 
      </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Edit"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="Delete" /> 
      </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Help"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="About" /> 
      </items> 
      </Menu> 
     </menus> 
     </MenuBar> 
    </top> 
    <center> 
     <TableView id="ttt" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER"> 
     <columns> 
      <TableColumn id="firstNameCol" prefWidth="124.0" text="First name" /> 
      <TableColumn id="lastNameCol" prefWidth="139.0" text="Last name" /> 
      <TableColumn id="emailCol" minWidth="7.0" prefWidth="197.0" text="Email" /> 
      <TableColumn id="addressCol" prefWidth="105.0" text="Address" /> 
      <TableColumn id="zipcodeCol" prefWidth="100.0" text="Zipcode" /> 
      <TableColumn id="cityCol" prefWidth="204.0" text="City" /> 
     </columns> 
     </TableView> 
    </center> 
</BorderPane> 
package dreamcrm; 

import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Group; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.TableView; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 


public class DreamCRM extends Application { 

    private TableView<Person> table = new TableView<Person>(); 
    private final ObservableList<Person> data = 
     FXCollections.observableArrayList(
      new Person("Jacob", "Smith", "[email protected]"), 
      new Person("Isabella", "Johnson", "[email protected]"), 
      new Person("Ethan", "Williams", "[email protected]"), 
      new Person("Emma", "Jones", "[email protected]"), 
      new Person("Michael", "Brown", "[email protected]") 
     );  


    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 

     Scene scene = new Scene(root); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 
package dreamcrm; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Label; 


public class FXMLDocumentController implements Initializable { 

    @FXML 
    private Label label; 

    @FXML 
    private void handleButtonAction(ActionEvent event) { 
     System.out.println("You clicked me!"); 
     label.setText("Hello World!"); 
    } 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     // TODO 
    }  

} 
package dreamcrm; 

import javafx.beans.property.SimpleStringProperty; 


public class Person { 

    private final SimpleStringProperty firstName; 
    private final SimpleStringProperty lastName; 
    private final SimpleStringProperty email; 

    public Person(String fName, String lName, String email) { 
     this.firstName = new SimpleStringProperty(fName); 
     this.lastName = new SimpleStringProperty(lName); 
     this.email = new SimpleStringProperty(email); 
    } 

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

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

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

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

    public String getEmail() { 
     return email.get(); 
    } 

    public void setEmail(String fName) { 
     email.set(fName); 
    }  

} 
+1

我應該從哪裏開始?您甚至不會將控制器與fxml關聯。即使你沒有將來自fxml的對象注入到控制器中,也不會分配'fx:id'屬性。假設你得到這個工作......你甚至不試圖將數據傳遞給從fxml創建的節點。如果你得到這個工作,你的'TableColumn'仍然缺少'cellValueFactory's。順便說一句:有一個教程在那裏做一個類似的,如果不是相同的事情:https://docs.oracle.com/javase/8/javafx/fxml-tutorial/fxml_tutorial_intermediate.htm使用它! – fabian

回答

1

你將要使用fx:id而不是id這一點。 fx:id仍然可以使用CSS選擇器,所以不用擔心。

正如@fabian指出的那樣,我錯過了,您還需要在您的fxml文件中添加fx:controller,以便它知道要控制它。

在文檔控制器,你需要讓它知道它是從使用@FXML

所以你這樣的FXML文件得到一些信息,

@FXML 
private TableView ttt; 
@FXML 
private TableColumn<Person, String> firstNameCol, lastNameCol, emailCol,addressCol, zipCol, cityCol; 

在這裏,你用你的fx:id作爲變量名。

然後,在您的initialize函數中,您將需要設置cellValueFactory,就像這樣。

@FXML 
public void initialize(){ 
    firstNamecol.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty()); 
//etc.. 

//Don't forget to add your list that you made to the tableview 
ttt.setItems(list); 
} 

添加完ObservableList你現在可以添加和刪除的項目,它應該相應地更新。

在一個相關的說明,您需要有SimpleStringProperty的可收回金額,即

public StringProperty firstNameProperty(){ 
    return firstName; 
} 

這應該讓你開始。

+0

謝謝你的幫助!此代碼「ttt.setItems(list);」是每個列或somehting別的代碼或是代碼「列表」的ObservableList? –

+0

'ttt.setItems(list)'用數據填充'TableColumns'。 –

+0

「list」是TableCOlumns? –