Question

I created a class that extends another class, so that I can get the JTextArea from the main class. I'm not getting any errors for some reason. When I get the text of the JTextArea it is the text that I changed it to, but it isn't showing up on the JTextArea. I am trying to open a txt file and get the text from the file and put it on the JTextArea. Can anyone please make any suggestions why this might not work?

 public JMenuItem open(){
        open.addActionListener (new ActionListener (){
            public void actionPerformed (ActionEvent e){
                jta.setText("");
                fileChooser=new JFileChooser();
                fileChooser.setDialogTitle("Choose a file...");
                int response=fileChooser.showOpenDialog(null);

                if(response==JFileChooser.APPROVE_OPTION){
                    String filePath=fileChooser.getSelectedFile().toString();
                    try{
                        File userFile=new File(filePath);
                        Scanner scan=new Scanner(userFile);
                        String text="";
                        while(scan.hasNextLine()){
                            text+=scan.nextLine();
                            jta.replaceSelection(text);
                        }
                    }catch (IOException e1){
                        e1.printStackTrace();
                    }
                }

            }
        });
        open.setAccelerator(KeyStroke.getKeyStroke('O', KeyEvent.CTRL_DOWN_MASK));
        return open;
    }

This ^^^ is used like so:

public class FileMenu extends MenuBar{
    private JMenu file=new JMenu("File");
    JMenuItem New= new JMenuItem("New");
    JMenuItem open= new JMenuItem("Open file...");
    JMenuItem save= new JMenuItem("Save");
    JMenuItem saveAs= new JMenuItem("Save As...");
    JMenuItem close= new JMenuItem("Exit");
    public JFileChooser fileChooser;
    FileActions fileActions;

    public FileMenu(){

    }

    public JMenu menu(){
        fileActions=new FileActions();
        file.add(fileActions.newWindow());
        file.add(fileActions.open());
        file.addSeparator();
        file.add(save);
        file.add(saveAs);
        file.addSeparator();
        file.add(close);

        save.setAccelerator(KeyStroke.getKeyStroke('S', KeyEvent.CTRL_DOWN_MASK));
        saveAs.setAccelerator(KeyStroke.getKeyStroke('E', KeyEvent.CTRL_DOWN_MASK));
        close.setAccelerator(KeyStroke.getKeyStroke('Q', KeyEvent.CTRL_DOWN_MASK));

        mb.add(file);
        return file;
    }
}

This is where the JTextArea is created:

public class Frame {
    static final String title="Simple Text Editor";
    static final int width=650;
    static final int height=700;
    public JFrame frame=new JFrame(title);
    public JTextArea jta=new JTextArea();
    JScrollPane scrollPane=new JScrollPane(jta);

    public void create(){
        //JFrame
        frame.setSize(width,height);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        // JMenuBar
        MenuBar menuBar=new MenuBar();
        frame.add(menuBar.menuBar(), BorderLayout.NORTH);

        jta.setText("Well this works...");
        // JTextArea
        jta.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        frame.add(scrollPane, BorderLayout.CENTER);
    }
}
Was it helpful?

Solution

It seems you want to populate the text area with the content of a text file. But to do that you read every line of the text file and do

jta.replaceSelection(text);

But as its name and javadoc indicates, replaceSelection() replaces the text selected by the user in the text area. What you simply want is to clear the text area and append the text to the text area:

jta.setText(""); // clear
while (scan.hasNextLine()){
    String line = scan.nextLine();
    jta.append(line);
    jta.append("\n");
}

Or, easier:

jta.read(new FileReader(userFile, null);

which will read everything from the file for you and populate the text area with its content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top