2016-12-05 63 views
0

我使用TableView开发了一个JavaFX应用程序,我想在其中显示来自MySQL数据库的数据。当我点击按钮来显示所有的数据时,在表格视图中只显示一列带有MySQL行中的行号的列,其他列仍然为空。在JavaFX中使用MySQL TableView

控制器:

public class FXMLDocumentController implements Initializable { 
    private Label label; 

    @FXML 
    private TableView table; 
    @FXML 
    private TextField uname; 
    @FXML 
    private PasswordField pass; 
    @FXML 
    private Button add; 
    @FXML 
    private Button del; 
    @FXML 
    private TableColumn tc1; 
    @FXML 
    private TableColumn tc2; 
    @FXML 
    private TableColumn tc3; 

    private ObservableList<ShowData>data; 
    private Connection1 c; 
    private void handleButtonAction(ActionEvent event) { 
     System.out.println("You clicked me!"); 
     label.setText("Hello World!"); 
    } 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     c= new Connection1(); 
    }  

    private void onClick(ActionEvent event) throws IOException { 
     Stage stage=new Stage(); 
     Parent root = FXMLLoader.load(getClass().getResource("/Box/FXML.fxml")); 
     Scene scene = new Scene(root); 
     stage.setScene(scene); 
     stage.show(); 
    } 

    @FXML 
    private void onAdd(ActionEvent event) { 
    } 

    @FXML 
    private void onDelete(ActionEvent event) { 
    } 

    @FXML 
    private void onOpen(ActionEvent event) { 
     try { 
      Connection conn=c.Connect(); 
      data=FXCollections.observableArrayList(); 
      ResultSet rs=conn.createStatement().executeQuery("SELECT * FROM logs"); 
      while (rs.next()) {     
       data.add(new ShowData(rs.getString(1), rs.getString(2), rs.getString(3))); 
      } 
     } catch (SQLException ex) { 
      System.err.println("Error"+ex); 
     } 
     tc1.setCellValueFactory(new PropertyValueFactory("id")); 
     tc2.setCellValueFactory(new PropertyValueFactory("username")); 
     tc3.setCellValueFactory(new PropertyValueFactory("msg")); 
     table.setItems(null); 
     table.setItems(data); 
    } 
} 

我没有得到任何错误。当我改变为例如:

data.add(new ShowData(rs.getString(2), rs.getString(1), rs.getString(3))); 

在第一列是显示用户名和在其他两列没有。 我的代码有什么问题?

这是ShowData讲座

package javafxapplication7; 
public class ShowData { 

    private final StringProperty id; 
    private final StringProperty username; 
    private final StringProperty msg; 

    public ShowData(String id, String username, String msg) { 
     this.id = new SimpleStringProperty(id); 
     this.username = new SimpleStringProperty(username); 
     this.msg = new SimpleStringProperty(msg); 
    } 

    public String getId() { 
     return id.get(); 
    } 

    public String getuname() { 
     return username.get(); 
    } 

    public String getpass() { 
     return msg.get(); 
    } 

    public void setId(String value) { 
     id.setValue(value); 
    } 

    public void setUname(String value) { 
     username.setValue(value); 
    } 

    public void setPass(String value) { 
     msg.setValue(value); 
    } 

    public StringProperty idproper(){ 
     return id; 
    } 

    public StringProperty unameproper(){ 
     return username; 
    } 

    public StringProperty passproper(){ 
     return msg; 
    } 
} 
+0

我上面显示:) –

回答

1

ShowData类所要求的PropertyValueFactory不符合命名约定的方法。

  • Getters包含大写首字母的属性名称。
  • 属性方法被命名为<property>Property(),即你的情况:
public String getUname() { 
    return username.get(); 
} 

public String getPass() { 
    return msg.get(); 
} 

public StringProperty idProperty(){ 
    return id; 
} 

public StringProperty unameProperty(){ 
    return username; 
} 

public StringProperty passProperty(){ 
    return msg; 
} 

这就是为什么PropertyValueFactory无法识别正确的方法使用,这就是为什么细胞保持为空。

+0

谢谢。这对我很有帮助! –