0

having a little trouble with my code. I've put in a file selector/reader but I have no idea how to make it display in the leftpane within my GUI. Ive tried a few things to no success and cannot figure it out. If someone could provide a solution and an explanation so that I can understand what I've done wrong/not done it would be highly appreciated.

Also my code is a bit messy and with the way I've done my methods its made doing certain things annoying and inconvenient if anyone has troops for the future in organizing methods and code it would be highly appreciated.

Here is my code.

public class JavaAssignment {

    public static JMenuBar setupMenu() { // method modifiers where mising here

        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);

        // 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 ) {

                            final JTextArea input = new JTextArea(nextLine);
                            nextLine = reader.readLine();
                        }

                        reader.close();

                    } catch (IOException e1) {

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


                }
            }});

        return menuBar;
    }

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

        window window = new window();


    }

    public static class window extends JFrame {

        public window() throws FileNotFoundException {

            JScrollPane leftScrollPane = new JScrollPane();
            JPanel rightPane = new JPanel();
            JSplitPane splitPane;

            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(setupMenu());
            splitPane.setPreferredSize(new Dimension(400, 200));
            splitPane.setLeftComponent(leftScrollPane);
            splitPane.setRightComponent(rightPane);
            this.add(splitPane);
            this.setSize(1280, 720);
        }
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    You're creating a new non-displayed JTextArea repeatedly within the while loop, every time a line is read: `final JTextArea input = new JTextArea(nextLine);`. Ask yourself -- does this make sense? You must learn to do mental walk-throughs of your code, asking yourself the very same question as you do it -- also known as "[Rubber Duck Debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging)". – Hovercraft Full Of Eels Nov 05 '17 at 18:45
  • 1
    Possible duplicate of [Most efficient way to read text file and dump content into JTextArea](https://stackoverflow.com/questions/13186818/most-efficient-way-to-read-text-file-and-dump-content-into-jtextarea) – Hovercraft Full Of Eels Nov 05 '17 at 18:47
  • 1
    Create your JTextArea **once**, add it to the GUI on GUI creation, and then use the link above to read the file into that same JTextArea. – Hovercraft Full Of Eels Nov 05 '17 at 18:48
  • 1
    Other issues: you're grossly over-using the static modifier here as well as using local variables where instance fields should be used. If I were your instructor, I'd tell you to scrap this code and start over, creating object-oriented compliant classes that have *state* (instance fields) and *behaviors* (instance methods). Otherwise without fields, how can one part of your program interact with another? Consider checking the tutorials that discuss OOP concepts as it applies to Java programming. Also there are some decent books on this. – Hovercraft Full Of Eels Nov 05 '17 at 18:52
  • 1
    Other suggestion (if you're listening): don't try to solve everything all at once. First try to create a simple program that does nothing but simply reads a text file into a JTextArea. Once you've solved that, then work on integrating it into the whole -- divide and conquer. – Hovercraft Full Of Eels Nov 05 '17 at 19:10
  • Yeah I am listening just haven't had the chance to reply due to the fact I'm trying to fix this mess, as much as it has crossed my mind a few times to restart this due to the mess I cannot because my assignment is due in a few hours. – Daniel Siwiec Nov 05 '17 at 19:23
  • 1
    You don't really have a choice because as your program is currently structure, there is no way that it can possibly work. It needs to be rewired, you need to use fields. Period. End of story. No choice here. – Hovercraft Full Of Eels Nov 05 '17 at 19:33
  • Right since I don't have a choice I'll have to restart it, could you please give me some tips on how to structure it? – Daniel Siwiec Nov 05 '17 at 20:05
  • `could you please give me some tips on how to structure it?` Get rid of all the static code. Start by reading the section on [How to Use File Choosers](https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html). Follow the basic structure of the demo. The demo just adds the name of the file to the text area. But there is no reason you can't change that code to actually load the file into the text area by using the read(...) method of the JTextArea. – camickr Nov 05 '17 at 20:08
  • 1) Create a private JTextArea field. 2) Do most *everything* in the instance (e.g., non-static) world. The only method that should be static, the only *thing* that should be static (other than constants) here is the main method. 3) Wire the GUI together in your program's constructor. ... – Hovercraft Full Of Eels Nov 05 '17 at 20:08
  • Thank you very much for your time and help. – Daniel Siwiec Nov 05 '17 at 20:51

0 Answers0