-1

I am converting an app written using Swing to JavaFX. I need an array of TextFields that I can manipulate collectively and as individual array members addressed by the array index. The following code works in Swing but I cannot find the equivalent in JavaFX, using TextField instead of JTextField. How can I achieve this in JavaFX?

private ArrayList<JTextField> fieldList= new ArrayList<JTextField>();

fieldList.add(fldCompletion);
fieldList.add(fldHrsToDate);
fieldList.add(fldHrsToComplete);

for(JTextField fl : fieldList) {
        fl.setText("");
        fl.setEnabled(true);    //FX equivalent is setDisable(false)
    }

fieldList.get(var).setText("");
fabian
  • 80,457
  • 12
  • 86
  • 114
Nick Ford
  • 21
  • 1
  • 4
  • 2
    I do not get the question, does replacing JTextField with TextField doesn't work? – ItachiUchiha Mar 05 '16 at 09:55
  • This approach should work, if done correctly. Please add what you tried. Right now it's hard to see why this would fail (except if you just replace the `JTextField`s with `TextField`s and try to add them to a Swing panel). – fabian Mar 05 '16 at 11:05

3 Answers3

0

I also don't understand your question because this part of the code should work in JavaFX in the same way as it works in Java(Swing) and you are not telling us what your actual problem is. So I just make a wild guess. Maybe you have just forgotten to add your text field to the scene graph too and therefore your are seeing nothing.

mipa
  • 10,369
  • 2
  • 16
  • 35
0

Apologies - it's my first post. I can instantiate the ArrayList -

private ArrayList<TextField> fieldList = new ArrayList<TextField>();

but when I try to add a TextField object to the array I get syntax errors:

fieldList.add(fldCompletion);

Multiple markers at this line - Syntax error, insert ")" to complete MethodDeclaration - Syntax error on token ".", @ expected after this token - Syntax error, insert "SimpleName" to complete QualifiedName - Syntax error, insert "Identifier (" to complete MethodHeaderName

This particular field is declared thus:

@FXML private TextField fldCompletion;
Nick Ford
  • 21
  • 1
  • 4
  • 1
    Could you please provide some small, complete, compilable piece of code? – mipa Mar 05 '16 at 12:46
  • I made a small test app to show the problem but it compiles OK! In the test app the TextFields are instantiated with the new keyword but in the app where I have the problem the TextFields aren't instantiated as such as I made them in Scene Builder. So I guess the question could be "how do you put a collection of controls that are specified in a FXML file into an array?". – Nick Ford Mar 05 '16 at 14:41
  • Where are you trying to add the `TextField` to the list? Must be done inside the initialize method. something like `@FXML private void initialize() { fieldList.add(fldCompletion); }` – Antonio Raposo Mar 05 '16 at 15:36
  • Thanks, Antonio - that has fixed it - I must research the initialize method! – Nick Ford Mar 05 '16 at 17:19
0

This will do just fine

TextField[] txt = new TextField[beanFields.length];
for (int i=0; i<=beanFields.length-1; i++) {
                TextField textField = new TextField();
                txt[i] = textField;
                textField.setPrefWidth(200);
                textField.setPrefHeight(32);
}
Howard J
  • 421
  • 3
  • 7