How disable move focus from textField in JavaFX - javafx

Is there possibility to keep focus on textField in JavaFX?
I do validation on textField using listener.
textField.textProperty().addListener(
new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (!validate(newValue)) {
textField.setStyle("-fx-text-fill: green;");
textField.requestFocus();
} else {
textField.setStyle("-fx-text-fill: black;");
}
}
}
);
It is function which returns boolean depending on textField value validity. If value is not valid then I change text color to RED. Then I want to keep focus on invalid textField and force user to correct value.
Can it be done?
Thanks in advance.

Also use a listener for the focused property which takes back the focus, when it's moved somewhere else:
ChangeListener<Boolean> focusLossListener = (observable, wasFocused, isFocused) -> {
if (!isFocused) {
tf.requestFocus();
}
};
textField.textProperty().addListener(
new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
textField.focusedProperty().removeListener(focusLossListener);
if (!validate(newValue)) {
textField.focusedProperty().addListener(focusLossListener);
textField.setStyle("-fx-text-fill: green;");
textField.requestFocus();
} else {
textField.setStyle("-fx-text-fill: black;");
}
}
}
);

Related

JavaFx How to make checkbox visible in tableColumnCell using cellFactory

I want to add checkBox to tableCell but i dont know what to do to make checkBox visible when i use .setCellFactory
I want to achive this with cellFactory function ---> checkBoxVisible
when i want to make use of cellFactory checkBox are not visible
----> wrong effect
window1.setCellFactory(new BooleanColorCellFactory());
window2.setCellFactory(new BooleanColorCellFactory());
This is BooleanColorCellFactory Class
#Override
public TableCell<Server, CheckBox> call(TableColumn<Server, CheckBox> param) {
return new TableCell<Server, CheckBox>(){
#Override
protected void updateItem(CheckBox item, boolean empty) {
super.updateItem(item, empty);
// if(!empty) {
// setVisible(true);
// setEditable(true);
// getChildren().add(item);
// setText(item.toString());
// if(item.isSelected())
// setStyle(" -fx-background-color: Green");
// else
// setStyle(" -fx-background-color: RED");
}
// }
};
}
}
I have tried some things but nothing was working.
What i need to add in BooleanColorCellFactory to make this work ?
UPDATE:
So i was playing around and i manage to get step closer to solution by adding this into BooleanColorCellFactory Class
if(!getChildren().contains(item))
getChildren().add(item);
but it is buggy and dont looks well and its added after i start scrolling(which is weird behavior for me)--> click
You shouldn't put a Node inside the item class unless you really need to. Furthermore never access the children of a Control directly unless you're writing a Skin for this Control.
You should instead add a BooleanProperty to the Server class:
private final BooleanProperty window1 = new SimpleBooleanProperty();
public boolean isWindow1() {
return window1.get();
}
public void setWindow1(boolean value) {
window1.set(value);
}
public BooleanProperty window1Property() {
return window1;
}
TableColumn<Server, Boolean> window1;
Callback<TableColumn<Server, Boolean>, TableCell<Server, Boolean>> factory = new BooleanColorCellFactory();
// cellValueFactory returns property
window1.setCellValueFactory(new PropertyValueFactory<>("window1"));
window1.setCellFactory(factory);
...
window2.setCellFactory(factory);
#Override
public TableCell<Server, Boolean> call(TableColumn<Server, Boolean> param) {
return new TableCell<Server, Boolean>(){
private final CheckBox checkBox = new CheckBox();
{
checkBox.selectedProperty().addListener((o, oldValue, newValue) -> {
// save new value in row item
WritableValue<Boolean> property = (WritableValue<Boolean>) getTableColumn().getCellObservableValue​(getIndex());
property.setValue(newValue);
});
}
#Override
protected void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setGraphic(null);
setStyle(null);
} else {
setGraphic(checkBox);
checkBox.setSelected(item);
setStyle(item ? "-fx-background-color: Green" : "-fx-background-color: RED");
}
}
};
}
Procede accordingly for window2

