JavaFX在自定义编辑单元格中请求焦点
我目前在一个屏幕中有两个JavaFX在自定义编辑单元格中请求焦点,java,javafx,tableview,focus,tablecell,Java,Javafx,Tableview,Focus,Tablecell,我目前在一个屏幕中有两个TableViews,右侧有一个动态列,右侧有两个静态列,右侧表格的单元格可以更改为可编辑,右键单击单元格并选择菜单的第一个选项,我的问题是我没有得到TextField请求焦点,我尝试添加以下代码,但它不起作用 tableScroll.getFocusModel().focus(row, param); tableScroll.requestFocus(); 任何帮助都将不胜感激 我加上一个例子: FXML文件: package application; import
TableView
s,右侧有一个动态列,右侧有两个静态列,右侧表格的单元格可以更改为可编辑,右键单击单元格并选择菜单的第一个选项,我的问题是我没有得到TextField
请求焦点,我尝试添加以下代码,但它不起作用
tableScroll.getFocusModel().focus(row, param);
tableScroll.requestFocus();
任何帮助都将不胜感激
我加上一个例子:
FXML文件:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("syncrtwotablesGridPane.fxml"));
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
package application;
//*****************************************************************************
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import bean.ColBean;
import bean.RowBean;
import bean.TestBean;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.util.Callback;
public class SyncrTwoTablesController implements Initializable {
@FXML
private ScrollPane scPane;
@FXML
private HBox hBox;
@FXML
private TableView<RowBean> tableNoScroll;
@FXML
private TableView<RowBean> tableScroll;
private TestBean testBean;
@FXML
private TableColumn<RowBean, String> tcName;
@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println("Controller");
initializeBean();
fillTables();
}
private void fillTables() {
tableNoScroll.setItems(FXCollections.observableList(testBean.getLstRow()));
tableScroll.setItems(FXCollections.observableList(testBean.getLstRow()));
tcName.setCellValueFactory(new PropertyValueFactory<RowBean, String>("nameRow"));
List<TableColumn<RowBean, String>> lstColums = new ArrayList<TableColumn<RowBean, String>>();
//TableColumn<RowBean, String> col = null;
tableScroll.getColumns().clear();
if (testBean.getLstRow().size() > 0) {
for(int i = 0; i < testBean.getLstRow().get(0).getLstColBean().size(); i++) {
TableColumn<RowBean, String> col = new TableColumn<RowBean, String>("col"+ i);
int id = i;
col.setCellValueFactory(
new Callback<CellDataFeatures<RowBean, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(CellDataFeatures<RowBean, String> p) {
return p.getValue().getLstColBean().get(id) != null
? p.getValue().getLstColBean().get(id).getColValue()
: new SimpleStringProperty("");
}
});
col.setCellFactory(
new Callback<TableColumn<RowBean, String>, TableCell<RowBean, String>>() {
@Override
public TableCell<RowBean, String> call(
TableColumn<RowBean, String> param) {
EditingCell<RowBean, String> cell = new EditingCell(id);
cell.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
addMenuMonthColumns(param, cell, id);
}
});
return cell;
}
});
lstColums.add(col);
}
tableScroll.getColumns().addAll(lstColums);
}
}
private void addMenuMonthColumns(TableColumn<RowBean, String> param, EditingCell<RowBean, String> cell, int i) {
ContextMenu menu = new ContextMenu();
menu.getItems().addAll(optionOne(param, i), optionTwo());
cell.setContextMenu(menu);
}
private MenuItem optionOne(TableColumn<RowBean, String> param, int i) {
MenuItem menuPlan = new MenuItem("Option 1");
menuPlan.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
int row = tableScroll.getSelectionModel().getSelectedIndex();
RowBean rowBean = tableScroll.getItems().get(row);
ColBean colBean = rowBean.getLstColBean().get(i);
colBean.setEditable(true);
tableScroll.getFocusModel().focus(row, param);
tableScroll.requestFocus();
refresh(tableScroll, tableScroll.getItems());
}
});
return menuPlan;
}
private MenuItem optionTwo() {
MenuItem menuPlan = new MenuItem("Option 2");
menuPlan.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
}
});
return menuPlan;
}
private void initializeBean() {
ColBean colBean = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean2 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean3 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean4 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean5 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean6 = new ColBean(new SimpleStringProperty("hola"));
List<ColBean> lstColBean = new ArrayList<ColBean>();
lstColBean.add(colBean);
lstColBean.add(colBean2);
lstColBean.add(colBean3);
lstColBean.add(colBean4);
lstColBean.add(colBean5);
lstColBean.add(colBean6);
ColBean colBean7 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean8 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean9 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean10 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean11= new ColBean(new SimpleStringProperty("adios"));
ColBean colBean12 = new ColBean(new SimpleStringProperty("adios"));
List<ColBean> lstColBean2 = new ArrayList<ColBean>();
lstColBean2.add(colBean7);
lstColBean2.add(colBean8);
lstColBean2.add(colBean9);
lstColBean2.add(colBean10);
lstColBean2.add(colBean11);
lstColBean2.add(colBean12);
RowBean rowBean = new RowBean(new SimpleStringProperty("hola"), lstColBean);
RowBean rowBean2 = new RowBean(new SimpleStringProperty("adios"), lstColBean2);
List<RowBean> lstRow = new ArrayList<RowBean>();
lstRow.add(rowBean);
lstRow.add(rowBean2);
testBean = new TestBean(new SimpleStringProperty("test"), lstRow);
}
/**
* Method that refresh the contains of the table.
*
* @param table
* of type <code>TableView<T></code>
* @param tableList
* of type <code>List<T></code>
*/
public static <T> void refresh(final TableView<T> table, final List<T> tableList) {
// table.setItems(null);
// table.layout();
// table.setItems(FXCollections.observableList(tableList));
FXCollections.copy(table.getItems(), tableList);
}
}
package application;
import bean.RowBean;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TextField;
public class EditingCell<S, T> extends TableCell<RowBean, String> {
private TextField textField;
private int col;
public EditingCell(int col) {
this.col = col;
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (item != null) {
if (getTableView().getItems().get(getIndex()).getLstColBean().get(col).isEditable()) {
if (textField == null) {
textField = new TextField();
}
textField.setText(item);
textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean newValue) {
if (newValue) {
System.out.println("requested");
textField.selectAll();
}
}
});
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
} else {
setText(item);
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
} else {
setText(null);
setGraphic(null);
}
}
}
}
package bean;
import java.io.Serializable;
import java.util.List;
import javafx.beans.property.SimpleStringProperty;
public class RowBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private SimpleStringProperty nameRow;
private List<ColBean> lstColBean;
public RowBean() {
}
public RowBean(SimpleStringProperty nameRow, List<ColBean> lstColBean) {
super();
this.nameRow = nameRow;
this.lstColBean = lstColBean;
}
/**
* @return the lstColBean
*/
public List<ColBean> getLstColBean() {
return lstColBean;
}
/**
* @param lstColBean
* the lstColBean to set
*/
public void setLstColBean(List<ColBean> lstColBean) {
this.lstColBean = lstColBean;
}
/**
* @return the nameRow
*/
public SimpleStringProperty getNameRowProperty() {
return nameRow;
}
/**
* @param nameRow
* the nameRow to set
*/
public void setNameRowProperty(SimpleStringProperty nameRow) {
this.nameRow = nameRow;
}
public String getNameRow() {
return nameRow.get();
}
public void setNameRow(String nameRow) {
this.nameRow = new SimpleStringProperty(nameRow);
}
}
package bean;
import java.io.Serializable;
import java.util.List;
import javafx.beans.property.SimpleStringProperty;
public class TestBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private SimpleStringProperty name;
private List<RowBean> lstRow;
public TestBean(SimpleStringProperty name, List<RowBean> lstRow) {
super();
this.name = name;
this.lstRow = lstRow;
}
/**
* @return the lstRow
*/
public List<RowBean> getLstRow() {
return lstRow;
}
/**
* @param lstRow
* the lstRow to set
*/
public void setLstRow(List<RowBean> lstRow) {
this.lstRow = lstRow;
}
/**
* @return the name
*/
public SimpleStringProperty getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(SimpleStringProperty name) {
this.name = name;
}
}
Main.java:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("syncrtwotablesGridPane.fxml"));
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
package application;
//*****************************************************************************
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import bean.ColBean;
import bean.RowBean;
import bean.TestBean;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.util.Callback;
public class SyncrTwoTablesController implements Initializable {
@FXML
private ScrollPane scPane;
@FXML
private HBox hBox;
@FXML
private TableView<RowBean> tableNoScroll;
@FXML
private TableView<RowBean> tableScroll;
private TestBean testBean;
@FXML
private TableColumn<RowBean, String> tcName;
@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println("Controller");
initializeBean();
fillTables();
}
private void fillTables() {
tableNoScroll.setItems(FXCollections.observableList(testBean.getLstRow()));
tableScroll.setItems(FXCollections.observableList(testBean.getLstRow()));
tcName.setCellValueFactory(new PropertyValueFactory<RowBean, String>("nameRow"));
List<TableColumn<RowBean, String>> lstColums = new ArrayList<TableColumn<RowBean, String>>();
//TableColumn<RowBean, String> col = null;
tableScroll.getColumns().clear();
if (testBean.getLstRow().size() > 0) {
for(int i = 0; i < testBean.getLstRow().get(0).getLstColBean().size(); i++) {
TableColumn<RowBean, String> col = new TableColumn<RowBean, String>("col"+ i);
int id = i;
col.setCellValueFactory(
new Callback<CellDataFeatures<RowBean, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(CellDataFeatures<RowBean, String> p) {
return p.getValue().getLstColBean().get(id) != null
? p.getValue().getLstColBean().get(id).getColValue()
: new SimpleStringProperty("");
}
});
col.setCellFactory(
new Callback<TableColumn<RowBean, String>, TableCell<RowBean, String>>() {
@Override
public TableCell<RowBean, String> call(
TableColumn<RowBean, String> param) {
EditingCell<RowBean, String> cell = new EditingCell(id);
cell.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
addMenuMonthColumns(param, cell, id);
}
});
return cell;
}
});
lstColums.add(col);
}
tableScroll.getColumns().addAll(lstColums);
}
}
private void addMenuMonthColumns(TableColumn<RowBean, String> param, EditingCell<RowBean, String> cell, int i) {
ContextMenu menu = new ContextMenu();
menu.getItems().addAll(optionOne(param, i), optionTwo());
cell.setContextMenu(menu);
}
private MenuItem optionOne(TableColumn<RowBean, String> param, int i) {
MenuItem menuPlan = new MenuItem("Option 1");
menuPlan.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
int row = tableScroll.getSelectionModel().getSelectedIndex();
RowBean rowBean = tableScroll.getItems().get(row);
ColBean colBean = rowBean.getLstColBean().get(i);
colBean.setEditable(true);
tableScroll.getFocusModel().focus(row, param);
tableScroll.requestFocus();
refresh(tableScroll, tableScroll.getItems());
}
});
return menuPlan;
}
private MenuItem optionTwo() {
MenuItem menuPlan = new MenuItem("Option 2");
menuPlan.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
}
});
return menuPlan;
}
private void initializeBean() {
ColBean colBean = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean2 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean3 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean4 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean5 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean6 = new ColBean(new SimpleStringProperty("hola"));
List<ColBean> lstColBean = new ArrayList<ColBean>();
lstColBean.add(colBean);
lstColBean.add(colBean2);
lstColBean.add(colBean3);
lstColBean.add(colBean4);
lstColBean.add(colBean5);
lstColBean.add(colBean6);
ColBean colBean7 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean8 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean9 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean10 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean11= new ColBean(new SimpleStringProperty("adios"));
ColBean colBean12 = new ColBean(new SimpleStringProperty("adios"));
List<ColBean> lstColBean2 = new ArrayList<ColBean>();
lstColBean2.add(colBean7);
lstColBean2.add(colBean8);
lstColBean2.add(colBean9);
lstColBean2.add(colBean10);
lstColBean2.add(colBean11);
lstColBean2.add(colBean12);
RowBean rowBean = new RowBean(new SimpleStringProperty("hola"), lstColBean);
RowBean rowBean2 = new RowBean(new SimpleStringProperty("adios"), lstColBean2);
List<RowBean> lstRow = new ArrayList<RowBean>();
lstRow.add(rowBean);
lstRow.add(rowBean2);
testBean = new TestBean(new SimpleStringProperty("test"), lstRow);
}
/**
* Method that refresh the contains of the table.
*
* @param table
* of type <code>TableView<T></code>
* @param tableList
* of type <code>List<T></code>
*/
public static <T> void refresh(final TableView<T> table, final List<T> tableList) {
// table.setItems(null);
// table.layout();
// table.setItems(FXCollections.observableList(tableList));
FXCollections.copy(table.getItems(), tableList);
}
}
package application;
import bean.RowBean;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TextField;
public class EditingCell<S, T> extends TableCell<RowBean, String> {
private TextField textField;
private int col;
public EditingCell(int col) {
this.col = col;
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (item != null) {
if (getTableView().getItems().get(getIndex()).getLstColBean().get(col).isEditable()) {
if (textField == null) {
textField = new TextField();
}
textField.setText(item);
textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean newValue) {
if (newValue) {
System.out.println("requested");
textField.selectAll();
}
}
});
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
} else {
setText(item);
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
} else {
setText(null);
setGraphic(null);
}
}
}
}
package bean;
import java.io.Serializable;
import java.util.List;
import javafx.beans.property.SimpleStringProperty;
public class RowBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private SimpleStringProperty nameRow;
private List<ColBean> lstColBean;
public RowBean() {
}
public RowBean(SimpleStringProperty nameRow, List<ColBean> lstColBean) {
super();
this.nameRow = nameRow;
this.lstColBean = lstColBean;
}
/**
* @return the lstColBean
*/
public List<ColBean> getLstColBean() {
return lstColBean;
}
/**
* @param lstColBean
* the lstColBean to set
*/
public void setLstColBean(List<ColBean> lstColBean) {
this.lstColBean = lstColBean;
}
/**
* @return the nameRow
*/
public SimpleStringProperty getNameRowProperty() {
return nameRow;
}
/**
* @param nameRow
* the nameRow to set
*/
public void setNameRowProperty(SimpleStringProperty nameRow) {
this.nameRow = nameRow;
}
public String getNameRow() {
return nameRow.get();
}
public void setNameRow(String nameRow) {
this.nameRow = new SimpleStringProperty(nameRow);
}
}
package bean;
import java.io.Serializable;
import java.util.List;
import javafx.beans.property.SimpleStringProperty;
public class TestBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private SimpleStringProperty name;
private List<RowBean> lstRow;
public TestBean(SimpleStringProperty name, List<RowBean> lstRow) {
super();
this.name = name;
this.lstRow = lstRow;
}
/**
* @return the lstRow
*/
public List<RowBean> getLstRow() {
return lstRow;
}
/**
* @param lstRow
* the lstRow to set
*/
public void setLstRow(List<RowBean> lstRow) {
this.lstRow = lstRow;
}
/**
* @return the name
*/
public SimpleStringProperty getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(SimpleStringProperty name) {
this.name = name;
}
}
syncrtwotablecontroller.java:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("syncrtwotablesGridPane.fxml"));
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
package application;
//*****************************************************************************
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import bean.ColBean;
import bean.RowBean;
import bean.TestBean;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.util.Callback;
public class SyncrTwoTablesController implements Initializable {
@FXML
private ScrollPane scPane;
@FXML
private HBox hBox;
@FXML
private TableView<RowBean> tableNoScroll;
@FXML
private TableView<RowBean> tableScroll;
private TestBean testBean;
@FXML
private TableColumn<RowBean, String> tcName;
@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println("Controller");
initializeBean();
fillTables();
}
private void fillTables() {
tableNoScroll.setItems(FXCollections.observableList(testBean.getLstRow()));
tableScroll.setItems(FXCollections.observableList(testBean.getLstRow()));
tcName.setCellValueFactory(new PropertyValueFactory<RowBean, String>("nameRow"));
List<TableColumn<RowBean, String>> lstColums = new ArrayList<TableColumn<RowBean, String>>();
//TableColumn<RowBean, String> col = null;
tableScroll.getColumns().clear();
if (testBean.getLstRow().size() > 0) {
for(int i = 0; i < testBean.getLstRow().get(0).getLstColBean().size(); i++) {
TableColumn<RowBean, String> col = new TableColumn<RowBean, String>("col"+ i);
int id = i;
col.setCellValueFactory(
new Callback<CellDataFeatures<RowBean, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(CellDataFeatures<RowBean, String> p) {
return p.getValue().getLstColBean().get(id) != null
? p.getValue().getLstColBean().get(id).getColValue()
: new SimpleStringProperty("");
}
});
col.setCellFactory(
new Callback<TableColumn<RowBean, String>, TableCell<RowBean, String>>() {
@Override
public TableCell<RowBean, String> call(
TableColumn<RowBean, String> param) {
EditingCell<RowBean, String> cell = new EditingCell(id);
cell.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
addMenuMonthColumns(param, cell, id);
}
});
return cell;
}
});
lstColums.add(col);
}
tableScroll.getColumns().addAll(lstColums);
}
}
private void addMenuMonthColumns(TableColumn<RowBean, String> param, EditingCell<RowBean, String> cell, int i) {
ContextMenu menu = new ContextMenu();
menu.getItems().addAll(optionOne(param, i), optionTwo());
cell.setContextMenu(menu);
}
private MenuItem optionOne(TableColumn<RowBean, String> param, int i) {
MenuItem menuPlan = new MenuItem("Option 1");
menuPlan.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
int row = tableScroll.getSelectionModel().getSelectedIndex();
RowBean rowBean = tableScroll.getItems().get(row);
ColBean colBean = rowBean.getLstColBean().get(i);
colBean.setEditable(true);
tableScroll.getFocusModel().focus(row, param);
tableScroll.requestFocus();
refresh(tableScroll, tableScroll.getItems());
}
});
return menuPlan;
}
private MenuItem optionTwo() {
MenuItem menuPlan = new MenuItem("Option 2");
menuPlan.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
}
});
return menuPlan;
}
private void initializeBean() {
ColBean colBean = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean2 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean3 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean4 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean5 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean6 = new ColBean(new SimpleStringProperty("hola"));
List<ColBean> lstColBean = new ArrayList<ColBean>();
lstColBean.add(colBean);
lstColBean.add(colBean2);
lstColBean.add(colBean3);
lstColBean.add(colBean4);
lstColBean.add(colBean5);
lstColBean.add(colBean6);
ColBean colBean7 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean8 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean9 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean10 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean11= new ColBean(new SimpleStringProperty("adios"));
ColBean colBean12 = new ColBean(new SimpleStringProperty("adios"));
List<ColBean> lstColBean2 = new ArrayList<ColBean>();
lstColBean2.add(colBean7);
lstColBean2.add(colBean8);
lstColBean2.add(colBean9);
lstColBean2.add(colBean10);
lstColBean2.add(colBean11);
lstColBean2.add(colBean12);
RowBean rowBean = new RowBean(new SimpleStringProperty("hola"), lstColBean);
RowBean rowBean2 = new RowBean(new SimpleStringProperty("adios"), lstColBean2);
List<RowBean> lstRow = new ArrayList<RowBean>();
lstRow.add(rowBean);
lstRow.add(rowBean2);
testBean = new TestBean(new SimpleStringProperty("test"), lstRow);
}
/**
* Method that refresh the contains of the table.
*
* @param table
* of type <code>TableView<T></code>
* @param tableList
* of type <code>List<T></code>
*/
public static <T> void refresh(final TableView<T> table, final List<T> tableList) {
// table.setItems(null);
// table.layout();
// table.setItems(FXCollections.observableList(tableList));
FXCollections.copy(table.getItems(), tableList);
}
}
package application;
import bean.RowBean;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TextField;
public class EditingCell<S, T> extends TableCell<RowBean, String> {
private TextField textField;
private int col;
public EditingCell(int col) {
this.col = col;
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (item != null) {
if (getTableView().getItems().get(getIndex()).getLstColBean().get(col).isEditable()) {
if (textField == null) {
textField = new TextField();
}
textField.setText(item);
textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean newValue) {
if (newValue) {
System.out.println("requested");
textField.selectAll();
}
}
});
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
} else {
setText(item);
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
} else {
setText(null);
setGraphic(null);
}
}
}
}
package bean;
import java.io.Serializable;
import java.util.List;
import javafx.beans.property.SimpleStringProperty;
public class RowBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private SimpleStringProperty nameRow;
private List<ColBean> lstColBean;
public RowBean() {
}
public RowBean(SimpleStringProperty nameRow, List<ColBean> lstColBean) {
super();
this.nameRow = nameRow;
this.lstColBean = lstColBean;
}
/**
* @return the lstColBean
*/
public List<ColBean> getLstColBean() {
return lstColBean;
}
/**
* @param lstColBean
* the lstColBean to set
*/
public void setLstColBean(List<ColBean> lstColBean) {
this.lstColBean = lstColBean;
}
/**
* @return the nameRow
*/
public SimpleStringProperty getNameRowProperty() {
return nameRow;
}
/**
* @param nameRow
* the nameRow to set
*/
public void setNameRowProperty(SimpleStringProperty nameRow) {
this.nameRow = nameRow;
}
public String getNameRow() {
return nameRow.get();
}
public void setNameRow(String nameRow) {
this.nameRow = new SimpleStringProperty(nameRow);
}
}
package bean;
import java.io.Serializable;
import java.util.List;
import javafx.beans.property.SimpleStringProperty;
public class TestBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private SimpleStringProperty name;
private List<RowBean> lstRow;
public TestBean(SimpleStringProperty name, List<RowBean> lstRow) {
super();
this.name = name;
this.lstRow = lstRow;
}
/**
* @return the lstRow
*/
public List<RowBean> getLstRow() {
return lstRow;
}
/**
* @param lstRow
* the lstRow to set
*/
public void setLstRow(List<RowBean> lstRow) {
this.lstRow = lstRow;
}
/**
* @return the name
*/
public SimpleStringProperty getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(SimpleStringProperty name) {
this.name = name;
}
}
RowBean.java:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("syncrtwotablesGridPane.fxml"));
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
package application;
//*****************************************************************************
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import bean.ColBean;
import bean.RowBean;
import bean.TestBean;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.util.Callback;
public class SyncrTwoTablesController implements Initializable {
@FXML
private ScrollPane scPane;
@FXML
private HBox hBox;
@FXML
private TableView<RowBean> tableNoScroll;
@FXML
private TableView<RowBean> tableScroll;
private TestBean testBean;
@FXML
private TableColumn<RowBean, String> tcName;
@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println("Controller");
initializeBean();
fillTables();
}
private void fillTables() {
tableNoScroll.setItems(FXCollections.observableList(testBean.getLstRow()));
tableScroll.setItems(FXCollections.observableList(testBean.getLstRow()));
tcName.setCellValueFactory(new PropertyValueFactory<RowBean, String>("nameRow"));
List<TableColumn<RowBean, String>> lstColums = new ArrayList<TableColumn<RowBean, String>>();
//TableColumn<RowBean, String> col = null;
tableScroll.getColumns().clear();
if (testBean.getLstRow().size() > 0) {
for(int i = 0; i < testBean.getLstRow().get(0).getLstColBean().size(); i++) {
TableColumn<RowBean, String> col = new TableColumn<RowBean, String>("col"+ i);
int id = i;
col.setCellValueFactory(
new Callback<CellDataFeatures<RowBean, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(CellDataFeatures<RowBean, String> p) {
return p.getValue().getLstColBean().get(id) != null
? p.getValue().getLstColBean().get(id).getColValue()
: new SimpleStringProperty("");
}
});
col.setCellFactory(
new Callback<TableColumn<RowBean, String>, TableCell<RowBean, String>>() {
@Override
public TableCell<RowBean, String> call(
TableColumn<RowBean, String> param) {
EditingCell<RowBean, String> cell = new EditingCell(id);
cell.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
addMenuMonthColumns(param, cell, id);
}
});
return cell;
}
});
lstColums.add(col);
}
tableScroll.getColumns().addAll(lstColums);
}
}
private void addMenuMonthColumns(TableColumn<RowBean, String> param, EditingCell<RowBean, String> cell, int i) {
ContextMenu menu = new ContextMenu();
menu.getItems().addAll(optionOne(param, i), optionTwo());
cell.setContextMenu(menu);
}
private MenuItem optionOne(TableColumn<RowBean, String> param, int i) {
MenuItem menuPlan = new MenuItem("Option 1");
menuPlan.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
int row = tableScroll.getSelectionModel().getSelectedIndex();
RowBean rowBean = tableScroll.getItems().get(row);
ColBean colBean = rowBean.getLstColBean().get(i);
colBean.setEditable(true);
tableScroll.getFocusModel().focus(row, param);
tableScroll.requestFocus();
refresh(tableScroll, tableScroll.getItems());
}
});
return menuPlan;
}
private MenuItem optionTwo() {
MenuItem menuPlan = new MenuItem("Option 2");
menuPlan.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
}
});
return menuPlan;
}
private void initializeBean() {
ColBean colBean = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean2 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean3 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean4 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean5 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean6 = new ColBean(new SimpleStringProperty("hola"));
List<ColBean> lstColBean = new ArrayList<ColBean>();
lstColBean.add(colBean);
lstColBean.add(colBean2);
lstColBean.add(colBean3);
lstColBean.add(colBean4);
lstColBean.add(colBean5);
lstColBean.add(colBean6);
ColBean colBean7 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean8 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean9 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean10 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean11= new ColBean(new SimpleStringProperty("adios"));
ColBean colBean12 = new ColBean(new SimpleStringProperty("adios"));
List<ColBean> lstColBean2 = new ArrayList<ColBean>();
lstColBean2.add(colBean7);
lstColBean2.add(colBean8);
lstColBean2.add(colBean9);
lstColBean2.add(colBean10);
lstColBean2.add(colBean11);
lstColBean2.add(colBean12);
RowBean rowBean = new RowBean(new SimpleStringProperty("hola"), lstColBean);
RowBean rowBean2 = new RowBean(new SimpleStringProperty("adios"), lstColBean2);
List<RowBean> lstRow = new ArrayList<RowBean>();
lstRow.add(rowBean);
lstRow.add(rowBean2);
testBean = new TestBean(new SimpleStringProperty("test"), lstRow);
}
/**
* Method that refresh the contains of the table.
*
* @param table
* of type <code>TableView<T></code>
* @param tableList
* of type <code>List<T></code>
*/
public static <T> void refresh(final TableView<T> table, final List<T> tableList) {
// table.setItems(null);
// table.layout();
// table.setItems(FXCollections.observableList(tableList));
FXCollections.copy(table.getItems(), tableList);
}
}
package application;
import bean.RowBean;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TextField;
public class EditingCell<S, T> extends TableCell<RowBean, String> {
private TextField textField;
private int col;
public EditingCell(int col) {
this.col = col;
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (item != null) {
if (getTableView().getItems().get(getIndex()).getLstColBean().get(col).isEditable()) {
if (textField == null) {
textField = new TextField();
}
textField.setText(item);
textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean newValue) {
if (newValue) {
System.out.println("requested");
textField.selectAll();
}
}
});
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
} else {
setText(item);
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
} else {
setText(null);
setGraphic(null);
}
}
}
}
package bean;
import java.io.Serializable;
import java.util.List;
import javafx.beans.property.SimpleStringProperty;
public class RowBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private SimpleStringProperty nameRow;
private List<ColBean> lstColBean;
public RowBean() {
}
public RowBean(SimpleStringProperty nameRow, List<ColBean> lstColBean) {
super();
this.nameRow = nameRow;
this.lstColBean = lstColBean;
}
/**
* @return the lstColBean
*/
public List<ColBean> getLstColBean() {
return lstColBean;
}
/**
* @param lstColBean
* the lstColBean to set
*/
public void setLstColBean(List<ColBean> lstColBean) {
this.lstColBean = lstColBean;
}
/**
* @return the nameRow
*/
public SimpleStringProperty getNameRowProperty() {
return nameRow;
}
/**
* @param nameRow
* the nameRow to set
*/
public void setNameRowProperty(SimpleStringProperty nameRow) {
this.nameRow = nameRow;
}
public String getNameRow() {
return nameRow.get();
}
public void setNameRow(String nameRow) {
this.nameRow = new SimpleStringProperty(nameRow);
}
}
package bean;
import java.io.Serializable;
import java.util.List;
import javafx.beans.property.SimpleStringProperty;
public class TestBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private SimpleStringProperty name;
private List<RowBean> lstRow;
public TestBean(SimpleStringProperty name, List<RowBean> lstRow) {
super();
this.name = name;
this.lstRow = lstRow;
}
/**
* @return the lstRow
*/
public List<RowBean> getLstRow() {
return lstRow;
}
/**
* @param lstRow
* the lstRow to set
*/
public void setLstRow(List<RowBean> lstRow) {
this.lstRow = lstRow;
}
/**
* @return the name
*/
public SimpleStringProperty getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(SimpleStringProperty name) {
this.name = name;
}
}
packagebean;
导入java.io.Serializable;
导入java.util.List;
导入javafx.beans.property.SimpleStringProperty;
公共类RowBean实现了可序列化{
/**
*
*/
私有静态最终长serialVersionUID=1L;
私有SimpleStringProperty nameRow;
私有列表;
公共RowBean(){
}
公共RowBean(SimpleStringProperty名称行,列表lstColBean){
超级();
this.nameRow=nameRow;
this.lstColBean=lstColBean;
}
/**
*@returnthelstcolbean
*/
公共列表getLstColBean(){
返回lstColBean;
}
/**
*@param-lstColBean
*要设置的对象
*/
公共void setLstColBean(列表lstColBean){
this.lstColBean=lstColBean;
}
/**
*@返回nameRow
*/
公共SimpleStringProperty getNameRowProperty(){
返回nameRow;
}
/**
*@param nameRow
*要设置的名称行
*/
公共void setNameRowProperty(SimpleStringProperty nameRow){
this.nameRow=nameRow;
}
公共字符串getNameRow(){
返回nameRow.get();
}
public void setNameRow(字符串nameRow){
this.nameRow=新的SimpleStringProperty(nameRow);
}
}
TestBean.java:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("syncrtwotablesGridPane.fxml"));
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
package application;
//*****************************************************************************
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import bean.ColBean;
import bean.RowBean;
import bean.TestBean;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.util.Callback;
public class SyncrTwoTablesController implements Initializable {
@FXML
private ScrollPane scPane;
@FXML
private HBox hBox;
@FXML
private TableView<RowBean> tableNoScroll;
@FXML
private TableView<RowBean> tableScroll;
private TestBean testBean;
@FXML
private TableColumn<RowBean, String> tcName;
@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println("Controller");
initializeBean();
fillTables();
}
private void fillTables() {
tableNoScroll.setItems(FXCollections.observableList(testBean.getLstRow()));
tableScroll.setItems(FXCollections.observableList(testBean.getLstRow()));
tcName.setCellValueFactory(new PropertyValueFactory<RowBean, String>("nameRow"));
List<TableColumn<RowBean, String>> lstColums = new ArrayList<TableColumn<RowBean, String>>();
//TableColumn<RowBean, String> col = null;
tableScroll.getColumns().clear();
if (testBean.getLstRow().size() > 0) {
for(int i = 0; i < testBean.getLstRow().get(0).getLstColBean().size(); i++) {
TableColumn<RowBean, String> col = new TableColumn<RowBean, String>("col"+ i);
int id = i;
col.setCellValueFactory(
new Callback<CellDataFeatures<RowBean, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(CellDataFeatures<RowBean, String> p) {
return p.getValue().getLstColBean().get(id) != null
? p.getValue().getLstColBean().get(id).getColValue()
: new SimpleStringProperty("");
}
});
col.setCellFactory(
new Callback<TableColumn<RowBean, String>, TableCell<RowBean, String>>() {
@Override
public TableCell<RowBean, String> call(
TableColumn<RowBean, String> param) {
EditingCell<RowBean, String> cell = new EditingCell(id);
cell.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
addMenuMonthColumns(param, cell, id);
}
});
return cell;
}
});
lstColums.add(col);
}
tableScroll.getColumns().addAll(lstColums);
}
}
private void addMenuMonthColumns(TableColumn<RowBean, String> param, EditingCell<RowBean, String> cell, int i) {
ContextMenu menu = new ContextMenu();
menu.getItems().addAll(optionOne(param, i), optionTwo());
cell.setContextMenu(menu);
}
private MenuItem optionOne(TableColumn<RowBean, String> param, int i) {
MenuItem menuPlan = new MenuItem("Option 1");
menuPlan.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
int row = tableScroll.getSelectionModel().getSelectedIndex();
RowBean rowBean = tableScroll.getItems().get(row);
ColBean colBean = rowBean.getLstColBean().get(i);
colBean.setEditable(true);
tableScroll.getFocusModel().focus(row, param);
tableScroll.requestFocus();
refresh(tableScroll, tableScroll.getItems());
}
});
return menuPlan;
}
private MenuItem optionTwo() {
MenuItem menuPlan = new MenuItem("Option 2");
menuPlan.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
}
});
return menuPlan;
}
private void initializeBean() {
ColBean colBean = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean2 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean3 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean4 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean5 = new ColBean(new SimpleStringProperty("hola"));
ColBean colBean6 = new ColBean(new SimpleStringProperty("hola"));
List<ColBean> lstColBean = new ArrayList<ColBean>();
lstColBean.add(colBean);
lstColBean.add(colBean2);
lstColBean.add(colBean3);
lstColBean.add(colBean4);
lstColBean.add(colBean5);
lstColBean.add(colBean6);
ColBean colBean7 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean8 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean9 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean10 = new ColBean(new SimpleStringProperty("adios"));
ColBean colBean11= new ColBean(new SimpleStringProperty("adios"));
ColBean colBean12 = new ColBean(new SimpleStringProperty("adios"));
List<ColBean> lstColBean2 = new ArrayList<ColBean>();
lstColBean2.add(colBean7);
lstColBean2.add(colBean8);
lstColBean2.add(colBean9);
lstColBean2.add(colBean10);
lstColBean2.add(colBean11);
lstColBean2.add(colBean12);
RowBean rowBean = new RowBean(new SimpleStringProperty("hola"), lstColBean);
RowBean rowBean2 = new RowBean(new SimpleStringProperty("adios"), lstColBean2);
List<RowBean> lstRow = new ArrayList<RowBean>();
lstRow.add(rowBean);
lstRow.add(rowBean2);
testBean = new TestBean(new SimpleStringProperty("test"), lstRow);
}
/**
* Method that refresh the contains of the table.
*
* @param table
* of type <code>TableView<T></code>
* @param tableList
* of type <code>List<T></code>
*/
public static <T> void refresh(final TableView<T> table, final List<T> tableList) {
// table.setItems(null);
// table.layout();
// table.setItems(FXCollections.observableList(tableList));
FXCollections.copy(table.getItems(), tableList);
}
}
package application;
import bean.RowBean;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TextField;
public class EditingCell<S, T> extends TableCell<RowBean, String> {
private TextField textField;
private int col;
public EditingCell(int col) {
this.col = col;
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (item != null) {
if (getTableView().getItems().get(getIndex()).getLstColBean().get(col).isEditable()) {
if (textField == null) {
textField = new TextField();
}
textField.setText(item);
textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean newValue) {
if (newValue) {
System.out.println("requested");
textField.selectAll();
}
}
});
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
} else {
setText(item);
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
} else {
setText(null);
setGraphic(null);
}
}
}
}
package bean;
import java.io.Serializable;
import java.util.List;
import javafx.beans.property.SimpleStringProperty;
public class RowBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private SimpleStringProperty nameRow;
private List<ColBean> lstColBean;
public RowBean() {
}
public RowBean(SimpleStringProperty nameRow, List<ColBean> lstColBean) {
super();
this.nameRow = nameRow;
this.lstColBean = lstColBean;
}
/**
* @return the lstColBean
*/
public List<ColBean> getLstColBean() {
return lstColBean;
}
/**
* @param lstColBean
* the lstColBean to set
*/
public void setLstColBean(List<ColBean> lstColBean) {
this.lstColBean = lstColBean;
}
/**
* @return the nameRow
*/
public SimpleStringProperty getNameRowProperty() {
return nameRow;
}
/**
* @param nameRow
* the nameRow to set
*/
public void setNameRowProperty(SimpleStringProperty nameRow) {
this.nameRow = nameRow;
}
public String getNameRow() {
return nameRow.get();
}
public void setNameRow(String nameRow) {
this.nameRow = new SimpleStringProperty(nameRow);
}
}
package bean;
import java.io.Serializable;
import java.util.List;
import javafx.beans.property.SimpleStringProperty;
public class TestBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private SimpleStringProperty name;
private List<RowBean> lstRow;
public TestBean(SimpleStringProperty name, List<RowBean> lstRow) {
super();
this.name = name;
this.lstRow = lstRow;
}
/**
* @return the lstRow
*/
public List<RowBean> getLstRow() {
return lstRow;
}
/**
* @param lstRow
* the lstRow to set
*/
public void setLstRow(List<RowBean> lstRow) {
this.lstRow = lstRow;
}
/**
* @return the name
*/
public SimpleStringProperty getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(SimpleStringProperty name) {
this.name = name;
}
}
packagebean;
导入java.io.Serializable;
导入java.util.List;
导入javafx.beans.property.SimpleStringProperty;
公共类TestBean实现了可序列化{
/**
*
*/
私有静态最终长serialVersionUID=1L;
私有财产名称;
私人名单行;
公共TestBean(SimpleStringProperty名称,列表lstRow){
超级();
this.name=名称;
this.lstRow=lstRow;
}
/**
*@返回第一排
*/
公共列表getLstRow(){
返回lstRow;
}
/**
*@param lstRow
*要设置的第一行
*/
公共无效setLstRow(列表lstRow){
this.lstRow=lstRow;
}
/**
*@返回名称
*/
公共SimpleStringProperty getName(){
返回名称;
}
/**
*@param name
*要设置的名称
*/
公共void集合名(SimpleStringProperty名称){
this.name=名称;
}
}
如果要选择一个单元格并将焦点放在它上,则必须为其启用选择,如
Table.getSelectionModel().setCellSelectionEnabled(true);
希望这能有所帮助。如果你想选择一个单元格并将注意力集中在它上,你必须为它启用选择功能,比如
Table.getSelectionModel().setCellSelectionEnabled(true);
希望这能有所帮助。一个可行但可能不是最好的解决方案是,在EditingCell的
updateItem()
方法中创建文本字段后,直接在文本字段上调用requestFocus()
方法
@覆盖
public void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
// ...
布尔焦点=假;
if(textField==null){
textField=新的textField();
焦点=真;
}
textField.setText(项目);
// ...
设置图形(文本字段);
setContentDisplay(仅限ContentDisplay.GRAPHIC_);
如果(焦点){
Platform.runLater(()->textField.requestFocus());
}
// ...
}
最后,您可以删除已测试的以下行:
tableScroll.getFocusModel().focus(行,参数);
tableScroll.requestFocus();
一个可行但可能不是最好的解决方案是,在EditingCell的updateItem()
方法中创建文本字段后,直接在文本字段上调用requestFocus()
方法
@覆盖
public void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
// ...
布尔焦点=假;
if(textField==null){
textField=新的textField();
焦点=真;
}
textField.setText(项目);
// ...
设置图形(文本字段);
setContentDisplay(仅限ContentDisplay.GRAPHIC_);
如果(焦点){
Platform.runLater(()->textField.requestFocus());
}
// ...
}
最后,您可以删除已测试的以下行:
tableScroll.getFocusModel().focus(行,参数);
tableScroll.requestFocus();
请!您多久需要一次该建议?对不起,我希望帖子现在符合规则,如果您看到任何不符合规则的细节,请告诉我。如果合适,您为什么不在您的单元格内调用textField.requestFocus()
?还有,为什么不使用?很好,终于有了一个例子:)现在让它符合规定的规则:M扔掉所有不相关的东西(f.i.所有的滚动绑定、自定义css、菜单等等)C添加编译所需的所有东西(f.i.数据bean-最好不是原始的,只需使用非常短的示例bean或jdk中包含的内容)V