2013-07-01 2 views
1

저는 JavaFX 및 Java의 초보자입니다. 여러분은 이미 println 함수를 사용하여 데이터베이스의 항목을 인쇄 할 수 있기 때문에 부족한 부분이 있으면이 코드를 이해할 수 있습니까? 오전 나는 무엇을 결여 할 TableView 채우기 FXML 파일로 작성된 TableView가있는 JavaFX에서

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

<AnchorPane id="AnchorPane" prefHeight="594.0" prefWidth="838.0" xmlns:fx="http://javafx.com/fxml" fx:controller="loadingcsvtotableview.TableViewController"> 
<children> 
<TableView fx:id="tblViewer" prefHeight="521.0" prefWidth="808.0" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="59.0"> 
    <columns> 
    <TableColumn prefWidth="75.0" text="ID" fx:id="colID" /> 
    <TableColumn prefWidth="175.0" text="Name" fx:id="colName" /> 
    <TableColumn prefWidth="330.0" text="Address" fx:id="colAddress" /> 
    <TableColumn prefWidth="75.0" text="Age" fx:id="colAge" /> 
    <TableColumn prefWidth="150.0" text="Contact Number" fx:id="colContact" /> 
    </columns> 
</TableView> 
<Button fx:id="cmdExport" layoutX="14.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="112.0" text="Export Data" AnchorPane.topAnchor="21.0" /> 
<Button fx:id="cmdClose" layoutX="144.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="112.0" text="Close" AnchorPane.topAnchor="21.0" /> 
<Button id="cmdClose" fx:id="cmdTest" layoutX="386.0" layoutY="21.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="112.0" text="Test" /> 
</children> 
</AnchorPane> 

:

