• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

HTMLDocument � no new line[ENTER] problem. Here is my code.

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

The code below (it was revised and some parts of it were eliminated for irrelevancy) has a frame with a JTextPane and save button to save the info in a word doc.

The problem is that new-line [ENTER] does not work properly. Meaning, if you press ENTER somewhere in the middle of a sentence (or at the end), the save document will not contain the new line (only the text you wrote)

Any idea what am I doing wrong?

MANY Thanks


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.text.*;

import javax.swing.text.html.*;
import java.io.*;

public class QuotingLetter implements ActionListener
{
protected JFrame frame;
protected JPanel mainPanel;
protected JFrame subFrame;

protected JButton exitButton = new JButton("Exit");
protected JButton saveButton = new JButton("Save As");

protected Vector v;

//protected JTextPane pane;
protected SimpleAttributeSet attr = new SimpleAttributeSet();

protected HTMLDocument document = new HTMLDocument();
protected JTextPane Editor = new JTextPane();
protected JTextPane SourceEditor = new JTextPane();
protected HTMLEditorKit editorKit = new HTMLEditorKit();
protected StyledEditorKit defaultEditor = new StyledEditorKit();
protected String userEmail="";
protected String fileName="";

public static void main(String args[])
{
JFrame frame = new JFrame();
QuotingLetter ql = new QuotingLetter(frame);
ql.setFrame();
}

public QuotingLetter(JFrame frame)
{
this.Editor.setText("");
this.Editor.setEditorKit(editorKit);
this.SourceEditor.setText(" ");
this.SourceEditor.setEditorKit(defaultEditor);
this.document.setPreservesUnknownTags(true);
this.Editor.setDocument(document);
this.frame=frame;
this.exitButton.addActionListener(this);
this.saveButton.addActionListener(this);
}

public void setFrame()
{
this.frame.setSize(700,650);
Toolkit kit = this.frame.getToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
Dimension windowSize = this.frame.getSize();
int windowWidth = windowSize.width;
int windowHeight = windowSize.height;
int upperLeftX = (screenWidth - windowWidth)/2;
int upperLeftY = (screenHeight - windowHeight)/2;
this.frame.setLocation(upperLeftX, upperLeftY);
this.frame.setTitle("testing HTML Editor");
this.setPanel();
}



public void setPanel()
{
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel buttonsPanel = new JPanel();
JPanel functionPanel = new JPanel(new BorderLayout());

JPanel emailPanel = new JPanel(new BorderLayout());
JPanel emailPanel1 = new JPanel(new BorderLayout());
JPanel emailPanel2 = new JPanel(new BorderLayout());
JPanel emailPanel3 = new JPanel(new BorderLayout());

//read info from file agentsEmail
FileReader f; // the actual file stream
BufferedReader r; // used to read the file line by line
String strEmail;

//setting the ta & mouse scroll
JScrollPane jsp = new JScrollPane(Editor);
JScrollBar jsb = jsp.getVerticalScrollBar();
jsb.setUnitIncrement(100);
functionPanel.add("Center", jsp);

//set font/color
JPanel iconPanel = new JPanel(new BorderLayout());
iconPanel.setBackground(Color.LIGHT_GRAY);
functionPanel.add("North", iconPanel);

JMenuBar menuBar = new JMenuBar();
menuBar.setBackground(Color.LIGHT_GRAY);
iconPanel.add("West", menuBar);


//add TEXT
this.writeEditorHTML1();

//set scroll pane in location 0 (at the begining)
Editor.setCaretPosition(0);

//general panels layout
mainPanel.add("South", buttonsPanel);
mainPanel.add("North", emailPanel);
mainPanel.add("Center", functionPanel);


//add buttons
buttonsPanel.add(exitButton);
buttonsPanel.add(saveButton);
//buttonsPanel.add(printButton);

this.frame.getContentPane().add(mainPanel);
this.frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.frame.show();
}

public void writeEditorHTML1()
{
String text="<font size=2 color=red><br>If you try to type enter HERE it will not result in the word doc :-( <br>"+


"<br><br>CHOICE(S) OF Mr. Thorn:"+



"<font size=4 color=black><pre> ____________________________________<br>" +
"<font size=4 color=black> Signature</pre><br><br>" +



"<font size=4 color=black>April June.<br>" +
"July August <br>" +
"ohhh......<br><br><br><br>Commercial / developers ";

try{

editorKit.insertHTML(document, document.getLength(), text, 0, 0, null);


}catch(Exception e)
{
JOptionPane.showMessageDialog(subFrame, "An error has occurred while writing the Letter X 8837 [QuotingLetter]", "error", JOptionPane.ERROR_MESSAGE);
}
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource()==exitButton)
{
this.frame.setVisible(false);
this.frame.dispose();
}


if (e.getSource()==saveButton)
{
subFrame = new JFrame();
FileDialog fd = new FileDialog(subFrame, "Save As",FileDialog.SAVE);

Toolkit kit2 = subFrame.getToolkit();
Dimension screenSize2 = kit2.getScreenSize();
int screenWidth2 = screenSize2.width;
int screenHeight2 = screenSize2.height;
Dimension windowSize2 = subFrame.getSize();
int windowWidth2 = windowSize2.width;
int windowHeight2 = windowSize2.height;
int upperLeftX = (screenWidth2 - windowWidth2)/2;
int upperLeftY = (screenHeight2 - windowHeight2)/2;
fd.setLocation(upperLeftX, upperLeftY);
fd.setFile (fileName+".doc"); // Set initial filename filter
fd.show();

if(fd.getFile() != null)
{
//2. Save Record
try{

String curFile = fd.getFile();

// The filename has a .
if(curFile.indexOf(".") != -1 )
{
curFile=curFile.substring(0, curFile.indexOf(".")-1);
}

String filename = fd.getDirectory() + curFile+".doc";

//saving into file
FileWriter writeDoc = new FileWriter(filename);
PrintWriter fileOutput = new PrintWriter(writeDoc);


String text=Editor.getText();

//take off the image from the jtextpane (editor)
String del="<img src=\"file:_Logo.jpg\">";

String[] result = text.split(del);
String saveText="";

for (int x=0; x<result.length; x++)
{
saveText+=result[x];
}
System.out.println(saveText);

fileOutput.print(saveText);

fileOutput.close();

Component c = SwingUtilities.getRoot((Component)e.getSource());
JFrame tmpDialog = (JFrame)c;
JOptionPane.showMessageDialog (tmpDialog, "The file has been successfully saved", "Saving completed", JOptionPane.INFORMATION_MESSAGE);



}catch(IOException ex)
{
Component c = SwingUtilities.getRoot((Component)e.getSource());
JFrame tmpDialog = (JFrame)c;
JOptionPane.showMessageDialog (tmpDialog, "An error has occurred while trying to write to file", "Error Message", JOptionPane.ERROR_MESSAGE);
}
}
}


}


}
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter,

Change all new line characters("\n") to break ("<br>"). In Word file it will look in the correct format.


.......
String text=Editor.getText();
text = text.replaceAll("\n","<br>");

//take off the image from the jtextpane (editor)
String del="<img src=\"file:_Logo.jpg\">";
....



Its working correctly in mysystem. please check it.
All The Best
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Sasi Kala,

your answer is good but for some reason, if the text is longer,

Instead of:

If you try to type enter HERE it will not result in the word doc :-(

place: (just duplicate it)
If you try to type enter HERE it will not result if you try to type enter ffff it will not result in the word doc :-(


you'll see that the end-result after placing your solution and typing [TESTING123] will be:

If you try to type enter
TESTING123 HERE it will not result f you try to type enter ffff it will not
result in the word doc :-(


THATS 2 ENTERS,

Any ideas??!?!
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic