Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Array to nice table presentation

Thread view: 
lounger - 14 Apr 2004 13:22 GMT
Hello all,  I am somewhat of a newbiew but learn real fast.

I have a Sorted Map and want to parse out the contents into a nice
viewable table.  My first thought was to create an HTML doc and display
it in a JEditorPane.

I can't, for the life of me, figure out how to get started.  I am able
to parse out the contents but am stuck there.

I am open to any suggestions. But would prefer something not to
complicated and not to have to save a file to the file system.

Thanks a lot.
John - 14 Apr 2004 13:28 GMT
> Hello all,  I am somewhat of a newbiew but learn real fast.
>
[quoted text clipped - 9 lines]
>
> Thanks a lot.

Once you have parsed it what data structure is it in? Post it. All you
have to do is restructure the data into a 2D array and then use it to
spew out a great big string with a few <table><tr><td> type tags thrown
in for good measure.

john
lounger - 14 Apr 2004 14:36 GMT
>> Hello all,  I am somewhat of a newbiew but learn real fast.
>>
[quoted text clipped - 16 lines]
>
> john

That makes sense.  How do I get it into the JEditorPane?
   public JEditorPane drawDoc(String type) throws IOException{  
       Set entrySet;    
       compPane = null;
       compPane = new JEditorPane();
       compPane.setContentType("text/html");

       if (type.equals("Nets")) {
           entrySet = NetMap.getNets().entrySet();
           for (Iterator it = entrySet.iterator(); it.hasNext(); ) {
             Map.Entry entry = (Map.Entry)it.next();
             Net net = ((Net)entry.getValue());
              //These are the column entries
             netName =  net.toString()
             isCrit = net.isBCrit();
             ...
           }  
       }
   }
John - 14 Apr 2004 14:57 GMT
>>> Hello all,  I am somewhat of a newbiew but learn real fast.
>>>
[quoted text clipped - 34 lines]
>            }         }
>    }

 Putting it into the JEditorPane is the straightforward bit.

(JEditorPane myPane is initialised elsewhere)
(mySortedMap is an entity object of your own creation.)

String tableString = mySortedMap.getTableHTML();
myPane.setText(tableString);

(IS EQUIVALENT TO)

myPane.setText(mySortedMap.getTableHTML()); //looks a bit neater

John
lounger - 14 Apr 2004 16:12 GMT
>>>> Hello all,  I am somewhat of a newbiew but learn real fast.
>>>>
[quoted text clipped - 49 lines]
>
> John

I almost get it. Is  mySortedMap the same as 'entrySet', or am I
creating a new Sorted Map with the column values in it?
lounger - 14 Apr 2004 16:18 GMT
>>>> Hello all,  I am somewhat of a newbiew but learn real fast.
>>>>
[quoted text clipped - 49 lines]
>
> John

I almost get it. Is  mySortedMap the same as 'entrySet', or am I
creating a new Sorted Map with the column values in it?

Thank you so much for your patients!

This is what I have now:

        if (type.equals("Nets")) {           
            entrySet = NetMap.getNets().entrySet();
            for (Iterator it = entrySet.iterator(); it.hasNext(); ) {
              Map.Entry entry = (Map.Entry)it.next();
               Net net = ((Net)entry.getValue());
                netString = net.toString()
                        +" - "
                         + net.isBCrit()
                         +" - "
                         + net.getDiffMate()
                        +" - "
                        + net.isBClk()
                        +" - "
                        + net.isBBus()
                        +" - "
                        + net.isBPwr()
                        +" - "
                        + net.isBGnd()
                        +" - "
                        + net.isBSingle()
                        +" - "
                        + net.isBDiff()
                        +" - "
                        + net.isTagged()
                        +" - "
                        + net.getId()
                        + newLine
                 ;
                  System.out.println(netString);
            }
John - 14 Apr 2004 17:03 GMT
>>>>> Hello all,  I am somewhat of a newbiew but learn real fast.
>>>>>
[quoted text clipped - 87 lines]
>                   System.out.println(netString);
>             }

Forget the "mySortedMap" thing for now.  I assume that each time you go
round the for loop and print netString you are printing the data for one
row. What you need to do is insert HTML instead of all the hyphens.

A table looks like this in HTML (2 rows and 5 cols)

<table>
  <tr>
    <td>data</td>
    <td>data</td>
    <td>data</td>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
    <td>data</td>
    <td>data</td>
    <td>data</td>
  </tr>
</table>

so for each row you want:

    <td>data</td>
    <td>data</td>
    <td>data</td>
    <td>data</td>
    <td>data</td>

