親ウィンドウの名前列のいずれかを選択してる状態でF3キーを入力後子ウィンドウが表示、その子ウィンドウのテキストフィールドに値を入力(文字列、数字何でも可)後OKボタンを押下すると子ウィンドウが閉じ、親ウィンドウの選択していたセルの表記がテキストフィールドで入力された値に切り替わる。というプログラムを作成中です。 ```package sample; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.stage.Stage; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; public class Controller implements Initializable{ @FXML private TableView<Person> tableView; @FXML private TableColumn firstName; @FXML private TableColumn<Person,String> lastName; @FXML private TableColumn email; @Override public void initialize(URL location, ResourceBundle resources) { //セルの編集を許可する処理(一先ず必要ないのでコメアウト) //Callback<TableColumn<Person,String>,TableCell<Person,String>> //lastNameFactory = TextFieldTableCell.forTableColumn(); //lastName.setCellFactory(lastNameFactory); //単一セルの選択の有効化 tableView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); tableView.getSelectionModel().setCellSelectionEnabled(true); //名前列のデータ行のセルの配色変更(sample.css) lastName.setCellFactory(TableColumn -> new TableCell<Person, String>(){ //セルに変更があった場合呼ばれるupdateItemメソッドを利用する public void updateItem(String item,boolean empty){ super.updateItem(item,empty); if(empty || item == null){ setText(null); setGraphic(null); }else{ setText(item); getStyleClass().add("tableCellClass"); } } }); //選択状態を検知するバインディングの設定 tableView.getSelectionModel().selectedItemProperty().addListener((ov,old,current)-> { //どこが選択されてるか確認用出力 System.out.println( "選択セル(TableView)" ); //選択セルの取得処理 for (TablePosition pos : tableView.getSelectionModel().getSelectedCells()) { //選択行、列の取得 int row = pos.getRow(); TableColumn lastName = pos.getTableColumn(); Person item = tableView.getItems().get(row); String selected = (String) lastName.getCellObservableValue(item).getValue(); } }); } //キーイベント処理(F3) 名前カラム選択時 public void onGetfanc(KeyEvent keyEvent) throws IOException { //Person.setLastName(lastName.getText()); tableView.setOnKeyPressed((KeyEvent event) ->{ if(event.getCode() == KeyCode.F3){ //if(se)); try { Stage stage = new Stage(); Parent root = FXMLLoader.load(getClass().getResource("lastName.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.setTitle("名前入力画面"); stage.showAndWait(); } catch (IOException e) { e.printStackTrace(); } } //tableView.setItems(Person); }); } } FXMLファイル <?xml version="1.0" encoding="UTF-8"?> <?import javafx.collections.FXCollections?> <?import javafx.geometry.Insets?> <?import javafx.scene.control.cell.PropertyValueFactory?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import sample.Person?> <?import java.net.URL?> <GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> <padding> <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> </padding> <Label style="-fx-font:NORMAL 20 Tahoma;" text="アドレス帳" GridPane.columnIndex="0" GridPane.rowIndex="0"> </Label> <TableView fx:id="tableView" editable="true" onKeyPressed="#onGetfanc" GridPane.columnIndex="0" GridPane.rowIndex="1"> <columns> <!-- TableCollumクラスを利用して、データを表示する為の列を追加 --> <TableColumn text="名字"> <!-- 列ごとにセル・ファクトリを指定して、データを列に関連付ける --> <cellValueFactory> <!-- PropertyValueFactoryクラスを使用することでセル・ファクトリは実装 --> <!-- Personクラスの対応するメソッドへの参照として表の列のプロパティを使用する --> <PropertyValueFactory property="firstName" /> </cellValueFactory> </TableColumn> <TableColumn fx:id="lastName" text="名前"> <cellValueFactory> <PropertyValueFactory property="lastName" /> </cellValueFactory> </TableColumn> <TableColumn text="メールアドレス"> <cellValueFactory> <PropertyValueFactory property="email"/> </cellValueFactory> </TableColumn> </columns> <items> <!-- データの行を定義して、そのデータを表の列に関連付ける --> <FXCollections fx:factory="observableArrayList"> <Person firstName="Jacob" lastName="aa" email="aaaa.aa@aaaa.aa"/> <Person firstName="Isabella" lastName="bb" email="bbbb.bb@bbbb.bb"/> <Person firstName="Ethan" lastName="cc" email="cccc.cc@cccc.cc"/> <Person firstName="Michael" lastName="dd" email="dddd.dd@dddd.dd"/> </FXCollections> </items> </TableView> <stylesheets> <URL value="@sample.css" /> </stylesheets> <columnConstraints> <ColumnConstraints /> </columnConstraints> <rowConstraints> <RowConstraints /> <RowConstraints /> </rowConstraints> </GridPane>
###試したこと
選択状態の検知をするバインディングは僕なりに記述をしてみたつもりです。
僕自身は後キータイプ処理のイベントハンドラをどうするか。という部分なのですが、列指定の情報を受取りイベントハンドラを起動する。というif文の記述方法が分かりません。説明下手で申し訳ないですが、ご教授して頂けたら幸いです。
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/26 06:48
2017/09/26 08:16