Changing prompt text for JavaFX textfield - javafx

I have a choicebox and textfield next to that choicebox in a JavaFX application. I want grey textbox in the textfield to tell the user what to input. However, I want the prompt text to change according to what is selected in the choicebox.
I looked online and found code on how to have a textfield with prompt text but I couldn't get the prompt text to change with a changeListener on the choicebox.
I tried
textfield = new Textfield(newPrompt);
with the textfield already previously declared with a different prompt text. This did not work. How do I achieve the effect of having changing prompt text based on the users selection in the choicebox?

Instead of reassigning the textfield variable to a new TextField object (via textfield = new TextField(newPrompt);), use the TextField's setPromptText(String s) method in your ChangeListener:
final ChoiceBox<String> box = ...; //choicebox created and filled
box.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
textfield.setPromptText("New Prompt Text!");
}
});

Related

JavaFX: Retrieve TextField from TextFieldTableCell

I have a TableView that I want to auto complete it's cells when in editing mode. Here is the code:
public static <T > void setEditableColumn(TableColumn<T, String> ){
TextFieldTableCell textCell = new TextFieldTableCell();
column.setCellFactory(text.forTableColumn());
}
I will use ControlsFX to do the autocompletion. My question is how can I access the TextField in textCell

Dynamically Adding context menu items to tableview columns

I have the following controller that is instantiated many times on my gui. The reason is beacause it has a tableview that gets filled with different kind of data. Looks like this
class Controller {
#FXML
TableView<Map<String, String> myTable;
private Manager manager;
//Each TableView has different ammount of columns with different names that get dynamically added to the table view using this function
public void setUpColumns(List<TableColumn<Map<String, String>, String>> columns){
myTable.getColumns().addAll(columns);
addContextMenuToColumnHeaders();
}
private addContextMenuToColumnHeaders(){
for (TableColumn<Map<String, String>, ?> tc : myTable.getColumns()){
ContextMenu addToGraphContextMenu = createAddToGraphContextMenu(tc);
tc.setContextMenu(addToGraphContextMenu);
}
}
private ContextMenu createAddToGraphContextMenu(TableColumn<Map<String, String> String> tc){
for (MangerHandledObject mHO : manager.getHandledObjects()){
MenuItem menuItem = new MenuItem(mHO.getName());
menuItem.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event){
//I want each menu item to have access to the column that is added to get the name of the column. Even after dynamically adding new menuItems
manager.callMethod(tc.getName());
}
});
}
}
}
The manager handled objects are not static. So the are added and deleted from the list that the manager keeps. I tried this
contextMenu.setOnShowing(......)
and before showing it will always check for the list from the manager and re-make the context menu items. But the problems is that when this executes I don't have access to the columns anymore. Is there any way to bypass this? Should I implement my own context menu to have a field of the column Name?
It worked. But I had to add at least one dummy MenuItem on my context menu in order for it to appear.

JavaFX FXML menuItem action

I'm trying to set a variable value when a MenuItem i chosen in a MenuButton object.
I've tried to search for this but I've came up empty handed.
Here's the code to set the MenuItems:
private ObservableList<MenuItem> templateMenuItems = FXCollections.observableArrayList();
#FXML private MenuButton menu = new MenuButton();
#FXML
protected void getTemplates() throws IOException {
CaspReturn tls = this.socket.runCmd(new Tls(""));
String tlsList = tls.getResponse();
String[] tlsListSplitt = tlsList.split("\\n");
for (int i = 0; i < tlsListSplitt.length; i++) {
String[] tlsLine = tlsListSplitt[i].split("\"");
this.templateMenuItems.add(new MenuItem(tlsLine[1]));
}
this.menu.getItems().setAll(this.templateMenuItems);
}
I'm not sure how to write the code to get the text from a menuItem or which field in scenebuilder the method should be in.
It's not clear what your asking, but I'll assume that you want to know the text of a menu item when it is clicked. To do that, you need to add an event handler onto the menu item. The following is clipped from the JavaDoc for ContextMenu:
MenuItem item1 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
You can get the event source, cast in to MenuItem and then get the text from that.
There's a real problem with your code the way it's written, though. You have calls to an external database in getTemplates, and as it's implemented as an #FXML element that almost guarantees that it'll be run on the FXAT, which is really, really bad.
I'd refactor that so that the database access is in a Task, and then the MenuItem creation is a handler for the onSucceeded event of the Task. Then you need instantiate a ContextMenu and install the MenuItem's on it in that event handler.
The getTemplates() method should be called as the onAction event for the button.

JavaFX passing data between screens

I have two screens which will be loaded at the start of program. I have a text field and a button on screen 1 and a label on screen2.
Now when I enter any text in text field of screen1 and and press the button, the label on screen2 should be set to that text (which was entered in screen1). (both the screens are active all the time right from program launch, so we need to update the label after the FXML is loaded).
I have tried properties and binding but had no success.
Controller 1: (Mapped to Controller1.fxml)
public class Controller1 {
#FXML private Button button;
#FXML private TextField tfield;
}
Controller 2: (Mapped to Controller2.fxml)
public class Controller2 {
#FXML private Label label;
}
Both the stages/screens are created from MainDriver.java class.

Instantiating ObservableIntegerValue in javafx

I have a variable ObservableIntegerValue called score .
I wanted to create a listener for it to listen for the changes of its value and according to that change the javafx label text displayed on my pane.
But in the method initialize() i need to instantiate and give it the initial value of 500 let's say.
How could it be done?
Beside an observable value, to make the score variable also bindable, you can use IntegerProperty instead of ObservableIntegerValue. IntegerProperty is an IntegerExpression, so it also implements ObservableIntegerValue interface, where IntegerExpression is,
A IntegerExpression is a ObservableIntegerValue plus additional
convenience methods to generate bindings in a fluent style.
IntegerProperty score = new SimpleIntegerProperty(500);
Text text = new Text("-");
// Bind score to text, to show on scene.
text.textProperty().bind(score.asString());
score.set(700); // new value
and the listener
score.addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
// value changed
}
});

Resources