-1

We are have an error when we try to obtain the first few character of a string
First we tried to remove the last two offending characters
That produced the same error we are seeing when we try to obtain the first few characters
The On Key Typed method is attached to a textfield when MAX character are exceeded a custom alert
is fired. We have looked at many ways to remove or get sub-strings in various SO questions before posting

The string entered will never be the same hence we do not know the specific character to replace
Here is the code and a screen shot of the ERROR notice that the "o" in front of the original text
The string we are entering is "This is a test to see how many yo"
We are trying to obtain only the "This is a test to see how many"
The System.out.println(strNew) is exactly that but when strNew is add to the textfield the "o" shows up

Our question is how to prevent this ?
OR What is the cause of the odd text that is palaced in the textfield?

Here is the Minimal Code to test

public class Atest extends Application {
@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("test.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}
public static void main(String[] args) {
    launch(args);
}

The Controller

public class testController implements Initializable {
@FXML
TextField txtDesc;
@FXML
private void handleButtonAction(ActionEvent event) {
    txtDesc.setText("Thanks");
}
@FXML
private void descLEN(){
    txtDesc.setOnKeyTyped(event ->{
    int maxCharacters = 10;
    if(txtDesc.getText().length() > maxCharacters)
        event.consume();
    });    
}
@Override
public void initialize(URL url, ResourceBundle rb) { 
}    

We have no idea how to post FXML Code all you need is a TextField
with the id txtDesc and set the OnKeyTyped to descLEN

Vector
  • 3,066
  • 5
  • 27
  • 54
  • 1
    [mcve] please .. – kleopatra Oct 19 '19 at 22:22
  • @kleopatra No other code to post this is a test application and this is all the code. We did not post the FXML file for it adds very little and the Custom Alert adds nothing to the clarity of the question Thanks for your concern – Vector Oct 19 '19 at 22:32
  • @kleopatra We also tested with out the Custom Alert and the same ERROR shows up We think the On Key Typed method may be the cause but we tested this without that in the mix and we see the same error – Vector Oct 19 '19 at 22:36
  • 2
    Follow the link Kleopatra posted. Do you think we want to reproduce the FXML and other code from scratch to help you fix your problem? – SedJ601 Oct 20 '19 at 02:18
  • 2
    Also if you want to set the max characters a TextField can have, use TextFormatter. – SedJ601 Oct 20 '19 at 02:19
  • See Kleopatra's answer [here](https://stackoverflow.com/questions/15159988/javafx-2-2-textfield-maxlength) – SedJ601 Oct 20 '19 at 02:22
  • @Sedrick OK sorry about the FXML Yes you code fixes the problem I had to remove the public static void addTextLimiter(final TextField txtDescription, final int maxLength) { AS I have never figured out how to implement or call this code structure I am still confused as to WHY the little "o" is placed at the beginning of the textfield OR FXML only has the textfield the other are just set never populated in code that is why we did not post the FXML Thank You for the FIX – Vector Oct 20 '19 at 03:15

1 Answers1

0

Because you are consumed with the Custom Alert you may not like our answer
I call this We Hear You Knocking but You Can NOT Come In Your event is consume
OK the answer suggested by Sedrick is great but we have only used 3 Lines of Code
No Custom Alert just 30 Character and a bunch of consuming ha ha

@FXML
private void onType(){
    txtDescription.setOnKeyTyped(event ->{
    int maxCharacters = 30;
    if(txtDescription.getText().length() > maxCharacters)event.consume();
}); 

All right 7 lines if you count the FXML tab and the declaration and formatting

James_Duh
  • 1,321
  • 11
  • 31
  • 2
    no, wrong approach - Textformatter is what must be used (as Sedrick already suggested) – kleopatra Oct 20 '19 at 09:32
  • 2
    actually, your snippet is ... really _really_ __really__ bad .. it a) doesn't compile b) seems to register a new onKeyTyped handler each time the onType method is called c) doesn't even work - don't know why it is accepted, as it does exactly nothing to solve the problem (and would wonder if it did: the handler registered with setOnXX is guaranteed to be the _last_ of all registered with addEventHandler(XXType), that is _after_ the skin already handled the key by replacing the text - and even if that were not the case: sibling handler are _always_ notified, irrespective of the consumed) – kleopatra Oct 20 '19 at 10:24
  • @kleopatra Can you explain why you feel this code does not work? It did solve the issue we also tested the code Sedrick suggest it worked as well We would enjoy knowing about register a new onKeyTyped each time it is used how do we prove to our self this is happening it is always nice to learn new testing information – Vector Oct 21 '19 at 00:02
  • @Grendel I don't _feel_ it's not working, I _know_ it's not working because I wrote the [mcve] (your job) demonstrating that it doesn't ;) Anyway, without knowing your exact problem in your exact context (as you insist on never providing it ... *hammer), it might well be that the problem was elsewhere, different entirely, accidentally removed .. what ever. – kleopatra Oct 21 '19 at 09:06
  • @kleopatra We have posted a Minimal Code Example sorry no FXML file we will try to attach as File – Vector Oct 21 '19 at 20:19