where data is just whatever you are displaying.

Then all you need to do is create another string to start with "<table>"
 and add netString after each trip around the for loop. Once you have
finished the for loop add </table>.

Finally put this huge string with all the table data into the
JEditorPane using setText().

Hey presto.

John
lounger - 14 Apr 2004 17:27 GMT
>>>>>> Hello all,  I am somewhat of a newbiew but learn real fast.
>>>>>>
[quoted text clipped - 133 lines]
>
> John

That's exactly what I just did ( I guess I got it after all).  Thanks.  
However, it is extremely slow.  Is there a better way?
Christophe Vanfleteren - 14 Apr 2004 17:32 GMT
> That's exactly what I just did ( I guess I got it after all).  Thanks.
> However, it is extremely slow.  Is there a better way?

What part is extremely slow? Setting the content of the JTextPane or
creating the HTML String? If it is the latter, you that you are using a
StringBuffer and not just appending to a String instead.

Signature

Kind regards,
Christophe Vanfleteren

SPC - 16 Apr 2004 13:22 GMT
> > That's exactly what I just did ( I guess I got it after all).  Thanks.
> > However, it is extremely slow.  Is there a better way?
>
> What part is extremely slow? Setting the content of the JTextPane or
> creating the HTML String? If it is the latter, you that you are using a
> StringBuffer and not just appending to a String instead.

AFAIK as of JDK1.4.0 string appending automatically uses a
stringbuffer, and there is no-longer a need to manually use a
stringbuffer for simple 'one line' appends like:
String s = "a" + c + "c"; // or similar

Fine if you can be sure of your JDK.

Regards

Steve
Christophe Vanfleteren - 16 Apr 2004 13:28 GMT
>> What part is extremely slow? Setting the content of the JTextPane or
>> creating the HTML String? If it is the latter, you that you are using a
[quoted text clipped - 6 lines]
>
> Fine if you can be sure of your JDK.

Appending like this has always used a StringBuffer for these constructs
IIRC.

But it is very unprobable that the OP is using this kind of construct to
generate an entire html table. He's probable appending to a String in a
loop, and in those cases, you need to use the StringBuffer for sure.

Consider

String s = "<table>";
for(int i=0;i<1000;i++) {
s = s + "<tr><td>a";
}

vs

StringBuffer sb = new StringBuffer(4000)
for(int i=0;i<1000;i++) {
a.append("<tr><td>")
a.append("a");
}

Signature

Kind regards,
Christophe Vanfleteren

GaryM - 14 Apr 2004 17:13 GMT
> Hello all,  I am somewhat of a newbiew but learn real fast.
>
[quoted text clipped - 9 lines]
>
> Thanks a lot.

If you are open to open source, check out display tag library for a
sweet set of tools which should cover all html table needs.

Project page is:

http://sourceforge.net/projects/displaytag/

Home page is

http://displaytag.sourceforge.net/
Chris Smith - 14 Apr 2004 17:53 GMT
> I have a Sorted Map and want to parse out the contents into a nice
> viewable table.  My first thought was to create an HTML doc and display
> it in a JEditorPane.

That's certainly doable, and you've got a good bit of advice on how to
do it.  If you're looking for other options, though, it wouldn't be too
difficult to display the data in a JTable.  The advantage is that the
JTable would read data in the native form rather than building and
parsing an intermediate file format, and that the data can be more
immediately manipulated by the user (e.g., by selecting a set of cells
and so forth) for further operations.

To do this, look into implementing a TableModel by extending
AbstractTableModel.  You just need to provide methods to get the data at
certain locations, and to report the total number of rows and columns.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 15 Apr 2004 23:59 GMT
>I have a Sorted Map and want to parse out the contents into a nice
>viewable table.  My first thought was to create an HTML doc and display
>it in a JEditorPane.

I suppose you could, but the code will run faster if you build a
JTable.

Google to find yourself some tutorials on setting them up. The
trickiest thing about them is JTable is single thread.  You must do
everything on the Swing thread, perhaps using invokeLater.

To get started see http://mindprod.com/jgloss/jtable.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Liz - 16 Apr 2004 01:32 GMT
> >I have a Sorted Map and want to parse out the contents into a nice
> >viewable table.  My first thought was to create an HTML doc and display
[quoted text clipped - 8 lines]
>
> To get started see http://mindprod.com/jgloss/jtable.html

I got the sun SimpleTableDemo.java program from their web site
and filled it with time zones using
TimeZone.getTimeZone, and it looks real nice. Better than html I think.

> --
> Canadian Mind Products, Roedy Green.
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.