Hi!, I have created a user inteface named "helpGUI". This user interface contains a JTextArea.
I also have created a text file named "help.txt".
I need to show the "help.txt" file in the JTextArea.

As I'm new to JAVA I have no idea how to do this. Given below is the code that i have done upto now.

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.border.*;

class helpGUI
{
	JFrame frame;
	JPanel panel1, panel2, panel3;
	JTextArea txtHelp;
	JScrollPane scrollHelp;
	JLabel lblImg;
	JButton btnOk;

	public helpGUI()
 	{
		java.io.BufferedReader reader = new java.io.BufferedReader(new FileReader("help.txt"));
		String temp = null;
		StringBuffer message = new StringBuffer();
		String newLine = System.getProperty("line.separator"); 

		//Scroll Panel
	  	panel1 = new JPanel();
		panel1.setLayout(new GridBagLayout());

		//Options Panel
  		panel2 = new JPanel();
		panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));

		//Image Panel
		panel3 = new JPanel();
		panel3.setLayout(new GridBagLayout());
		lblImg = new JLabel(new ImageIcon("helpIMAGE.jpg"));
		lblImg.setHorizontalAlignment(JLabel.CENTER);
		panel3.add(lblImg);

		//Help ScrollPane
		txtHelp = new JTextArea(30, 50);
		txtHelp.setLineWrap(true);
		txtHelp.setText("QuickData Management System");
		txtHelp.setEditable(false); 
		scrollHelp = new JScrollPane(txtHelp);
		scrollHelp.setEnabled(false); 
		panel1.add(scrollHelp, getConstraints(0, 4, 2, 1, GridBagConstraints.EAST));

		//Button - Ok
		btnOk = new JButton("Ok");
		panel1.add(btnOk, getConstraints(0, 5, 2, 1, GridBagConstraints.EAST));
		btnOk.addActionListener(new okListener());
		
  		frame.getContentPane().add(panel1);
  		frame.getContentPane().add(panel2);
		frame.getContentPane().add(panel3);
		frame.getContentPane().add(BorderLayout.CENTER, panel1);
  		frame.getContentPane().add(BorderLayout.SOUTH, panel2);
		frame.getContentPane().add(BorderLayout.NORTH, panel3);
  		frame.pack();
  		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  		frame.setVisible(true);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
	}

	private GridBagConstraints getConstraints(int gridx,int gridy, int gridwidth,int gridheight,int anchor)
	{
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5,5,5,5);
		c.gridx = gridx;
		c.gridy = gridy;
		c.gridwidth = gridwidth;
		c.gridheight = gridheight;
		c.anchor = GridBagConstraints.WEST;

		return c;
	}

	class okListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ev)
		{
			frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
			frame.setVisible(false);
		}
	}

	public static void main(String args[])
	{
		helpGUI st = new helpGUI();
	}
}

Please help me with this problem.

wait, ur using a netbeans IDE? i cant see a file reader on your code. you must declare a file reader. i can't remember the code cause im not on my pc. www.w3school.org try to get some help there.

wait, ur using a netbeans IDE? i cant see a file reader on your code. you must declare a file reader. i can't remember the code cause im not on my pc. www.w3school.org try to get some help there.

No dude I'm not using NetBeans IDE, I just code it through notepad and compile it and run through the command pormpt.

wait, ur using a netbeans IDE? i cant see a file reader on your code. you must declare a file reader. i can't remember the code cause im not on my pc. www.w3school.org try to get some help there.

If you looked code properly you will find at the start attempt to read file. Secondly I do not see any Java tutorials on w3schools I guess you mistaken Java for Java Script


@rizillion here is some update on your code

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.border.*;

class HelpGUI
{
	JFrame frame = new JFrame("Help GUI"); // YOU FORGOT TO INSTATIATE JFRAME
	JPanel panel1, panel2, panel3;
	JTextArea txtHelp;
	JScrollPane scrollHelp;
	JLabel lblImg;
	JButton btnOk;

