Search code examples
javajlabeljtextarea

Word count for a scrollable JTextArea (scrollPane) word count


I have been trying to make a program where data inputted into a JTextArea, would then be counted and then displayed on a JLabel the number of words, after a button is clicked.

However the code I have tried to use, just shows the value of 1 everytime.

Any idea why? ` import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class SPanel extends JPanel {

public SPanel(){
    final TextAPanel textPanel = new TextAPanel();

    final JLabel outputLabel = new JLabel();
    JButton click = new JButton ("Click");
    click.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            String word = textPanel.inputBox.getText();

            System.out.println("Test: " +word);

        }

    });

 }
}

  import java.awt.Dimension;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;

 import javax.swing.JLabel;
 import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TextAPanel extends JPanel {
public JTextArea inputBox = new JTextArea(20,10);
public JScrollPane scrollPane = new JScrollPane(inputBox);
TextAreaPanel(){

    JLabel title = new JLabel("Please type in the box below:");

    inputBox.setLineWrap(true);

    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();

    gc.anchor = GridBagConstraints.NORTHWEST;
    gc.gridx = 0;
    gc.gridy = 0;

    add(title,gc);

    gc.gridx = 0;
    gc.gridy = 1;
    add(scrollPane, gc);


}

 }

Just to put it in context, my JTextArea is on a seperate panel / class to the panel containing the button and JLabel.

Every time I run the program and click the button, after inputting some words the value is always one even if the text box is empty.


Solution

  • Well, if you create a new TextAPanel and assign it to the textPanel variable - then when you try to get input you get nothing, because the newly created panel doesn't contain anything. If textPanel is a field in your class, you should just remove the textPanel = new TextAPanel(); line and it should work fine. If you get a NullPointerException, it means you forgot to initialize it earlier in the code, you should probably do it in the constructor.

    EDIT: OK, I made it work, the code is below. I have no idea how your program compiled, because in your TextAPanel class you had TextAreaPanel() method/constructor, which caused compilation error in my Eclipse. Anyway, I got this working, you'll need to complete it, but this should get you started:

    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    
    @SuppressWarnings("serial")
    public class SPanel extends JPanel {
    
        public SPanel() {
            final TextAPanel textPanel = new TextAPanel();
            this.add(textPanel);
    
            final JLabel outputLabel = new JLabel();
            JButton click = new JButton("Click");
            click.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    String word = textPanel.inputBox.getText();
    
                    System.out.println("Test: " + word);
                    System.out.println(word.split("\\s+").length);
                }
    
            });
    
            this.add(click);
    
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.add(new SPanel());
            f.setVisible(true);
        }
    
    }
    
    @SuppressWarnings("serial")
    class TextAPanel extends JPanel {
        public JTextArea inputBox = new JTextArea(20, 10);
        public JScrollPane scrollPane = new JScrollPane(inputBox);
    
        TextAPanel() {
    
            JLabel title = new JLabel("Please type in the box below:");
    
            inputBox.setLineWrap(true);
    
            setLayout(new GridBagLayout());
            GridBagConstraints gc = new GridBagConstraints();
    
            gc.anchor = GridBagConstraints.NORTHWEST;
            gc.gridx = 0;
            gc.gridy = 0;
    
            add(title, gc);
    
            gc.gridx = 0;
            gc.gridy = 1;
            add(scrollPane, gc);
    
        }
    
    }