Question

i'm very confused but it seems this is just simple to you. [JAVA]

I have implemented an action performed from pressing a button, lets say its b1. Pressing b1 will result to browsing a file and output it to a text area. I implemented another button, say b2, for generating a tokens

This is what i want to do:

I want b2 to tokenize the text from the text area(text.getText()) wherein the contents of the text area is brought by b1.

Problem is that I can't tokenize the text content resulting from b1 in b2. It just gives blank result. I suspect that it can't get the text from b1 because the text is not global and it is only inside the e.getSource==b1 condition.

[EDIT] ok here:

if(e.getSource()==button){
        fileChooser= new JFileChooser();
        returnVal = fileChooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            try {
                // What to do with the file, e.g. display it in a TextArea
                text.read( new FileReader( file.getAbsolutePath() ), null );
            }catch(IOException ex){}
    }else{}
}


if(e.getSource()==b2){
        String[] token= text.getText().split(" ");
        System.out.println(token[0]);
}

When I put the tokenizer inside b1 condition, it works. however above doesn't.

It seems that when I do "text.getText()", it doesn't get the changes made in b1 where you choose a file and output it to the text. That's why I'm asking if there's a way to incorporate an action done by b1 to b2.

Was it helpful?

Solution

It works when I run it. What doesn't work though is trying to print the token[0]. When I print the whole thing, it works. What may be happening is that you're not allowing delimiters to coalesce just by .split(" ");. which may cause the first token to be nothing, if there are any spaces in the beginning of the file. I changed it to .split("[\\n\\s]+"); which allows for delimiters of one or more characters.

if (e.getSource() == b2) {
    String[] token = text.getText().split("[\\n\\s]+");
    for (String s : token) {
        System.out.println(s);
    }
}

Or maybe you just forgot to register the listener to the b2. ?

Here's the running example

import java.awt.BorderLayout;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class Test3 extends JFrame {

    JButton button;
    JButton b2;
    JTextArea text;
    JFileChooser fileChooser;
    int returnVal;

    public Test3() {
        text = new JTextArea(20, 40);
        button = new JButton("Read");
        button.addActionListener(new Listener());
        b2 = new JButton("Write");
        b2.addActionListener(new Listener());

        add(button, BorderLayout.NORTH);
        add(text, BorderLayout.CENTER);
        add(b2, BorderLayout.SOUTH);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Test3();
            }
        });

    }

    private class Listener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == button) {
                fileChooser = new JFileChooser();
                returnVal = fileChooser.showOpenDialog(Test3.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    try {
                        // What to do with the file, e.g. display it in a TextArea
                        text.read(new FileReader(file.getAbsolutePath()), null);
                    } catch (IOException ex) {
                    }
                } else {
                }
            }

            if (e.getSource() == b2) {
                String[] token = text.getText().split("[\\n\\s]+");
                for (String s : token) {
                    System.out.println(s);
                }
            }
        }
    }
}

As a side note. Never swallow you exceptions

} catch (IOException ex) {
}

Put something meaningful where you can see the exception like

} catch (IOException ex) {
     ex.printStackTrace();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top