0

I tried various methods to solve the problem, including creating an object of the method FileOpener of public void FileOpener, but various errors seem to pop-up. Someone please provide a solution which does not have too long or complicated codes. The menubar works, and the ActionListener is being used to for the JMenuItem's clicks. A label will be added to a panel, and is supposed to display the contents of the text file, but does not work. the load menu item, and the exit menu item works perfectly.

import java.util.Scanner;
import java.io.*;
import javax.swing.*;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

public class GraphDisplay
{

    public GraphDisplay()
    {
        gui();
        FileOpener();
    }

    public void gui()
    {
        JFrame jf = new JFrame("Ethnic diversity Graph");
        //Creates a constructor for JFrame.

        FileOpener();

        JMenuBar bar = new JMenuBar();
        jf.setJMenuBar(bar);

        JMenu file = new JMenu("File");
            bar.add(file);

        JMenuItem load = new JMenuItem("Load");
            file.add(load);
            load.addActionListener(new LoadClick());
        JMenuItem exit = new JMenuItem("Exit");
            file.add(exit);
            exit.addActionListener(new ExitClick());

        JMenu help = new JMenu("Help");
            bar.add(help);

        JMenuItem guide = new JMenuItem("User Guide");
            help.add(guide);

        JMenuItem about = new  JMenuItem("About");
            help.add(about);
            about.addActionListener(new AboutClick());

        JPanel jpOne = new JPanel();

        JLabel output = new JLabel();

        jf.add(jpOne);
        jpOne.add(output);

        jf.setVisible(true);
        jf.setSize(800, 600);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }//void gui end

    public void FileOpener()
    {
        JFileChooser chooser = new JFileChooser();

        int status = chooser.showOpenDialog(null);  

        try{
        if (status != JFileChooser.APPROVE_OPTION){

             // Error
        }else
          {
             File file = chooser.getSelectedFile();
             Scanner scan = new Scanner(file);



             String info = "";
             while (scan.hasNext()){
                info += scan.nextLine() + "\n";
             }


          }
    }catch(Exception e){
    }
    }



    public static void main(String[] args)
    {
        new GraphDisplay();

    }//main end

    class ExitClick implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }

    }//ExitClick end

    class AboutClick implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {
            JOptionPane.showMessageDialog(null, "This program is used to show graph from user's text file", "About", JOptionPane.INFORMATION_MESSAGE);
        }

    }//AboutClick end

    class LoadClick implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {
            FileOpener();
        }

    }//AboutClick end

}//class end

Thank You

Prasun
  • 5
  • 2
  • *"Someone please provide a solution which does not have too long or complicated codes"* - If wishes were horses. Problem #1 - `JLabel` really isn't a good choice for this kind of operation, a `JTextArea` or `JList` would be better choices. For examples of reading a file into a `JTexaArea`, you can have a look at [this](https://stackoverflow.com/questions/6356086/how-to-read-text-file-in-jtextarea-in-java-swing) and [this](https://stackoverflow.com/questions/13186818/most-efficient-way-to-read-text-file-and-dump-content-into-jtextarea) – MadProgrammer Nov 20 '17 at 08:44
  • Problem #2 - Your `ActionListener` needs some way to reference to the panel. This is, yet, another not so uncommon problem. You could pass a reference of the panel to the `ActionListener`, or, since you're using inner classes anyway, simply make the panel an instance field of the `GraphDisplay` – MadProgrammer Nov 20 '17 at 08:46
  • Inside the function FileOpener() , you have read the contents in a String called 'info' and you haven't set it to any component. Also my suggestion is to use StringBuffer or StringBuilder instead of String. Also as suggested in the prev comments, use JTextArea instead of JLabel – Nandha Nov 20 '17 at 08:47

0 Answers0