[This is my following code. Here i wants to access the textField from another class. textField is under overridden method named call().]
public class URLCellFactory {
private static final URLCellFactory urlCellFactory = new URLCellFactory();
// private final TextField textField = new TextField();
public Callback<TableColumn<SQLManager, String>, TableCell<SQLManager, String>> cell() {
Callback<TableColumn<SQLManager, String>, TableCell<SQLManager, String>> cellFactory =
new Callback<TableColumn<SQLManager, String>, TableCell<SQLManager, String>>() {
@Override
public TableCell<SQLManager, String> call(TableColumn<SQLManager, String> elementsStringTableColumn) {
final TableCell<SQLManager, String> cell = new TableCell<SQLManager, String>() {
private final TextField textField = new TextField();
{
urlCellFactory.operateURL(textField);
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(textField);
}
}
};
return cell;
}
};
return cellFactory;
}
public void operateURL(TextField textField) {
textField.setPromptText("Enter value");
textField.setOnAction((ActionEvent event) -> {
System.out.println("Something spoken shit");
textField.setEditable(false);
//getURL(textField.getText());
});
}
// this function should get access from the textField component. Like: textField.getText();
// public static String getURL() {
// return ;
// }
}
[I tried making the textField global, static and also non-static but it corrupt my output.]
public class URLCellFactory {
private static final URLCellFactory urlCellFactory = new URLCellFactory();
private TextField textField = new TextField();
public Callback<TableColumn<SQLManager, String>, TableCell<SQLManager, String>> cell() {
Callback<TableColumn<SQLManager, String>, TableCell<SQLManager, String>> cellFactory =
new Callback<TableColumn<SQLManager, String>, TableCell<SQLManager, String>>() {
@Override
public TableCell<SQLManager, String> call(TableColumn<SQLManager, String> elementsStringTableColumn) {
final TableCell<SQLManager, String> cell = new TableCell<SQLManager, String>() {
//private final TextField textField = new TextField();
{
urlCellFactory.operateURL(textField);
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(textField);
}
}
};
return cell;
}
};
return cellFactory;
}
public void operateURL(TextField textField) {
textField.setPromptText("Enter value");
textField.setOnAction((ActionEvent event) -> {
System.out.println("Something spoken shit");
textField.setEditable(false);
//getURL(textField.getText());
});
}
public String getURL() {
return this.textField.getText();
}
}
[The goal is to access the textField component without corrupting output. As i mentioned earlier i tried every possible ways to make it work but i failed. So, is there any right way to do that properly please provide it would be very helpful. Please also add comment about how your code works if anyone provide any.
Thanks in advance.]
Trying to get the text directly from the
TextFieldof the cell is the wrong approach. TheTableViewAPI does not provide a way to get the cell at a particular index. Also, there can be more items in the table than there are cells being displayed. Using the text fields to store application state will lead to information loss.A
TableViewis backed by a "model". The model is a custom class that exposes properties. The class itself represents a single item in the table, displayed in a single row. The properties represent the values to display in each column. Editing the value of a cell should result in the new value being written back to the property of a model instance. You can get the new values via the items when needed.Cell implementations that allow editing take one of two forms.
The cell can enter and exit the editing state. When editing, the UI changes to a control that allows user input. The implementation works via the
startEdit,cancelEdit, andcommitEditAPI of the cell and outside code reacts to the appropriate events.The cell is "always editing". This bypasses the normal editing API and typically makes use of bidirectional bindings.
Your current implementation seems to be an attempt at the second form. But given you also appear to make the text field non-editable in the on-action handler, it seems the first form would be a better fit. And in that case, you can use the built-in
TextFieldTableCellclass instead of writing your own.Here is an example with a
TableViewthat displaysPersoninstances which have a first name property and a last name property.