Array to nice table presentation

L

lounger

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

John

lounger said:
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.
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
 
L

lounger

John said:
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

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();
...
}
}
}
 
J

John

lounger said:
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();
...
} }
}
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
 
L

lounger

John said:
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
I almost get it. Is mySortedMap the same as 'entrySet', or am I
creating a new Sorted Map with the column values in it?
 
L

lounger

John said:
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
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);
}
 
J

John

lounger said:
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);
}

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
 
G

GaryM

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.


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/
 
L

lounger

John said:
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

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?
 
C

Christophe Vanfleteren

lounger said:
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.
 
C

Chris Smith

lounger said:
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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

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
 
L

Liz

Roedy Green said:
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

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

SPC

Christophe Vanfleteren said:
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
 
C

Christophe Vanfleteren

SPC said:
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.

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");
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top