Search code examples
javaswingarraylistjtextarea

Java - Can't get getText in jTextArea to work properly


I have a seperate JFrame where there is a text box (jTextArea) that takes numbers as inputs, each separated with a new line. Upon closing the JFrame with the text box, the data is supposed to be stored in an ArrayList of integers. The ArrayList is checked when clicking a button in the main JFrame and errors are logged if they happen.

The code for the JFrame with the jTextArea looks like this:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        boolean success = false;
        try{
            selectedTime = Long.parseLong(jTextField1.getText());
            if(selectedTime >= 10000){
                success = true;
                if(!jTextArea1.equals("") && !jTextArea1.equals(null)){
                    try{
                        for(int i = 0; i < jTextArea1.getLineCount(); i++){
                            n = Integer.parseInt(jTextArea1.getText(jTextArea1.getLineStartOffset(i),jTextArea1.getLineEndOffset(i)));
                            if(n <= 172){
                                worldsChosen.add(n);
                            }
                        }
                    }catch(Exception e){
                        errorsHappened = true;
                    }
                }
            }else{
                javax.swing.JOptionPane.showMessageDialog(null,"The specified time was not above or equal to 10000 ms. Please try again.");
                success = false;
            }
        }catch(Exception e){
            javax.swing.JOptionPane.showMessageDialog(null,"The specified time was not set in numbers exclusively. Please try again.");
            success = false;
        }
        if(success){
            gui.hideWorlds();
        }
    }

Note: it also checks whether a text box has a number input equal to or above 10000 (this works).

The code for the main JFrame:

 if(jCheckBox5.isSelected()){
            checkWorld = true;
            if(!worldsChosen.isEmpty()){
                changeWorlds = true;
            }else{
                log("You've selected the option for automatic world switching,");
                log("but all of your inputs weren't formatted correctly.");
                errorsHappened = true;
            }
        }else{
            errorsHappened = false;
        }
if(errorsHappened == true){
                log("One or multiple worlds weren't added due to incorrect formatting.");
                log("Retry to make script automatically change worlds.");               
            }

Whenever I run the script with the check box selected and something formatted correctly in the text area (like this):

1
2
3
4
5

etc.

It outputs all of the log messages (as if the check box had been selected but none of the inputs were formatted correctly).

I've tried to the best of my knowledge to fix this, but I just can't see how it messes up.

Any help appreciated :).

Mike Haye.


Solution

  • Read the api doc of getText(int, int): the second argument is not an offset. It's a length.

    Side note 1: it should probably be easier to get all the text as a single string and split on newline chars, and the parse every string into an integer.

    Side note 2: The test if (jTextArea1.equals("")) can't succeed. A JTextArea instance will never be equals to a String instance.