Results 1 to 18 of 18
- 07-17-2007, 02:56 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 9
- Rep Power
- 0
problem trying to display the contents of a text file in JTextArea
hello.
I’m having difficulty trying to view the contents of a text file in a JTextArea. when i run the following program, i press the View Order button. but no data from the Order.txt file is being displayed in the text area (i've attached the .txt file to this message). how can i fix this problem?
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class ViewOrder extends JFrame{
JPanel pnlText, pnlBody, pnlFooter;
JButton btnViewOrder;
JButton btnReturnToOrderSystem;
JLabel jl;
JTextArea jta;
Container contentpane;
public ViewOrder(){
super("View Order");
contentpane = getContentPane();
contentpane.setLayout(new BorderLayout());
pnlText = new JPanel();
pnlBody = new JPanel();
pnlFooter = new JPanel();
jta = new JTextArea(250, 250);
jta.setFont(new Font("Serif", Font.PLAIN, 12));
// Set lineWrap and wrapStyleWord true for the text area
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
jta.setEditable(false);
// Create a scroll pane to hold the text area
JScrollPane jsp = new JScrollPane(jta);
// Set BorderLayout for the panel, add label and scrollpane
pnlBody.add(jsp, BorderLayout.CENTER);
jl = new JLabel("Text retrieved from file:");
btnViewOrder = new JButton("View Order");
btnReturnToOrderSystem = new JButton("Return to Order System Menu");
pnlText.add(jl);
pnlFooter.add(btnViewOrder);
pnlFooter.add(btnReturnToOrderSystem);
contentpane.add(pnlText,BorderLayout.NORTH);
contentpane.add(pnlBody,BorderLayout.CENTER);
contentpane.add(pnlFooter,BorderLayout.SOUTH);
setSize(500, 500);
setVisible(true);
btnViewOrder.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Read from file
try{
FileInputStream in = new FileInputStream("Order.txt");
BufferedReader iS = new BufferedReader(new InputStreamReader(in));
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
String il;
while ((il = iS.readLine()) != null){
out.println(il);
}
out.flush();
jta.setText(sw.toString());
in.close();
iS.close();
sw.close();
out.close();
}
catch(java.io.IOException ex){
System.out.println("Cannot read from file");
}
}
});
btnReturnToOrderSystem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
setVisible(false);
//OrderSystem os = new OrderSystem();
//os.setVisible(true);
}
});
}
public static void main(String[] args){
new ViewOrder();
}
}
- 04-01-2008, 02:07 AM #2
Member
- Join Date
- Feb 2008
- Location
- Oregon, USA
- Posts
- 49
- Rep Power
- 0
You took the hard way, all you need is a filename, FileReader, and to call the JTextArea's read method.
Java Code:File openAs = new File("Order.txt"); FileReader in = new FileReader(openAs); textArea.read(in, openAs.toString());
- 03-03-2009, 07:08 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 2
- Rep Power
- 0
Dude So many thanks !! :D this helps a lot
Last edited by chec69; 03-03-2009 at 03:09 PM.
- 03-04-2009, 02:59 AM #4
Member
- Join Date
- Feb 2008
- Location
- Oregon, USA
- Posts
- 49
- Rep Power
- 0
You're welcome. However, try not to post on threads that are a year old.
- 03-20-2009, 02:27 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 7
- Rep Power
- 0
-
then you have a bug somewhere. How about starting your own new thread on this and posting a small compilable program that tries to do what you want here and nothing more -- tries to read a file and display it in a JTextArea, and we'll see if we can help you. Before posting code though, please read the FAQ regarding use of code tags.
- 03-21-2009, 11:22 AM #7
Member
- Join Date
- Mar 2009
- Posts
- 7
- Rep Power
- 0
Display content into jTextarea.
Hello Friend,
I have pasted the sample program which i use.
The only markable difference is i am using huge size files to display. The size may be morethan 100 MB. Please help me out.:confused:
Java Code:import java.awt.BorderLayout; import java.awt.event.*; import java.awt.*; import javax.swing.*; import java.io.*; public class ViewOrder extends JFrame{ JPanel pnlText, pnlBody, pnlFooter; JButton btnViewOrder; JButton btnReturnToOrderSystem; JLabel jl; JTextArea jta; Container contentpane; public ViewOrder(){ super("View Order"); contentpane = getContentPane(); contentpane.setLayout(new BorderLayout()); pnlText = new JPanel(); pnlBody = new JPanel(); pnlFooter = new JPanel(); jta = new JTextArea(250, 250); jta.setFont(new Font("Serif", Font.PLAIN, 12)); // Set lineWrap and wrapStyleWord true for the text area jta.setLineWrap(true); jta.setWrapStyleWord(true); jta.setEditable(false); // Create a scroll pane to hold the text area JScrollPane jsp = new JScrollPane(jta); // Set BorderLayout for the panel, add label and scrollpane pnlBody.add(jsp, BorderLayout.CENTER); jl = new JLabel("Text retrieved from file:"); btnViewOrder = new JButton("View Order"); btnReturnToOrderSystem = new JButton("Return to Order System Menu"); pnlText.add(jl); pnlFooter.add(btnViewOrder); pnlFooter.add(btnReturnToOrderSystem); contentpane.add(pnlText,BorderLayout.NORTH); contentpane.add(pnlBody,BorderLayout.CENTER); contentpane.add(pnlFooter,BorderLayout.SOUTH); setSize(500, 500); setVisible(true); btnViewOrder.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //Read from file try{ File openAs = new File("D://mgc12ux_network.log"); // This file is huge in size. More than 100 MB FileReader in = new FileReader(openAs); jta.read(in, openAs.toString()); } catch(java.io.IOException ex){ System.out.println("Cannot read from file"); } } }); btnReturnToOrderSystem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ setVisible(false); //OrderSystem os = new OrderSystem(); //os.setVisible(true); } }); } public static void main(String[] args){ new ViewOrder(); } }
-
Friend, I will gladly try to help you if you follow our instructions. Again, start a new thread on this, and don't hijack somebody else's thread.
- 03-21-2009, 01:14 PM #9
Member
- Join Date
- Mar 2009
- Location
- Yemen - Taiz
- Posts
- 5
- Rep Power
- 0
hi all,
I have simple solution,
it is function read the content of any file,then display it on the JtextArea
or TextArea.
that's it :)Java Code:private void setTextContent(String Path) { try { java.io.DataInputStream o=new java.io.DataInputStream( new java.io.FileInputStream(Path)); String buf="",ur_text=""; while((buf=o.readLine())!=null) { ur_text+=buf; } // Now the ur_text ready to displayed on the text elemnet //this.jta.setText(ur_text); System.out.println(ur_text); } catch(java.io.IOException ioe) { ioe.printStackTrace(); } }
- 03-21-2009, 07:06 PM #10
Member
- Join Date
- Feb 2008
- Location
- Oregon, USA
- Posts
- 49
- Rep Power
- 0
Your function is not very good. In my opinion, if you have to edit a function to get what you want out of it, the function needs to be rewritten. What you might do differently is make the function return the "ur_text" string and set the JTextArea's text content with the returned string. I've written an equivalent function with different code, only two lines need to be changed in yours to make it return a string.
Java Code:private String getFileContents(String path) throws java.io.IOException { try { StringBuilder contents = new StringBuilder(), line; java.io.BufferedReader in = new java.io.BufferedReader(java.io.FileReader(path)); while ((line = in.readLine()) != null) { contents.append(line); // this is faster than "String += String" } } catch (FileNotFoundException fnfe) { System.err.println("The file \"" + path + "\" could not be found."); return null; } return contents.toString(); } JTextArea textArea = new JTextArea(); textArea.setText(getFileContents("files/textfile.txt"));
- 03-21-2009, 07:26 PM #11
Member
- Join Date
- Mar 2009
- Location
- Yemen - Taiz
- Posts
- 5
- Rep Power
- 0
friend Bluefox815,you have a pointJava Code:contents.append(line); // this is faster than "String += String"
and this v.good notice
---------------------
- 07-09-2009, 01:54 PM #12
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
Please Help...
Hi all I'm New , I'm face same problem when i open more than 10MB Text file but when I open 7MB Text file it's open fine . I dont understand please help me ....
Hello_Kolkata
- 07-09-2009, 02:35 PM #13
Hi,
Small changes in ur code.
rows and columns are more .
I have made it as 50,50 to make it scroll.
And then StringWriters getBuffer() method i called on that i called toString to get the text.Run this code and check once.
Java Code:import java.awt.BorderLayout; import java.awt.event.*; import java.awt.*; import javax.swing.*; import java.io.*; public class ViewOrder extends JFrame{ JPanel pnlText, pnlBody, pnlFooter; JButton btnViewOrder; JButton btnReturnToOrderSystem; JLabel jl; JTextArea jta; Container contentpane; public ViewOrder(){ super("View Order"); contentpane = getContentPane(); contentpane.setLayout(new BorderLayout()); pnlText = new JPanel(); pnlBody = new JPanel(); pnlFooter = new JPanel(); [B]jta = new JTextArea(50,50);[/B] jta.setFont(new Font("Serif", Font.PLAIN, 12)); // Set lineWrap and wrapStyleWord true for the text area jta.setLineWrap(true); jta.setWrapStyleWord(true); jta.setEditable(false); // Create a scroll pane to hold the text area JScrollPane jsp = new JScrollPane(jta); // Set BorderLayout for the panel, add label and scrollpane pnlBody.add(jsp, BorderLayout.CENTER); jl = new JLabel("Text retrieved from file:"); btnViewOrder = new JButton("View Order"); btnReturnToOrderSystem = new JButton("Return to Order System Menu"); pnlText.add(jl); pnlFooter.add(btnViewOrder); pnlFooter.add(btnReturnToOrderSystem); contentpane.add(pnlText,BorderLayout.NORTH); contentpane.add(pnlBody,BorderLayout.CENTER); contentpane.add(pnlFooter,BorderLayout.SOUTH); setSize(500, 500); setVisible(true); btnViewOrder.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //Read from file try{ FileInputStream in = new FileInputStream("Order.txt"); BufferedReader iS = new BufferedReader(new InputStreamReader(in)); StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw); String il; while ((il = iS.readLine()) != null){ out.println(il); } System.out.println(sw.getBuffer().toString()); [B]jta.setText(sw.getBuffer().toString()); [/B] out.flush(); in.close(); iS.close(); sw.close(); out.close(); } catch(java.io.IOException ex){ System.out.println("Cannot read from file"); } } }); btnReturnToOrderSystem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ setVisible(false); //OrderSystem os = new OrderSystem(); //os.setVisible(true); } }); } public static void main(String[] args){ new ViewOrder(); } }Ramya:cool:
- 07-09-2009, 05:06 PM #14
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
VERY old thread people... time to leave it.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 07-09-2009, 05:28 PM #15
Hi Singing Boyo,
Great catch Man!!!!!!!!!!!!!!!!!!
I have not checked the date of the thread.........
Tnx
D:D
-Regards
RamyaRamya:cool:
- 07-11-2009, 09:18 PM #16
However this thread is old,Can we display the document file contents into TextArea with out modifications.??
Mak
(Living @ Virtual World)
- 07-13-2009, 05:32 AM #17
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
Thank u for reply
when i view below 8mb file it's open fine but when I view more than 8mb ... nothing happened... I am waiting about 1/2 hour but still same ...
I am using NetBeans IDE 6.5.1
Please help me ....
-
Similar Threads
-
[SOLVED] Last line in JTextArea wont display
By Chris.Brown.SPE in forum Advanced JavaReplies: 5Last Post: 04-11-2008, 01:52 PM -
problems trying to view the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 1Last Post: 07-18-2007, 11:20 PM -
problem trying to view the contents of a text file in JTextArea
By warship in forum AWT / SwingReplies: 0Last Post: 07-17-2007, 03:30 PM -
viewing the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 0Last Post: 07-17-2007, 02:29 PM


LinkBack URL
About LinkBacks


Bookmarks