I don't know what's wrong with my JTextPane
Hi, can anyone help me, cos i'm stuck in here.
i have JTextPane and save button in my application, every time i click on save button, it will get the whole text in JTextPane (using getText() method) and put it in String variable and then save it to a File...
In the first time i try to wrote" This is Java " in JTextPane and then save it to file named MyText.txt, when i open the file using notepad or anything else the result is what i expected nothings wrong...
and then i change " This is Java " to" This is me " and i save it again but when i open MyText.txt file with notepad or anything else again, the content of the file became like this" This is meva " of course the result is not what i want, what i want is " This is me ".
Whats wrong with this? why everytime i wrote some text but the length are smaller than the first time, it not change the whole text but it only change in the new text range?
illustration :
1) wrote " This is Java " and get the whole text (using getText()) then save it to MyText.txt
2) Change " This is Java " to " This is me " and then save it to the same file
3) The result in the file is " This is meva " not " This is me "
thanks a lot..
thanks a lot..
We have no idea what your code looks like, so how do you expect use to help?
Here is a working example:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=725863
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
Hi, sorry about that, this is my code :
public class myJTextPane extends JFrame implements ActionListener,DocumentListener
{
String contentTextPane = "";
JTextPane textPane = new JTextPane();
JButton btn = new JButton("Save");
File file;
RandomAccessFile ram;
public myJTextPane()
{
setSize(400,400);
setLayout(new BorderLayout());
textPane.getDocument().addDocumentListener(this);
btn.addActionListener(this);
getContentPane().add(textPane,BorderLayout.CENTER);
getContentPane().add(btn,BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn)
{
try
{
file = new File("MyText.txt");
ram = new RandomAccessFile(file,"rw");
ram.writeBytes(updateTextPane());
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
public String updateTextPane()
{
contentTextPane = textPane.getText();
return contentTextPane;
}
public void insertUpdate(DocumentEvent e)
{
updateTextPane();
}
public void removeUpdate(DocumentEvent e)
{
updateTextPane();
}
public void changedUpdate(DocumentEvent e)
{
updateTextPane();
}
public static void main(String[] args)
{
new myJTextPane();
}
}