public class TableViewController implements Initializable{ 

@FXML 
private TableView<studentInfo> tblViewer = new TableView<studentInfo>(); 
@FXML 
private TableColumn colID = new TableColumn(); 
@FXML 
private TableColumn colName = new TableColumn(); 
@FXML 
private TableColumn colAddress = new TableColumn(); 
@FXML 
private TableColumn colAge = new TableColumn(); 
@FXML 
private TableColumn colContact = new TableColumn(); 
@FXML 
private Button cmdTest; 

@Override 
public void initialize(URL url, ResourceBundle rb) { 
// TODO 
System.out.println("READ"); 
System.out.println(this.getClass().getSimpleName() + ".initialize"); 
cmdTest.setOnAction(new EventHandler<ActionEvent>() {    
    @Override 
    public void handle(ActionEvent event) { 
    System.out.println("Button Pressed");     
    colID.setCellValueFactory(new PropertyValueFactory<studentInfo, Integer>("id")); 
    colName.setCellValueFactory(new PropertyValueFactory<studentInfo, String>("name")); 
    colAddress.setCellValueFactory(new PropertyValueFactory<studentInfo, String>("address")); 
    colAge.setCellValueFactory(new PropertyValueFactory<studentInfo, Integer>("age")); 
    colContact.setCellValueFactory(new PropertyValueFactory<studentInfo, String>("contact_num")); 
    tblViewer.getItems().setAll(getAllstudentInfo()); 
    //tblViewer.getColumns().addAll(colID, colName, colAddress, colAge, colContact); 
    //System.out.println(tblViewer.getItems().setAll(getAllstudentInfo())); 
    } 
});  
} 

public class studentInfo{ 

private final SimpleIntegerProperty idCol; 
private final SimpleStringProperty nameCol; 
private final SimpleStringProperty addressCol; 
private final SimpleIntegerProperty ageCol; 
private final SimpleStringProperty contact_numCol; 

public studentInfo(Integer id, String name, String address, Integer age, String contact_num){ 
    this.idCol = new SimpleIntegerProperty(id); 
    this.nameCol = new SimpleStringProperty(name); 
    this.addressCol = new SimpleStringProperty(address); 
    this.ageCol = new SimpleIntegerProperty(age); 
    this.contact_numCol = new SimpleStringProperty(contact_num); 
    } 
    public Integer getidCol(){ 
     return idCol.get(); 
    } 
    public void setidCol(Integer id){ 
     idCol.set(id);  
    } 
    public String getnameCol(){ 
     return nameCol.get(); 
    } 
    public void setnameCol(String name){ 
     nameCol.set(name); 
    } 
    public String getaddressCol(){ 
     return addressCol.get(); 
    } 
    public void setaddressCol(String address){ 
     addressCol.set(address); 
    } 
    public Integer getageCol(){ 
     return ageCol.get(); 
    } 
    public void setageCol(Integer age){ 
     ageCol.set(age); 
    } 
    public String getcontact_numCol(){ 
     return contact_numCol.get(); 
    } 
    public void setcontact_numCol(String contact_num){ 
     contact_numCol.set(contact_num); 
    }   
    } 


public List<studentInfo> getAllstudentInfo(){ 
Connection conn; 
List ll = new LinkedList(); 
Statement st; 
ResultSet rs; 
String url = "jdbc:mysql://localhost/java_test"; 
String user = "root"; 
String pass = ""; 
String driver = "com.mysql.jdbc.Driver"; 

try{ 
    Class.forName(driver); 
    conn = DriverManager.getConnection(url, user, pass); 
    st = conn.createStatement(); 
    String recordQuery = ("Select * from student");    
    rs = st.executeQuery(recordQuery); 
    while(rs.next()){  
     Integer id = rs.getInt("id"); 
     String name = rs.getString("name"); 
     String address = rs.getString("address"); 
     Integer age = rs.getInt("age"); 
     String contact_num = rs.getString("contact_num"); 
     ll.add(new studentInfo(id, name, address, age, contact_num)); 
     System.out.println(id +","+ name +","+ address +","+ age +","+ contact_num +" "+"added."); 
    }    
}catch(ClassNotFoundException | SQLException ex){ 
    Logger.getLogger(TableViewController.class.getName()).log(Level.SEVERE, null, ex); 
} 
return ll; 
} 

그리고 이것은 FXML 파일은? 제발 나를 도와주세요.

답변

0

FXML 가져 오기에서 새 인스턴스를 시작하지 않음으로 시작하십시오. 그냥

@FXML 
private TableColumn<String> colContact; 
2

본 숙박 값 공장은 콩에있는 getter 및 setter 기능과 일치하지 않습니다. 당신이 다음과 같이 셀 값 공장을 설정한다면 :

colID.setCellValueFactory(new PropertyValueFactory<studentInfo, Integer>("id")); 

속성 값 공장 함수 getId() setId()와 idProperty()를 찾습니다. 마지막 것은 빈에있는 속성 자체를 반환합니다. API of PropertyValueFactorytutorial of Table View을 확인해보십시오.

또한 agonist_에서 말하는 것처럼, 두 번 열을 만듭니다.

몇 가지가 있습니다. 버튼을 클릭 할 때마다 CellValueFactory를 설정합니다. 버튼 이벤트가 아닌 초기화 버튼에 넣어야합니다.

getAllstudentInfo() 함수는 GUI가 차단되는 쿼리를 차단합니다. 백그라운드 스레드에서이 작업을 수행해야합니다.

그래서 뭔가에 tblViewer.getItems().setAll(getAllstudentInfo()); 교체 :

Service<ObservableList<studentInfo>> service = new Service<ObservableList<studentInfo>>() {     
@Override 
protected Task<ObservableList<studentInfo>> createTask() { 
    return new Task<ObservableList<studentInfo>>() {        
     @Override 
     protected ObservableList<studentInfo> call() throws Exception { 
      return FXCollections.observableArrayList(getAllstudentInfo()); 
     } 
    }; 
} 
}; 
tblViewer.itemsProperty().bind(service.valueProperty());   
service.start(); 

당신을 도울 것이라는 점을 확실 concurrency tutorial of JavaFX 2을 보라.

희망이 있습니다.

+0

감사합니다. Antonio J. – user2282225

+0

정확하고 정확한 답변으로 저의 혼란을 해결했습니다. 감사합니다 @Antonio J. – Chiranjib

관련 문제