Shared ChangeListener VS multiple ChangeListeners? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have several TextFields that need to perform certain actions when focus is lost, like: trimming text, comparing it to initial one, checking it and if it's valid - saving. Which way would be better to handle it? By creating one ChangeListener with switch statement and sharing it among my Textfields:
ChangeListener<Boolean> focusHandler = new ChangeListener<Boolean>() {
private String initialValue = null;
#Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean focus) {
ReadOnlyBooleanProperty booleanProperty = (ReadOnlyBooleanProperty) observable;
TextField textField = (TextField)booleanProperty.getBean();
if (focus) { // gained focus
initialValue = textField.getText();
} else { // lost focus
trimText(textField);
if(!textField.getText().equals(initialValue)){ //text was changed
if(textField.getText().isEmpty()){ //text was erased
switch (textField.getId()) {
case CoreConstants.AREA_SIZE_TF:
emptyAreaSize();
break;
case CoreConstants.NUMBER_OF_PEOPLE_TF:
emptyPeopleLiving();
break;
default:
//no actions
break;
}
}else{
switch (textField.getId()) {
case CoreConstants.NAME_TF:
System.out.println("Execute name code!");
break;
case CoreConstants.SURNAME_TF:
System.out.println("Execute last name code!");
break;
case CoreConstants.MAIL_TF:
System.out.println("Execute mail code!");
break;
default:
//no actions
break;
}}}}}};
nameTextField.focusedProperty().addListener(focusHandler);
surnameTextField.focusedProperty().addListener(focusHandler);
Or by creating separate ChangeListener for each of TextFields like this:
numberOfPeopleTextField.focusedProperty().addListener(new ChangeListener<Boolean>() {
private String initialValue = null;
#Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean focus) {
if (focus) { // gained focus
initialValue = numberOfPeopleTextField.getText();
} else { // lost focus
trimText(numberOfPeopleTextField);
if(!numberOfPeopleTextField.getText().equals(initialValue)){ //text was changed
if(numberOfPeopleTextField.getText().isEmpty()){
emptyPeopleLiving();
}else{
mailInfoUpdate("mail");
}
}
}
}
});
mailTextField.focusedProperty().addListener(new ChangeListener<Boolean>() {
private String initialValue = null;
#Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean focus) {
if (focus) { // gained focus
initialValue = mailTextField.getText();
} else { // lost focus
trimText(mailTextField);
if(!mailTextField.getText().equals(initialValue)){ //text was changed
mailInfoUpdate("mail");
}
}
}
});
Creating several ChangeListeners VS one shared ChangeListener what is better to use and why?
I'd go with option 3: Create a class containing the common code and use different handlers.
class FocusChangeListener implements ChangeListener<Boolean> {
private final TextField textField;
private final Consumer<? super String> changeHandler;
private String initialValue = null;
FocusChangeListener(TextField textField, Consumer<? super String> changeHandler) {
this.textField = textField;
this.changeHandler = changeHandler;
}
#Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean focus) {
if (focus) { // gained focus
initialValue = textField.getText();
} else { // lost focus
trimText(textField);
if(changeHandler != null && !textField.getText().equals(initialValue)) { //text was changed
changeHandler.accept(textField.getText());
}
}
}
}
numberOfPeopleTextField.focusedProperty().addListener(new FocusChangeListener(numberOfPeopleTextField, text -> {
if(text.isEmpty()){
emptyPeopleLiving();
} else{
mailInfoUpdate("mail");
}
}));
mailTextField.focusedProperty().addListener(new FocusChangeListener(mailTextField, text -> mailInfoUpdate("mail")));
If it suits your needs better, you could also replace the Consumer with a abstract method in FocusChangeListener...
Switching on the id is a bad idea since it does not seperate the concerns in addition to using magic strings. Reimplementing the whole listener is not a good idea either since it makes the code harder to maintain...

Table cell combobox - action not performed

I'm applying the below cell factory to a column.
targetEnviroment.setCellFactory(new Callback<TableColumn<DevWorkTabBench, String>, TableCell<DevWorkTabBench, String>>() {
#Override
public TableCell<DevWorkTabBench, String> call(TableColumn<DevWorkTabBench, String> param) {
TableCell<DevWorkTabBench, String> cell = new TableCell<DevWorkTabBench, String>() {
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
String status = null;
try {
status = getTableView().getItems().get(getIndex()).getObjectStatus();
} catch (IndexOutOfBoundsException ex) {
status = "";
}
if (status.equalsIgnoreCase("ReadyForDeployment")) {
ComboBox<String> comboBox = new ComboBox(environmentList);
comboBox.valueProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
commitEdit(newValue);
}
});
comboBox.setOnShown(new EventHandler<Event>() {
#Override
public void handle(Event event) {
getTableView().edit(getIndex(), getTableColumn());
getTableView().getSelectionModel().select(getIndex());
}
});
comboBox.setValue(item);
setGraphic(comboBox);
} else {
setGraphic(null);
}
if (empty) {
setGraphic(null);
}
}
};
return cell;
}
});
When I change the status to the mentioned status, I get the look of ComboBox in that particular cell but the drop down does not occur. Even after multiple clicks no action seems to be performed on the combobox. I do not get any exception other than the handled one. Other columns are editable and performing task as expected.
I have no idea what is wrong here. Can anyone please help me.
Since you are always displaying the combo box in the (non-empty) cells, you don't really need to go into "editing" mode as the standard TextFieldTableCell etc does. Your implementation is more similar to the CheckBoxTableCell, which essentially bypasses the editing mechanism. From the documentation for that class:
Note that the CheckBoxTableCell renders the CheckBox 'live', meaning
that the CheckBox is always interactive and can be directly toggled by
the user. This means that it is not necessary that the cell enter its
editing state (usually by the user double-clicking on the cell). A
side-effect of this is that the usual editing callbacks (such as on
edit commit) will not be called. If you want to be notified of
changes, it is recommended to directly observe the boolean properties
that are manipulated by the CheckBox.
So your cell implementation behaves similarly: don't invoke edit(...) (which I think is messing things up) and don't rely on the commitEdit(...), cancelEdit() etc methods (which won't work as you're not in editing state), but just update the model class directly.
I can't test, since there isn't a MCVE to work from, so this might not work directly, but it should be enough to get you started toward something that will work.
targetEnviroment.setCellFactory(new Callback<TableColumn<DevWorkTabBench, String>, TableCell<DevWorkTabBench, String>>() {
#Override
public TableCell<DevWorkTabBench, String> call(TableColumn<DevWorkTabBench, String> param) {
TableCell<DevWorkTabBench, String> cell = new TableCell<DevWorkTabBench, String>() {
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null) ;
} else {
String status = getTableView().getItems().get(getIndex()).getObjectStatus();
if (status.equalsIgnoreCase("ReadyForDeployment")) {
ComboBox<String> comboBox = new ComboBox(environmentList);
comboBox.valueProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
//commitEdit(newValue);
getTableView().getItems().get(getIndex()).setObjectStatus(newValue);
}
});
comboBox.setValue(item);
setGraphic(comboBox);
} else {
setGraphic(null);
}
}
}
};
return cell;
}
});

