2012-05-24 47 views
0

我在將數據從一個屏幕中的表格填充到另一個屏幕中的文本字段時遇到問題。我有兩個類FirstClass包含一個文本框和一個按鈕。按下一個按鈕後,將打開一個包含值表的第二個窗口。當用戶雙擊一行時,行的第二列的值應該被插入到FirstClass的文本框中。這兩個類的代碼都附在上面。感謝你在期待。如何在JavaFX 2.0中的其他屏幕TextField上填充TableView數據

import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 

public class FirstClass extends Application { 
public static void main(String[] args) { 
    launch(args); 
} 

@Override 
public void start(final Stage primaryStage) { 
    primaryStage.setTitle("First Class"); 

    GridPane gridpane = new GridPane(); 
    gridpane.setPadding(new Insets(5)); 
    gridpane.setHgap(5); 
    gridpane.setVgap(5); 

    final TextField userNameFld = new TextField(); 
    gridpane.add(userNameFld, 1, 1); 
    Button btn = new Button(); 
    btn.setText("Show Table"); 
    gridpane.add(btn, 1, 3); 

    btn.setOnAction(new EventHandler<ActionEvent>() { 

     @Override 
     public void handle(ActionEvent event) { 
      String a = TableClass.showDialog(primaryStage, true, "Table Window"); 
      userNameFld.setText(a); 
     } 
    }); 

    StackPane root = new StackPane(); 

    Scene scene =new Scene(root, 300, 250); 

    root.getChildren().addAll(gridpane); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 
} 







import javafx.beans.property.SimpleStringProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Modality; 
import javafx.stage.Stage; 

public class TableClass extends Stage { 

private static TableClass dialog; 
private static String value = ""; 


public static class Person { 
    private final SimpleStringProperty firstName; 
    private final SimpleStringProperty lastName; 


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

    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); 
    } 
} 


private TableView<Person> table = new TableView<Person>(); 
    private final ObservableList<Person> data = 
     FXCollections.observableArrayList(
      new Person("JACK", "BROWN"), 
      new Person("JOHN", "VIANNEYS"), 
      new Person("MICHAEL", "NELSON"), 
      new Person("WILLIAM", " CAREY") 
     ); 


public TableClass(Stage owner, boolean modality, String title) { 
    super(); 
    initOwner(owner); 
    Modality m = modality ? Modality.APPLICATION_MODAL : Modality.NONE; 
    initModality(m); 
    setOpacity(1); 
    setTitle(title); 

    StackPane root = new StackPane(); 

    Scene scene = new Scene(root, 750, 750); 

    setScene(scene); 
    GridPane gridpane = new GridPane(); 
    gridpane.setPadding(new Insets(5)); 
    gridpane.setHgap(5); 
    gridpane.setVgap(5); 


    TableColumn firstNameCol = new TableColumn("First Name"); 
    firstNameCol.setMinWidth(100); 
    firstNameCol.setCellValueFactory(
    new PropertyValueFactory<Person,String>("firstName") 
    ); 

    TableColumn lastNameCol = new TableColumn("Last Name"); 
    lastNameCol.setMinWidth(200); 
    lastNameCol.setCellValueFactory(
    new PropertyValueFactory<Person,String>("lastName") 
    ); 



    table.setItems(data); 
    table.getColumns().addAll(firstNameCol, lastNameCol); 



    table.setOnMouseClicked(new EventHandler<MouseEvent>() { 

      public void handle(MouseEvent me) { 

        if (me.getClickCount() >= 2) { 
         String srr = table.getItems().get (table.getSelectionModel().getSelectedIndex()).getLastName(); 
         value = srr; 
         dialog.hide(); 
         } 
       } 
     }); 


    gridpane.add(table, 1, 5,1,20); 
    root.getChildren().add(gridpane); 
    } 


public static String showDialog(Stage stg, Boolean a , String title){ 
     dialog = new TableClass(stg,a, title); 
     dialog.show(); 
     return value; 
    } 
} 

回答

1

的快速簡便的方法(但它引入了2類之間的耦合)將通過userNameFldshowDialog方法,並使其TableClass的成員。然後您可以從TableClass類中更改其值。

更好的方法是bind the valueuserNameFldvalue

+0

Thankx很多它確實與Bind Values一起工作,我認爲這是最好的解決方案。再次感謝您的快速回復。 – user1414600