访问/更改 JEditorPane 的 html 加载元素HTMLEditorKit 与 Unicode 的问题 (Java)
这将是一个很长的问题,所以请耐心等待 :)
我的应用程序
我正在开发一个 Java(带有 JFrame GUI)桌面应用程序,它执行以下操作:
- 扫描 (.txt) 文件。
- 从这些中解析一些数字 文件,执行一些计算 他们并最终存储结果 在字符串变量中。
- 以特殊的方式输出这些数字 (表)格式。 (注:格式 包括一些 Unicode(阿拉伯语) 人物。)
问题
前两部分进展顺利。然而,当我来到第三部分(格式化输出)时,我不知道如何显示这种特殊格式,所以,
- 在 Java 中显示特殊格式化输出(表)的最佳方式是什么?< /strong>
注意:格式化程序< /a> 不会有帮助因为它没有对表格的适当支持。
解决方案一:
我做了研究,发现我可以使用 JEditorPane,因为它可以显示特殊格式,例如“html”。因此,我决定创建一个具有所需(表格)格式的“html”页面,然后在 [JEditorPane][4] 上显示此页面。我这样做了,一切都很顺利,直到我想将一些 html 元素的值更改为这些 (.txt) 文件中解析的数字。
- 我怎样才能访问 html 元素(例如
)并更改 它的值?
请注意,(.html) 是使用 setPage(url)
加载到 JEditorPane 中的。
Unicode 字符显示正确,但我无法更改某些元素值(例如,我想将 000,000,000
的值更改为 ; MainController.getCurrentTotalPayment()
解决方案二:
我找到了一个解决方法,其中涉及使用HTMLDocument 和 HTMLEditorKit,这样我就可以使用 HTMLEditorKit 从头开始创建 (.html) 并使用 kit.insertHTML
将其显示在 JEditorPane 上,
我已经使用上述方法成功添加了内容,而且我也成功了。能够从 (.txt) 文件添加已解析的数字,因为我将它们存储在我的 (MainController) 类中。不幸的是,Unicode 阿拉伯字符未正确显示
- 如何显示这些 Unicode 。 字符正确吗?
所以第一个解决方案缺乏对 html 元素的访问,而第二个解决方案缺乏 Unicode 支持!
我的同事建议我使用 JSP html 文档中的代码可以访问我的 MainController.java 类。因此,将页面加载到 JEditorPane 中,其中 html 元素已更改。没有JSP的帮助就没有办法做到这一点吗?
其他一些人建议使用 JTidy 但没有办法在 Java 的 JDK 中执行此操作?
我对所有可能的解决方案持开放态度。请帮忙。
我的代码:一些代码内容被省略,因为它们不相关
MyFormattedOuputSolutionOne.java:MyFormattedOuputSolutionTwo.java:htmlFormatTable.html
class MainController
{
private static String currentTotalPayment;
public static void main(String[] args)
{
CheckBankFilesView cbfView = new CheckBankFilesView();
cbfView.setVisible(true);
}
public static void setCurrentTotalPayment(String totalPayment) {
MainController.currentTotalPayment = totalPayment;
}
public static String getCurrentTotalPayment() {
return currentTotalPayment;
}
}
:
public class MyFormattedOuputSolutionOne extends javax.swing.JFrame {
private void MyFormattedOuputSolutionOne() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
myFormattedOuput = new javax.swing.JEditorPane();
myFormattedOuput.setContentType("text/html");
//myFormattedOuput.setContentType("text/html; charset=UTF-8"); //Doesn't seem to work
myFormattedOuput.setEditable(false);
jScrollPane1.setViewportView(myFormattedOuput);
myFormattedOuput.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
try{
myFormattedOuput.setPage(getClass().getResource("resources/emailFormat2.html"));
//How can I edit/change html elements loaded in 'myFormattedOuput'?
}catch(Exception e){
}
}
}
MainController.java
public class MyFormattedOuputSolutionTwo extends javax.swing.JFrame {
private void MyFormattedOuputSolutionTwo() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
myFormattedOuput = new javax.swing.JEditorPane();
myFormattedOuput.setContentType("text/html");
//myFormattedOuput.setContentType("text/html; charset=UTF-8"); //Doesn't seem to work
myFormattedOuput.setEditable(false);
jScrollPane1.setViewportView(myFormattedOuput);
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();
myFormattedOuput.setEditorKit(kit);
myFormattedOuput.setDocument(doc);
myFormattedOuput.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
try{
// Tried to set the charset in <head> but it doesn't work!
//kit.insertHTML(doc, 1, "<meta http-equiv = \"Content-Type\" content = \"text/html; charset=UTF-8\">", 0, 0, HTML.Tag.META);
kit.insertHTML(doc, doc.getLength(), "<label> السلام عليكم ورحمة الله وبركاته ,,, </label>", 0, 0, null); //Encoding problem
kit.insertHTML(doc, doc.getLength(), "<br/>", 0, 0, null); // works fine
kit.insertHTML(doc, doc.getLength(), MainController.getCurrentTotalPayment(), 0, 0, null); // works fine
//How can I solve the Unicode problem above?
}catch(Exception e){
}
}
}
感谢
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8">
</head>
<body>
<label> السلام عليكم ورحمة الله وبركاته ,,, </label>
<br/>
<label> الأخوة الكرام نفيدكم بتفاصيل المدفوعات لشهر </label> XX/143X </label> هـ كما هو موضح ادناه </label>
<br/>
<table align="right" border="1" width="600" cellpadding="5" cellspacing="0">
<tr char="utf-8" bgcolor="cccccc" align="center">
<td colspan="3"> <label> تفاصيل مدفوعات بنك الرياض </label> <img src="..\images\riyadh.gif" width="65" height="15"/> </td>
</tr>
<tr align="center">
<td></td>
<td id="cell1">0,000,000.00</td>
<td align="right"> معاشات </td>
</tr>
<tr align="center">
<td></td>
<td id="cell2">0,000,000.00</td>
<td align="right"> أخطار </td>
</tr>
<tr align="center">
<td bgcolor="cccccc"> المجموع </td>
<td bgcolor="cccccc"> 0,000,000.00 <label> ريال سعودي </label> </td>
<td></td>
</tr>
</table>
<br/>
<label> شاكرين لكم حسن تعاونكم ...... </label>
<br/>
<label> فريق العمليات بقسم الحاسب الآلي </label>
</body>
</html>
您阅读我的长篇多个问题帖子,迫不及待地等待您的回答。
更新:
感谢@Howard 的这种见解,如果我用相应的unicode 替换阿拉伯字符(例如 party = \u0628),它可以正常工作,但必须有一种方法可以做到这一点,而无需替换每个字符,对吗?
that's going to be a long question so bear with me :)
My Application
I'm developing a Java (with JFrame GUI) desktop application that does the following:
- Scan (.txt) files.
- Parses some numbers from these
files, performs some calculations on
them and finally stores the results
in String variables. - Outputs these numbers in a special
(table) format. (Note: the format
includes some Unicode (Arabic)
Characters.)
Problem
The first two parts went smoothly. However when I came to the 3th part (the formatted output) I didn't know how to display this special format so,
- What is the best way to display a special formatted output (table) in Java?
Note: Formatter is not going to help because it has no proper support for tables.
Solution One:
I did my research and found that I could use JEditorPane, since it can display special formats such as "html". So I decided to create an "html" page with the needed (table) format and then display this page on [JEditorPane][4]. I did that and it went smoothly until I wanted to change some html elements' values to the parsed numbers from those (.txt) files.
- How can I have an access to an html
element(e.g.<td></td>
) and change
its value?
Note that the (.html) is loaded inside JEditorPane using setPage(url)
.
The Unicode characters are displayed properly but I couldn't change some of the elements values (e.g. I want to change the value of <td> 000,000,000 </td>
to <td> MainController.getCurrentTotalPayment() </td>
Solution Two:
I've found a workaround to this which involves using HTMLDocument and HTMLEditorKit, That way I can create the (.html) using HTMLEditorKit from scratch and display it on the JEditorPane using kit.insertHTML
.
I have successfully added the content using the above method and I also was able to add the parsed numbers from (.txt) files because I have them stored in my (MainController) class. Unfortunately, the Unicode Arabic characters were not displayed properly.
- How can I display these Unicode
characters properly?
So the first solution lacks the access to html elements and the second lacks the Unicode support!
My colleagues advised me to use JSP code in the html document that can have an access to my MainController.java class. Therefore, loading the page into JEditorPane with the html elements changed already. Isn't there a way to do that without the help of JSP?
Some other people recommended the use of JTidy but isn't there a way to do it within Java's JDK?
I'm open to all possible solutions. Please help.
My Code: Some code content were omitted because they are not relevant
MainController.java
class MainController
{
private static String currentTotalPayment;
public static void main(String[] args)
{
CheckBankFilesView cbfView = new CheckBankFilesView();
cbfView.setVisible(true);
}
public static void setCurrentTotalPayment(String totalPayment) {
MainController.currentTotalPayment = totalPayment;
}
public static String getCurrentTotalPayment() {
return currentTotalPayment;
}
}
MyFormattedOuputSolutionOne.java:
public class MyFormattedOuputSolutionOne extends javax.swing.JFrame {
private void MyFormattedOuputSolutionOne() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
myFormattedOuput = new javax.swing.JEditorPane();
myFormattedOuput.setContentType("text/html");
//myFormattedOuput.setContentType("text/html; charset=UTF-8"); //Doesn't seem to work
myFormattedOuput.setEditable(false);
jScrollPane1.setViewportView(myFormattedOuput);
myFormattedOuput.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
try{
myFormattedOuput.setPage(getClass().getResource("resources/emailFormat2.html"));
//How can I edit/change html elements loaded in 'myFormattedOuput'?
}catch(Exception e){
}
}
}
MyFormattedOuputSolutionTwo.java:
public class MyFormattedOuputSolutionTwo extends javax.swing.JFrame {
private void MyFormattedOuputSolutionTwo() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
myFormattedOuput = new javax.swing.JEditorPane();
myFormattedOuput.setContentType("text/html");
//myFormattedOuput.setContentType("text/html; charset=UTF-8"); //Doesn't seem to work
myFormattedOuput.setEditable(false);
jScrollPane1.setViewportView(myFormattedOuput);
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();
myFormattedOuput.setEditorKit(kit);
myFormattedOuput.setDocument(doc);
myFormattedOuput.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
try{
// Tried to set the charset in <head> but it doesn't work!
//kit.insertHTML(doc, 1, "<meta http-equiv = \"Content-Type\" content = \"text/html; charset=UTF-8\">", 0, 0, HTML.Tag.META);
kit.insertHTML(doc, doc.getLength(), "<label> السلام عليكم ورحمة الله وبركاته ,,, </label>", 0, 0, null); //Encoding problem
kit.insertHTML(doc, doc.getLength(), "<br/>", 0, 0, null); // works fine
kit.insertHTML(doc, doc.getLength(), MainController.getCurrentTotalPayment(), 0, 0, null); // works fine
//How can I solve the Unicode problem above?
}catch(Exception e){
}
}
}
htmlFormatTable.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8">
</head>
<body>
<label> السلام عليكم ورحمة الله وبركاته ,,, </label>
<br/>
<label> الأخوة الكرام نفيدكم بتفاصيل المدفوعات لشهر </label> XX/143X </label> هـ كما هو موضح ادناه </label>
<br/>
<table align="right" border="1" width="600" cellpadding="5" cellspacing="0">
<tr char="utf-8" bgcolor="cccccc" align="center">
<td colspan="3"> <label> تفاصيل مدفوعات بنك الرياض </label> <img src="..\images\riyadh.gif" width="65" height="15"/> </td>
</tr>
<tr align="center">
<td></td>
<td id="cell1">0,000,000.00</td>
<td align="right"> معاشات </td>
</tr>
<tr align="center">
<td></td>
<td id="cell2">0,000,000.00</td>
<td align="right"> أخطار </td>
</tr>
<tr align="center">
<td bgcolor="cccccc"> المجموع </td>
<td bgcolor="cccccc"> 0,000,000.00 <label> ريال سعودي </label> </td>
<td></td>
</tr>
</table>
<br/>
<label> شاكرين لكم حسن تعاونكم ...... </label>
<br/>
<label> فريق العمليات بقسم الحاسب الآلي </label>
</body>
</html>
Thank you for reading my long multiple questions thread and cannot wait for your answer.
Update:
Thanks to @Howard for this insight, if I replace the arabic character with its corresponding unicode (e.g. ب = \u0628) it works fine but there must be a way to do it without the need to replace each character, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案一
可以编辑加载到 JEditorPane 中的 HTML。以下是基于 MyFormattedOuputSolutionOne.java 的完整代码:
它异步加载文档并等待加载页面。加载页面时,它会访问文档的元素来搜索元素,并将文本插入到表格第二行和第三行的第一个
中。
顺便说一句,您的 HTML 无效!您应该清理它。当您执行此操作时,文档元素的索引将会更改,并且您必须调整查找插入点的代码。
窗口看起来是这样的:
解决方案二
我发现编码没有问题。字符显示正确。但我必须在 Eclipse 项目中将 Java 文件的编码设置为 UTF-8。
解决方案三
您是否考虑过使用
JTable
在用户界面?HTML 可能如下所示:
由于我一个字都不懂,所以我无法提出更好的格式。首先,
< ;label>
元素仅允许在使用这种 HTML 结构,HTML 树中的
元素将位于索引 4 处,即您应该将该行更改
为
Theindexs 0–3 is now
;
元素。附带说明一下,您可以在将 HTML 文档传递到
之前对其进行编辑,而不是在将 HTML 加载到
以便它已在JEditorPane
(将其加载到HTMLDocument
的文本模型中)后对其进行编辑。 setPage元素中包含正确的数据。由于
JEditorPane.setPage
方法仅接受URL
,因此您的选择是read
,它接受InputStream
和>Object
描述模型(在您的情况下应该是HTMLDocument
的实例)。StringBufferInputStream
是此任务的最佳候选者,但它已被弃用,因为它无法正确读取 UTF-8 字符。考虑到这一点,您宁愿使用String.getBytes("UTF-8")
函数(自 J2SE 6 起),以及ByteArrayInputStream
,您的HTML 声明了编码,JEditorPane
在读取时会尊重它。Solution One
It is possible to edit HTML loaded into JEditorPane. Here's the complete code based on your MyFormattedOuputSolutionOne.java:
It loads document asynchronously and waits for page to be loaded. When page is loaded, it accesses the elements of the document to search for elements and inserts text into the first
<td>
in the 2nd and 3rd row of the table.By the way your HTML is not valid! You should clean it up. When you do it, the indexes of the document elements will change and you'll have to adjust code which finds the insertion points.
The window looks this way:
Solution Two
I've found no issues with encoding. The characters display correctly. Yet I had to set the encoding of Java files to UTF-8 in the Eclipse project.
Solution Three
Have you considered using
JTable
to display table of results in the UI?The HTML might look this way:
Since I don't understand a word, I cannot propose a better formatting. First of all,
<label>
elements are allowed only in<form>
. You had a sequence of three<label>
s above the table where only one of them had opening<label>
tag, there were three closing</label>
tags. I made them all into<p>
; however if you meant them to be headers for table columns, you should have used a table row with three<th>
elements.With this structure of the HTML,
<table>
element in the HTML tree would be at index 4, i.e. you should change the lineto
The indexes 0–3 are now
<p>
elements.As a side note, instead of editing HTML after loading it into
JEditorPane
, which loads it into text model ofHTMLDocument
, you could edit your HTML document before passing tosetPage
so that it already contains the correct data in<td>
elements. SinceJEditorPane.setPage
method accepts onlyURL
, then your choice would beread
which accepts anInputStream
andObject
which describes the model (should be instance ofHTMLDocument
in your case).StringBufferInputStream
is the best candidate for this task yet it was deprecated because it cannot correctly read UTF-8 characters. Having this in mind, you would rather useString.getBytes("UTF-8")
function (since J2SE 6), andByteArrayInputStream
, your HTML declares the encoding andJEditorPane
would respect it when reading.