Neither startEdit or setOnEditCommit getting called

I followed this example mentioned on this link -
UITableView - Better Editing through Binding?
I changed it a bit accordingly
Model class -
public static class TableData {
private String firstName, lastName;
private TableData(String first, String last) {
this.firstName = first;
this.lastName = last;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Custom Cell factory -
public static class TextFieldCellFactory implements Callback<TableColumn<TableData, String>, TableCell<TableData, String>> {
#Override
public TableCell<TableData, String> call(TableColumn<TableData, String> param) {
TextFieldCell textFieldCell = new TextFieldCell();
return textFieldCell;
}
public static class TextFieldCell extends TableCell<TableData, String> {
private TextField textField;
private StringProperty boundToCurrently = null;
private String newval = "";
public TextFieldCell() {
textField = new TextField();
textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
System.out.println("key pressed");
commitEdit(textField.getText());
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
}
});
textField.textProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// commitEdit(newValue);
System.out.println("" + newValue);
newval = newValue;
}
});
textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if(!newValue){
System.out.println("losing focus" + newval);
//commichange();
commitEdit(textField.getText());
}
}
});
this.setGraphic(textField);
}
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
// Show the Text Field
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
textField.setText(item);
} else {
this.setContentDisplay(ContentDisplay.TEXT_ONLY);
}
}
}
}
setting onEditCommit-
c1.setOnEditCommit(
new EventHandler<TableColumn.CellEditEvent<TableData, String>>() {
#Override
public void handle(TableColumn.CellEditEvent<TableData, String> t) {
System.out.println("ON edit commit" + t);
((TableData) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setFirstName(t.getNewValue());
}
}
);
Problem 1- I want to know what happens when commitEdit() is called. Does it invoke setOnEditCommit ? If it does then why its not invoking setOnEditCoommit
Problem 2 - Why its not going into setOnEditCommit?
Problem 3 I applied startEdit just to check if its enters that field. But that method also is not getting invoked.
Can anyone specify what i am missing here.I don't want a workaround. I need to understand whats the reason behind it
P.S I have removed the binding properties as given in the link.
Your table never enters an editing state (because you never ask it to). Because the cell never has isEditing() return true, the default commitEdit() method becomes a no-op.
You need the TableView to know that it has to start editing a cell when the text field in that cell receives focus. You can do this by modifying the focus listener on the text field:
textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
#Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (newValue) {
getTableView().edit(getIndex(), getTableColumn());
} else {
commitEdit(textField.getText());
}
}
});

Update validator

I'm using this code to validate TextField for network port.
fieldNport.textProperty().addListener(new ChangeListener<String>()
{
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue)
{
IpAddressNameValidator.hide();
if (!newValue.matches("-?\\d+(\\.\\d+)?"))
{
MenuItem cc = new MenuItem(newValue + " is not correct network port");
cc.getStyleClass().add("validator-item");
IpAddressNameValidator.getItems().clear();
IpAddressNameValidator.getItems().add(cc);
IpAddressNameValidator.show(fieldNport, Side.RIGHT, 10, 0);
}
}
});
I noticed that the validator is not updated when I delete the old value with backspace. The only solution that I found is this IpAddressNameValidator.hide(); and then show the validator message again.
I there other way to refresh the validator message when I add or remove values? This solution works but the message is blinking when I add new values.
Every time the text changes and the regex expression matches then you are unnecessarily recreating the MenuItem etc. Rather do it like this:
fieldNport.textProperty().addListener(new ChangeListener<String>()
{
private MenuItem cc = new MenuItem();
{
cc.getStyleClass().add("validator-item");
ipAddressNameValidator.getItems().clear();
ipAddressNameValidator.getItems().add(cc);
}
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue)
{
if (!newValue.matches("-?\\d+(\\.\\d+)?"))
{
if ( ! ipAddressNameValidator.isShowing() )
{
ipAddressNameValidator.show(fieldNport, Side.RIGHT, 10, 0);
}
cc.setText( newValue + " is not correct network port" );
}
}
});

Resources