473,395 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

textArea problem

35
I am displaying the contents of a textfile in a textArea. I want that when i edit this text in the textArea it is automatically saved in the textfile. I have wriitten this code, but the problem is that when i edit the text in the text Area it isn't saving it on the textfile. What do i have wrong? Thanks a lot.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.BorderLayout;
  2. import java.io.DataInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5.  
  6. import javax.swing.JDialog;
  7. import javax.swing.JTextArea;
  8. import java.awt.*;
  9. import javax.swing.*;
  10.  
  11.  
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import java.awt.event.ActionListener.*;
  15. import java.util.*;
  16. import java.io.*;
  17. import javax.swing.*;
  18. import java.util.Calendar;
  19. import java.awt.image.*;
  20. import javax.imageio.*;
  21.  
  22. import java.io.FileInputStream;
  23. import java.io.FileOutputStream;
  24. import java.io.DataInputStream;
  25. import java.io.DataOutputStream;
  26. import java.io.BufferedInputStream;
  27. import java.io.BufferedOutputStream;
  28. import java.io.IOException;
  29. import java.io.EOFException;
  30. import java.io.FileNotFoundException;
  31.  
  32.  
  33.  
  34. public class Rock_Notes_Editable {
  35.  
  36. private JTextArea textArea;
  37. public Rock_Notes_Editable() {
  38. super();
  39.  
  40. try{
  41. JDialog dialog = new JDialog();
  42. dialog.getContentPane().setLayout(new BorderLayout(10, 10));
  43.  
  44.  
  45.  
  46. Test4Rx imagePanel = new Test4Rx("rocks.gif");
  47.         dialog.add(imagePanel, "North");
  48.         dialog.add(new Test4Rx("rocks.gif"));
  49. //
  50. String record = null;
  51. File file = new File("rock.txt");
  52. FileInputStream fileInput = new FileInputStream(file);
  53. DataInputStream dis = new DataInputStream(fileInput);
  54.  
  55. String string = dis.readLine();
  56. textArea = new JTextArea();
  57. while(string != null){
  58. textArea.append(string + "\n");
  59. string = dis.readLine();
  60. textArea.setBackground(Color.pink);
  61. textArea.setFont(new Font("Times New Roman", Font.BOLD, 14));
  62. textArea.setEditable(true);
  63. textArea.setForeground(Color.blue);
  64. BufferedWriter w = new BufferedWriter(new FileWriter("rock.txt", true));
  65. try {
  66.  
  67.  textArea.write(w);
  68.  
  69. } finally {
  70.    w.close();
  71. }
  72.  
  73.  
  74. }
  75. dialog.getContentPane().add(BorderLayout.CENTER, textArea);
  76. dialog.setTitle("Geography Notes");
  77. dialog.setSize(800, 900);
  78.  
  79.  
  80.  
  81. dialog.setVisible(true);
  82.  
  83.  
  84. }catch (Exception e){
  85. e.printStackTrace();
  86. }
  87.  
  88. }
  89.  
  90. public static void main(String[] args){
  91. new Rock_Notes_Editable();
  92. }
  93. }
Feb 12 '08 #1
3 2296
r035198x
13,262 8TB
1.) Use FileReader and FileWriter to read/write character files.
2.) Where is the code that handles the saving of the edited contents part? You'll need to handle at least one event for that.
Feb 12 '08 #2
BigDaddyLH
1,216 Expert 1GB
I see you are using JTextComponent's write method (but in the wrong place). Why not simplify your code and use the corresponding read method too?

http://java.sun.com/javase/6/docs/ap...Component.html
Feb 12 '08 #3
saytri
35
Ok thanks a lot. :-)
Feb 13 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Csaba Gabor | last post by:
What I'd like to do is to be able to set the font of a textarea element to the same font that another element is using (say, for example, an <INPUT type=text ...> element, but if that's a no go,...
5
by: Jesper Rønn-Jensen | last post by:
I have a textarea that must be limited to 70 characters. No big deal -- at least so I thought. * Textarea must not exceed 70 characters * Exceeding content must be cut off * Must work on input by...
9
by: Paul Gorodyansky | last post by:
Hi, FAQ tells how to insert (or replace0 a text at a caret position in Mozilla (and same works for Opera 8+), for example, at http://www.faqts.com/knowledge_base/view.phtml/aid/1661 it talks...
3
by: arashamiri | last post by:
Hi, I wrote an auto resizing textarea: function myMax(anumber, another) { if (anumber > another) { return anumber; } return another; }
5
by: bwucke | last post by:
Is there any way to scroll textarea (to the bottom row) from Javascript in Opera? The textarea.scrollTop=textarea.scrollHeight+textarea.scrollTop; approach doesn't work, and there's so many...
5
by: Robine | last post by:
Hello all, looking for some help here! I've got a page on a website I'm developing that contains a textarea. The textarea will allow users of the site to enter a description of an item. When...
4
by: Keith Bentrup | last post by:
Hi all, I wrote a simple search function to find text in a textarea where not all the text is visible (ie. the text box displays 10 lines but there may be more than 1000 lines to search). I can...
16
by: Jen | last post by:
Hi. I have this problem that I think should be easy but have been struggling with this for days. I have a list based on a recordset from a database. This list consists of records meeting a certain...
5
by: a113n | last post by:
I have the following XSL code to handle textareas: <!-- Match TEXTAREA --> <xsl:template name="TEXTAREA" match="TEXTAREA"> <TEXTAREA ROWS="{@ROWS}" COLS="{@COLS}" NAME="{@NAME}"> <xsl:if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.