2016-03-22 7 views
-2

テーブルはfxmlです。 カラム:ファーストネーム、姓、電話番号、メールアドレス 正確なメールアドレスを編集するには、Emailコラムでマウスをクリックしてください。 次のコードが正しくない働いている、ここで正確なセル、テーブルの正確なセルを編集/開く方法

table.setOnMouseClicked(new EventHandler<javafx.scene.input.MouseEvent>() { 
+0

あなたはTableCell代わりが提供する編集機能を使用する必要がありますか?これは完全ですか? –

+0

これで完了です。私はfxmlの名前の電子メールを持っていますが、私はこの列だけを使用したいのですが、私はそれをどのようにして行うことができるのでしょうか? –

答えて

0

ではなく、編集することは常に開いている電子メールには、テーブルビューの列を編集する方法の簡単な例です。テキストフィールドの列を使用すると、クリックするとデータの入力が可能になります。この場合、電子メール列のみが編集可能です。

import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.scene.Scene; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.TextFieldTableCell; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 


public class JavaFXApplication7 extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     TableView<Person> tv = new TableView(); 
     TableColumn<Person, String> fCol = new TableColumn<>(); 
     TableColumn<Person, String> lCol = new TableColumn<>(); 
     TableColumn<Person, String> pCol = new TableColumn<>(); 
     TableColumn<Person, String> eCol = new TableColumn<>(); 

     tv.setEditable(true); 
     tv.getColumns().addAll(fCol, lCol, pCol, eCol); 

     fCol.setCellValueFactory(data -> data.getValue().firstName); 
     lCol.setCellValueFactory(data -> data.getValue().lastName); 
     pCol.setCellValueFactory(data -> data.getValue().phone); 
     eCol.setCellValueFactory(data -> data.getValue().email); 
     eCol.setCellFactory(tc -> new TextFieldTableCell<>()); 

     ObservableList<Person> items = FXCollections.observableArrayList(); 
     Person p = new Person(); 
     p.email.set("[email protected]"); 
     p.firstName.set("Tony"); 
     p.lastName.set("Stark"); 
     p.phone.set("(555) 555-1212"); 

     items.add(p); 

     tv.setItems(items); 

     StackPane root = new StackPane(); 
     root.getChildren().add(tv); 

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

     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

    public class Person { 
     StringProperty firstName = new SimpleStringProperty(); 
     StringProperty lastName = new SimpleStringProperty(); 
     StringProperty phone = new SimpleStringProperty(); 
     StringProperty email = new SimpleStringProperty(); 



    } 

} 
0
table.setOnMouseClicked(new EventHandler<javafx.scene.input.MouseEvent>() { 

全体ではなくTableViewのクリックハンドラを設定します。この方法では、クリックされた列を区別できません。コードの残りの部分である

// Simplified Person class 
public class Person { 

    private final StringProperty name; 

    private final StringProperty email; 

    public Person(String name, String email) { 
     this.email = new SimpleStringProperty(email); 
     this.name = new SimpleStringProperty(name); 
    } 

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

    public final void setEmail(String value) { 
     this.email.set(value); 
    } 

    public final StringProperty emailProperty() { 
     return this.email; 
    } 

    public final String getName() { 
     return this.name.get(); 
    } 

    public final void setName(String value) { 
     this.name.set(value); 
    } 

    public final StringProperty nameProperty() { 
     return this.name; 
    } 

} 
TableView<Person> table = new TableView<>(FXCollections.observableArrayList(
     new Person("Darth Vader", "[email protected]"), 
     new Person("James Bond", "[email protected]"))); 
table.setEditable(true); 

Callback<TableColumn<Person, String>, TableCell<Person, String>> cellFactory = col 
     -> new TableCell<Person, String>() { 

      { 
       // make cell itself editable 
       setEditable(true); 
      } 

      @Override 
      public void startEdit() { 
       super.startEdit(); 
       // open dialog for input when the user edits the cell 
       TextInputDialog dialog = new TextInputDialog(getItem()); 
       dialog.setGraphic(null); 
       dialog.setHeaderText("Set new " + col.getText() + "."); 
       dialog.setTitle("Edit " + col.getText()); 
       Optional<String> opt = dialog.showAndWait(); 
       if (opt.isPresent()) { 
        commitEdit(opt.get()); 
       } else { 
        cancelEdit(); 
       } 
      } 

      @Override 
      protected void updateItem(String item, boolean empty) { 
       super.updateItem(item, empty); 
       setText(empty ? null : item); 
      } 

     }; 

TableColumn<Person, String> name = new TableColumn<>("name"); 
name.setCellValueFactory(p -> p.getValue().nameProperty()); 
name.setCellFactory(cellFactory); 

TableColumn<Person, String> email = new TableColumn<>("email"); 
email.setCellValueFactory(p -> p.getValue().emailProperty()); 
email.setCellFactory(cellFactory); 

table.getColumns().addAll(name, email); 
+0

SceneBuilderのコードを別のコードで使用すると、fxmlのシーンは使用されません。JPanel –

+0

@AlexAs:トップレベルのクラスを 'cellFactory'として使用すると、fxml内のパーツを簡単にアセンブルすることができます(この場合、新しいイベントハンドラ(){'私がチェックした最後の時間に、SceneBuilderは 'cellValueFactory' /' cellFactory'を選択することを許可しなかったので、SceneBuilderによって生成されたfxmlsだけに頼ると、とにかくタスクを達成することができなくなります。 。 – fabian

関連する問題