Re: how to save the data of a JTable to a html file?



thanks, All.
Now, I met another problem.
Because it is an Applet program, if I want to access local files, now I
modified the java.security file in jre1.5.0_07\lib\security directory.
But the project is a B/S project, so I think it's not a good way. we
can't change every files in Client.
my question is: if I don't want to modify the java.security file, what
way should I walk?
thanks!

Best Regards


Thomas Hawtin wrote:
ant wrote:
how to save the data of a JTable to a html file?
now, I am doing a Java Applet program, and I need to save the content
of a JTable to html formatted file.
if you hava some useful suggestions or you know some third-party
components, please tell me!

Perhaps not directly relevant (it's a piece of cake to generate HTML
from the table model), but I thought I'd try using JTable itself (well,
the PL&F) to generate HTML. JTable supports copying as HTML via
clipboards and drag and drop, which we can exploit with a private
clipboard. Disappointing code and disappointing results below.

Some bad points:

o You need to mutate the table (select all).
o The APIs are cumbersome.
o Actual requirements of the implementation are under specified.
o The table is plain, with just toString applied to data.
o Even the header isn't included.
o There's no sensible extension mechanism.
o It's not particularly efficiently coded.

Tom Hawtin

import java.awt.*;
import java.awt.datatransfer.*;
import javax.swing.*;

class ExportTableAsHTMLDemo {
private static final DataFlavor HTML_STRING_FLAVOR;
static {
// The datatransfer API could do with some work here...
try {
HTML_STRING_FLAVOR = new DataFlavor(
"text/html;class=java.lang.String"
);
} catch (ClassNotFoundException exc) {
// Don't even load class.
throw new Error(exc);
}
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
go();
}
});
}
public static void go() {
// Some random data.
javax.swing.JTable table = new javax.swing.JTable(
new Object[][]{
new Object[] { "some", "data" },
new Object[] { "more", "data" },
},
new Object[] { "Col A", "Col B" }
);

// Exports selected data only.
table.selectAll(); // Evil.

javax.swing.TransferHandler handler =
table.getTransferHandler();
if (handler == null) {
System.err.println("No transfer handler.");
return;
}
int actions = handler.getSourceActions(table);
if ((actions & javax.swing.TransferHandler.COPY) == 0) {
System.err.println("Table does not support copy.");
return;
}

java.awt.datatransfer.Clipboard clipboard =
new java.awt.datatransfer.Clipboard(
"Export table as HTML private clipboard"
);
try {
handler.exportToClipboard(
table, clipboard, javax.swing.TransferHandler.COPY
);
} catch (IllegalStateException exc) {
exc.printStackTrace();
return;
}
java.awt.datatransfer.Transferable transferable =
clipboard.getContents(/* unused... */null);
if (transferable == null) {
System.err.println("Clipboard empty");
return;
}
// Just support HTML as String.
// Could also use HTML as Reader or UTF-8 InputStream
// (particularly for large tables,
// if the implementation was better).
if (!transferable.isDataFlavorSupported(HTML_STRING_FLAVOR)) {
System.err.println("HTML (String) not supported");
return;
}
try {
Object data = transferable.getTransferData(
HTML_STRING_FLAVOR
);
System.out.println(data);
} catch (java.io.IOException exc) {
exc.printStackTrace();
return;
} catch (java.awt.datatransfer.UnsupportedFlavorException exc) {
System.err.println("HTML (String) not supported");
return;
}
}
}

<html>
<body>
<table>
<tr>
<td>some</td>
<td>data</td>
</tr>
<tr>
<td>more</td>
<td>data</td>
</tr>
</table>
</body>
</html>
--
Unemployed English Java programmer
http://jroller.com/page/tackline/

.



Relevant Pages

  • Re: how to save the data of a JTable to a html file?
    ... Perhaps not directly relevant (it's a piece of cake to generate HTML from the table model), but I thought I'd try using JTable itself to generate HTML. ... JTable supports copying as HTML via clipboards and drag and drop, which we can exploit with a private clipboard. ... } catch (ClassNotFoundException exc) { ... public static void main{ ...
    (comp.lang.java.programmer)
  • Re: accessing document object model (DOM) using vb and asp.net
    ... This is crude and prone to error-- Powerpoint ... where one button click creates the html and performs the ... copy to clipboard and the second button click creates a Powerpoint instance ... >> My development environment is Visual Web developer Express 2005. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Word 2004 for Mac-2 strange features?
    ... OPEN the HTML file in Word." ... clipboard is a "marker" that tells the clipboard what area you had selected. ... When you get to the destination application and "Paste", ... Many applications on the Mac have not yet figured out ...
    (microsoft.public.mac.office.word)
  • Re: getData HTML format???
    ... The above only gets the TEXT (no HTML ... > clipboard methods allows you to manipulate ..uhm... ... > you get html tags? ... > *selection*, not with the underground mechanics of how this selection ...
    (comp.lang.javascript)
  • Re: Clipboard to DataTable
    ... The only way to do this would be to try and load the file, string, HTML, ... > clipboard is in a format one could create a dataTable from. ...
    (microsoft.public.dotnet.languages.csharp)