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

Discussion in 'Java' started by ant, Sep 21, 2006.

  1. ant

    ant Guest

    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!
    thanks a lot!
    ant, Sep 21, 2006
    #1
    1. Advertising

  2. 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.


    1) An applet will need to be 'trusted' before it can do any such thing.
    2) To be trusted, it will need to be signed and accepted as trusted by
    the end user.
    3) converting the table data to HTML is relatively trivial, so..

    > if you hava some useful suggestions or you know some third-party
    > components,


    ....a third party 'component' is entirely overkill!

    Andrew T.
    Andrew Thompson, Sep 21, 2006
    #2
    1. Advertising

  3. ant wrote:
    > how to save the data of a JTable to a html file?


    Write code to do it. Open a text file. Write the page's HTML header and
    start of the body to the file. Write the start of an HTML table to the
    file. For each row, read the data from the table model, replace special
    HTML entities in the data, generate an HTML table row and write it to
    the file. At the end close the HTML table, the HTML body and the HTML
    root element. Close the file.

    > now, I am doing a Java Applet program,


    If you want to store the file locally you will need to sign the applet.

    > if you hava some useful suggestions or you know some third-party
    > components, please tell me!


    Writing the code should consume much less time than searching for some
    3rd party tool and learning that tool. This is an almost trivial
    programming task.

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    Thomas Weidenfeller, Sep 21, 2006
    #3
  4. 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/
    Thomas Hawtin, Sep 21, 2006
    #4
  5. ant

    ant Guest

    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/
    ant, Sep 22, 2006
    #5
  6. ant wrote:
    > > ant wrote:
    > > > how to save the data of a JTable to a html file?

    ....
    > thanks, All.


    Your future lack of top-posting, should be thanks enough.

    > Now, I met another problem.
    > Because it is an Applet program, if I want to access local files,


    The only practical ways for an applet to access local files are
    a) Sign the applet and get the user to accept the signed code.
    b) Launch the applet via. WebStart and use the FileService.

    Note there is another way to get an applet to the point that
    it can break out of the security sandbox..

    >...now I
    > modified the java.security file ...


    ...and that is it.

    A few 'highly motivated' and 'technically proficient' people
    might be able to edit policy files on there own PC in order
    to allow an applet to run, but that is not a good description
    of the average internet surfer.

    Andrew T.
    Andrew Thompson, Sep 22, 2006
    #6
  7. ant wrote:
    > 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?


    Did you at least read the answers you got? Or was that already too much
    work for your royal highness? May I repeat them for you, sir?

    > Andrew Thompson wrote:
    >> 1) An applet will need to be 'trusted' before it can do any such
    >> thing.
    >> 2) To be trusted, it will need to be signed and accepted as trusted by
    >> the end user.


    > Thomas Weidenfeller wrote:
    >> If you want to store the file locally you will need to sign the
    >> applet.



    --
    The comp.lang.java.gui FAQ:
    http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    Thomas Weidenfeller, Sep 22, 2006
    #7
  8. ant

    ant Guest

    to Andrew Thompson:
    thanks for your answer!

    to Thomas Weidenfeller:
    I am sorry for my inattention.
    thank you! I know what I should do.

    Thomas Weidenfeller wrote:
    > ant wrote:
    > > 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?

    >
    > Did you at least read the answers you got? Or was that already too much
    > work for your royal highness? May I repeat them for you, sir?
    >
    > > Andrew Thompson wrote:
    > >> 1) An applet will need to be 'trusted' before it can do any such
    > >> thing.
    > >> 2) To be trusted, it will need to be signed and accepted as trusted by
    > >> the end user.

    >
    > > Thomas Weidenfeller wrote:
    > >> If you want to store the file locally you will need to sign the
    > >> applet.

    >
    >
    > --
    > The comp.lang.java.gui FAQ:
    > http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
    > ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    ant, Sep 22, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sincethe2003
    Replies:
    2
    Views:
    531
    Craig Deelsnyder
    Jul 14, 2004
  2. Tivo Escobar
    Replies:
    1
    Views:
    7,238
    manusa
    Apr 12, 2007
  3. Tintin92
    Replies:
    1
    Views:
    1,661
    Andrew Thompson
    Feb 14, 2007
  4. Shahar Golan
    Replies:
    5
    Views:
    274
    kaeli
    Oct 16, 2003
  5. Haircuts Are Important
    Replies:
    3
    Views:
    292
    Haircuts Are Important
    Jun 4, 2013
Loading...

Share This Page