	public HelpGUI()
 	{
		/*java.io.BufferedReader reader = new java.io.BufferedReader(new FileReader("help.txt"));
		String temp = null;
		StringBuffer message = new StringBuffer();
		String newLine = System.getProperty("line.separator");*/

		//Scroll Panel
	  	panel1 = new JPanel();
		panel1.setLayout(new GridBagLayout());

		//Options Panel
  		panel2 = new JPanel();
		panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));

		//Image Panel
		panel3 = new JPanel();
		panel3.setLayout(new GridBagLayout());
		lblImg = new JLabel(new ImageIcon("helpIMAGE.jpg"));
		lblImg.setHorizontalAlignment(JLabel.CENTER);
		panel3.add(lblImg);

		//Help ScrollPane
		txtHelp = new JTextArea(30, 50);
		txtHelp.setLineWrap(true);
		//txtHelp.setText("QuickData Management System");
		try {
			//use buffering, reading one line at a time
			//FileReader always assumes default encoding is OK!
			BufferedReader input =  new BufferedReader(new FileReader("food.txt"));
			try {
				String line = null; //not declared within while loop
				
				while (( line = input.readLine()) != null){
					txtHelp.append(line+"\n");
				}
			}
			finally {
				input.close();
			}
		}
		catch (IOException ex){
			ex.printStackTrace();
		}

		txtHelp.setEditable(false); 
		scrollHelp = new JScrollPane(txtHelp);
		scrollHelp.setEnabled(false); 
		panel1.add(scrollHelp, getConstraints(0, 4, 2, 1, GridBagConstraints.EAST));

		//Button - Ok
		btnOk = new JButton("Ok");
		panel1.add(btnOk, getConstraints(0, 5, 2, 1, GridBagConstraints.EAST));
		btnOk.addActionListener(new okListener());		
		
  		frame.getContentPane().add(panel1);
  		frame.getContentPane().add(panel2);
		frame.getContentPane().add(panel3);
		frame.getContentPane().add(BorderLayout.CENTER, panel1);
  		frame.getContentPane().add(BorderLayout.SOUTH, panel2);
		frame.getContentPane().add(BorderLayout.NORTH, panel3);
  		frame.pack();
  		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  		frame.setVisible(true);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
	}

	private GridBagConstraints getConstraints(int gridx,int gridy, int gridwidth,int gridheight,int anchor)
	{
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5,5,5,5);
		c.gridx = gridx;
		c.gridy = gridy;
		c.gridwidth = gridwidth;
		c.gridheight = gridheight;
		c.anchor = GridBagConstraints.WEST;

		return c;
	}

	class okListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ev)
		{
			frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
			frame.setVisible(false);
		}
	}

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

The above is just quick dirty solution. Normally you would one class only for GUI and closes action related to GUI. Anything else will be in separate file and you will only call methods. Have look at the code and if you do not understand something just ask.

If you looked code properly you will find at the start attempt to read file. Secondly I do not see any Java tutorials on w3schools I guess you mistaken Java for Java Script


@rizillion here is some update on your code

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.border.*;

class HelpGUI
{
	JFrame frame = new JFrame("Help GUI"); // YOU FORGOT TO INSTATIATE JFRAME
	JPanel panel1, panel2, panel3;
	JTextArea txtHelp;
	JScrollPane scrollHelp;
	JLabel lblImg;
	JButton btnOk;

	public HelpGUI()
 	{
		/*java.io.BufferedReader reader = new java.io.BufferedReader(new FileReader("help.txt"));
		String temp = null;
		StringBuffer message = new StringBuffer();
		String newLine = System.getProperty("line.separator");*/

		//Scroll Panel
	  	panel1 = new JPanel();
		panel1.setLayout(new GridBagLayout());

		//Options Panel
  		panel2 = new JPanel();
		panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));

		//Image Panel
		panel3 = new JPanel();
		panel3.setLayout(new GridBagLayout());
		lblImg = new JLabel(new ImageIcon("helpIMAGE.jpg"));
		lblImg.setHorizontalAlignment(JLabel.CENTER);
		panel3.add(lblImg);

		//Help ScrollPane
		txtHelp = new JTextArea(30, 50);
		txtHelp.setLineWrap(true);
		//txtHelp.setText("QuickData Management System");
		try {
			//use buffering, reading one line at a time
			//FileReader always assumes default encoding is OK!
			BufferedReader input =  new BufferedReader(new FileReader("food.txt"));
			try {
				String line = null; //not declared within while loop
				
				while (( line = input.readLine()) != null){
					txtHelp.append(line+"\n");
				}
			}
			finally {
				input.close();
			}
		}
		catch (IOException ex){
			ex.printStackTrace();
		}

		txtHelp.setEditable(false); 
		scrollHelp = new JScrollPane(txtHelp);
		scrollHelp.setEnabled(false); 
		panel1.add(scrollHelp, getConstraints(0, 4, 2, 1, GridBagConstraints.EAST));

		//Button - Ok
		btnOk = new JButton("Ok");
		panel1.add(btnOk, getConstraints(0, 5, 2, 1, GridBagConstraints.EAST));
		btnOk.addActionListener(new okListener());		
		
  		frame.getContentPane().add(panel1);
  		frame.getContentPane().add(panel2);
		frame.getContentPane().add(panel3);
		frame.getContentPane().add(BorderLayout.CENTER, panel1);
  		frame.getContentPane().add(BorderLayout.SOUTH, panel2);
		frame.getContentPane().add(BorderLayout.NORTH, panel3);
  		frame.pack();
  		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  		frame.setVisible(true);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
	}

	private GridBagConstraints getConstraints(int gridx,int gridy, int gridwidth,int gridheight,int anchor)
	{
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5,5,5,5);
		c.gridx = gridx;
		c.gridy = gridy;
		c.gridwidth = gridwidth;
		c.gridheight = gridheight;
		c.anchor = GridBagConstraints.WEST;

		return c;
	}

	class okListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ev)
		{
			frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
			frame.setVisible(false);
		}
	}

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

The above is just quick dirty solution. Normally you would one class only for GUI and closes action related to GUI. Anything else will be in separate file and you will only call methods. Have look at the code and if you do not understand something just ask.

Thankyou for replying to thread dude and its working fine. Something I dont understand in the code is:

SwingUtilities.invokeLater(new Runnable() public void run()

what does this line do?

Anyway dude, Thankyou again for helping me.

That is just my preferred way to start up Swing application, plus it make more thread-safe (have look here for little info)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.