English 中文(简体)
如何在 JavaFX 2. 0 中在其他屏幕的文本字段中填入表视图数据?
原标题:How to populate TableView data on the other screen TextField in JavaFX 2.0

我无法将数据从一个屏幕中的表格装入另一个屏幕中的文本字段。 我有两个“ 第一” 类别, 包含一个文本框和一个按钮。 按下按钮时, 打开第二个窗口, 包含一个数值表。 当用户双击一行时, 第二列的值应该插入到第一“ 屏幕” 的文本框中。 两个类别的代码都附在后面。 感谢您期待 。

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;
  }
 }
最佳回答

简单快捷的方式(但它引入了两个类之间的连接)是将 userNameFld 传递到您的 showDialog 方法,使其成为表格分类的成员。然后,您可以从表格分类类中更改其值 。

更好的办法是将userNameFld 的值 改为 value

问题回答

暂无回答




相关问题
format text_field helper?

Is it possible to apply strftime formatting to the value of a text input field using a Rails text_field helper? In the form I use for creating or editing a record, I have a text input field which is ...

django admin TinyMCE integration

This is weird: I have installed and configured django-tinymce, but it doesn t seem to work with django admin. this works fine with Safari: class ArticleAdmin(admin.ModelAdmin): ...

TextField in UITableView

I m new to iphone world .. help me out of this. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *MYIdentifier =@"MyIdentifier"; ...

Static text within text field?

How can i keep static text within a <input type="text" name="site"> text field, just like the tumblr account ***.tumblr.com here > http://www.tumblr.com/

Select only first four lines, from Sql text field

Sql Server 2008 Express >> Visual Web Developer >> C# I m pulling records out of the table like this: SELECT Name, Category, Review FROM ReviewTable This works fine but the Review field Type in SQL ...

Flash Dynamic Text Width

Hey! I try to find out the text size of a textfield in the following example field.text = aaaaaaa ; trace(field.textWidth); setTimeput(function(){ field.text = aa ; trace(field.textWidth); },...

Textfield and Go Button

I would like to place search textfield and the Go button side by side. It looks aligned equally in Moz, Opera, IE8, Safari but not in IE7. IE7 shows 1px gap in height. How it can be done for IE7?