JTextArea in a SplitPane not displaying text when file is loaded in

having a problem with the code I am writing for homework.

The function I need it to do is simple when I load a txt document in I want the left panel in my GUI to display the text that is in the document.

Ive tried a few things tweaking the JTextField and using JTextArea but whenever I load the sample file I always just end up with a small empty box where I can type stuff in.

enter image description here

If anyone has a fix/solution for this and could point me in what I was doing wrong it would be highly appreciated.

Here is my code.

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.*;

public class JavaAssingnment {


public static void main(String[] args) throws FileNotFoundException {

    window window = new window();
    String reader;



}

public static class window extends JFrame {



    public window() throws FileNotFoundException {


            JMenuBar menuBar = new JMenuBar(); // menubar
            JMenu menu1 = new JMenu("File"); //menu 
            menuBar.add(menu1); // add menu to gui
            JMenu menu2 = new JMenu("Help");
            menuBar.add(menu2);
            JMenuItem menuItem1 = new JMenuItem("Load File", KeyEvent.VK_1); // create drop down menu
            JMenuItem menuItem2 = new JMenuItem("Save File", KeyEvent.VK_1);
            JMenuItem menuItem3 = new JMenuItem("Exit", KeyEvent.VK_1);     
            JMenuItem menuItem4 = new JMenuItem("About", KeyEvent.VK_1);

            menu1.add(menuItem1); // adds drop down menus to gui
            menu1.add(menuItem2);
            menu1.add(menuItem3);
            menu2.add(menuItem4);

            JPanel leftScrollPane = new JPanel();
            JPanel rightPane = new JPanel();
            JSplitPane splitPane;
            JTextArea input = new JTextArea();
            input.setVisible(true);
            input.setSize(300,300);
            leftScrollPane.add(input);


            this.setVisible(true);
            this.setSize(400, 400);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            splitPane = new JSplitPane();
            splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
            splitPane.setDividerSize(10);
            splitPane.setDividerLocation(100);
            splitPane.setLeftComponent(leftScrollPane);
            splitPane.setRightComponent(rightPane);
            splitPane.setOneTouchExpandable(true);
            splitPane.setDividerLocation(600);

            Dimension minimumSize = new Dimension(100, 50);

            leftScrollPane.setSize(400, 400);

            this.setJMenuBar(menuBar);
            splitPane.setPreferredSize(new Dimension(400, 200));
            splitPane.setLeftComponent(leftScrollPane);
            splitPane.setRightComponent(rightPane);
            this.add(splitPane);
            this.setSize(1280, 720);

            // execute code when selected
            menuItem1.addActionListener(new ActionListener() {


                @Override
                public void actionPerformed(ActionEvent e) {

                    final JFileChooser fc = new JFileChooser();

                    // you can set the directory with the setCurrentDirectory method.
                    int returnVal = fc.showOpenDialog(null);

                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        // User has selected to open the file.

                        File file = fc.getSelectedFile();

                        try {
                            // Open the selected file
                            BufferedReader reader = new BufferedReader(new FileReader(file));

                            // Output the contents to the console.
                            String nextLine = reader.readLine();

                            while ( nextLine != null ) {

                                nextLine = reader.readLine();
                                input.setText(nextLine);

                            }

                            reader.close();

                        } catch (IOException e1) {

                            System.err.println("Error while reading the file");
                        } 


                    }
                }});

    }
}
}

3 answers

  • answered 2018-04-14 15:41 Andrew Thompson

            JTextArea input = new JTextArea();
    

    This does not suggest a size, while..

            JTextArea input = new JTextArea(20,40);
    

    Suggests a size in columns and rows.

    input.setSize(300,300);
    

    That does not help in the slightest, since the layout will ignore the size in favor of the preferred size. But don't go messing with that either, since the numbers are simply a guess, whereas setting the rows and columns allows the runtime to determine how big it should be.

    Further:

    JPanel leftScrollPane = new JPanel();
    

    Will end up with a default FlowLayout which means the components will appear at their preferred size, and will not be stretched to the size of the panel.

    This will 'fix' that:

    JPanel leftScrollPane = new JPanel(new GridLayout()); // will stretch content to fill
    

  • answered 2018-04-14 15:41 Emre Dalkiran

    In addition to what Andrew Thompson said, there is an error in your while loop.

    String nextLine = reader.readLine();
    

    You read the first line here.

                            while ( nextLine != null ) {
    

    If the first line is not null

                                nextLine = reader.readLine();
    

    You read the second line and put it to screen.

                                input.setText(nextLine);
                            }
    

    You do not check for read line in your control statement for while loop.

    Instead try it like this:

                            String nextLine;
                            String temp ="";
    
                            while ( ( nextLine = reader.readLine()) != null ) {
                                temp += nextLine;
                                input.setText(temp);
    

    If you do not add each line you read before to newly read line, you will only end up with the last line of the text file.

  • answered 2018-04-14 15:41 camickr

    splitPane.setLeftComponent(leftScrollPane);
    splitPane.setRightComponent(rightPane);
    splitPane.setOneTouchExpandable(true);
    splitPane.setDividerLocation(600);
    
    Dimension minimumSize = new Dimension(100, 50);
    
    leftScrollPane.setSize(400, 400);
    
    this.setJMenuBar(menuBar);
    splitPane.setPreferredSize(new Dimension(400, 200));
    splitPane.setLeftComponent(leftScrollPane); // ???
    splitPane.setRightComponent(rightPane); // ??
    

    Why are you adding the components to the splitpane twice?

    JPanel leftScrollPane = new JPanel();
    JPanel rightPane = new JPanel();
    JSplitPane splitPane;
    JTextArea input = new JTextArea();
    input.setVisible(true);
    input.setSize(300,300);
    leftScrollPane.add(input);
    

    Why are you adding the text area to a JPanel that you call a "scroll pane". Typically you would add the text area to an actual JScrollPane so that scrollbar can be used when necessary. There is no need for the panel. Also:

    1. Swing components are visible by default so there is no need for setVisible(true);
    2. The layout manager (or in this case the JSplitPane) will determine the size of each compnents so the setSize(...) does nothing. You should NOT bet trying to set the size of a component.

    So better code would be something like:

    JTextArea textArea = new JTextArea(5,20);
    JScrollPane scrollPane = new JScrollPane( textArea );
    splitPane.setLeftComponent( scrollPane );
    

    whenever I load the sample file

    Don't read the file in a loop. Use the read(...) method of the JTextArea:

    textArea.read(reader, null);