hema chandra
hema chandra

Reputation: 420

How to accept SOH character from TextField in JAVAFx

I have written simple GUI screen that actually contains a JavaFx textField where the user enters a string and that String I have to pass to TCP client.

Now the problem is user enters a string that contains SOH as the delimiter. SOH is nothing but "\u001". But when the user enters a String that contains this delimiters then it is removed and only the simple plain text is retrieved. How can I eliminate this. This unique character is very important for me. For example my string is as follows:

8=FIX.4.2\u001 9=9 \u001 35=A \u001 34=1\u001 49=TTDS68AP

Observer the above String where I have the \u001 character representing SOH. But when I entered this String in the TextField then the result is like:

8=FIX.4.29=9135=A34=149=TTDS68AP

How can I get the SOH character too from the Text Field?

Upvotes: 2

Views: 5157

Answers (3)

Oleg
Oleg

Reputation: 6314

The problem is that javaFX TextField filters invalid characters with the following function:

private static boolean isInvalidCharacter(char c, boolean newlineIllegal, boolean tabIllegal) {
    if (c == 0x7F) return true;
    if (c == 0xA) return newlineIllegal;
    if (c == 0x9) return tabIllegal;
    if (c < 0x20) return true;
    return false;
}

This happens after you paste and before the String is stored in the field. Before this function is called a custom filter can be set and applied, in this filter you can change SOH to something else; it's unicode representation is a good candidate:

textField.setTextFormatter(new TextFormatter<>((t) -> {
    t.setText(t.getText().replace((char)1, '\u2401'));
    return t;
}));

Adding the above will change it after it's pasted and before it's stored. When you want to use the String from the textField you need to replace it back to SOH with:

String withSOH = field.getText().replace('\u2401', (char)1);

Upvotes: 4

Joop Eggen
Joop Eggen

Reputation: 109547

I think the problem is that you want to enter by keyboard and see the control character in the text field:

  • Ctrl-A (SOH) is a known shortcut, and selects the previous character.
  • The control character will not be displayed in a normal font.

So I came to the following solution:

    String SOH_REPR = "°"; // We show SOH as this string.
    someTextField.setOnKeyPressed((ke) -> {
                if (ke.getCode().getName().equals("A")
                        && ke.isControlDown()
                        && !ke.isAltDown()
                        && !ke.isMetaDown()
                        && !ke.isShiftDown()) {
                    TextField textField = (TextField) ke.getSource();
                    ke.consume();
                    int pos = textField.getCaretPosition();
                    textField.insertText(pos, SOH_REPR);
                }
            });
    someTextField.setOnKeyReleased((ke) -> {
                if (ke.getCode().getName().equals("A")
                        && ke.isControlDown()
                        && !ke.isAltDown()
                        && !ke.isMetaDown()
                        && !ke.isShiftDown()) {
                    ke.consume();
                }
            });

// Convert it back.
String text = someTextField.getText().replace(SOH_REPR, "\u0001");

Pasting text with SOHs would still needed to be done for completeness sake.

Upvotes: 1

KL_
KL_

Reputation: 1513

Try adding the your encoding as argument.

System.setProperty("file.encoding", "yourEncoding");

I think UTF-8 should work.

Upvotes: -1

Related Questions