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 / General / August 2007

Tip: Looking for answers? Try searching our database.

Simple example for fomatting output in columns?

Thread view: 
A Watcher - 15 Aug 2007 22:43 GMT
I'm a novice at Java and I can't figure out something that is simple
in other languages.  I need to display columns of output.  Some are
integer, some decimal and some character.  The data varies in size
from line to line.  How can get this to line up?  I'm writing to a
JTextArea.  I've looked a JTables and got totally bewildered.   Thanks
for any help.
Manish Pandit - 15 Aug 2007 23:14 GMT
> I'm a novice at Java and I can't figure out something that is simple
> in other languages.  I need to display columns of output.  Some are
> integer, some decimal and some character.  The data varies in size
> from line to line.  How can get this to line up?  I'm writing to a
> JTextArea.  I've looked a JTables and got totally bewildered.   Thanks
> for any help.

JDK 1.5+ has support for formatted printing -

http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html#printf(java.uti
l.Locale,%20java.lang.String,%20java.lang.Object
...)

-cheers,
Manish
Knute Johnson - 16 Aug 2007 00:06 GMT
> I'm a novice at Java and I can't figure out something that is simple
> in other languages.  I need to display columns of output.  Some are
> integer, some decimal and some character.  The data varies in size
> from line to line.  How can get this to line up?  I'm writing to a
> JTextArea.  I've looked a JTables and got totally bewildered.   Thanks
> for any help.

Look at Manish's post on formatted printing and you will need a
mono-spaced font.  You can use 'Monospaced' as a font name and a system
font will be chosen for you.  Also look at the class Formatter.

Signature

Knute Johnson
email s/nospam/knute/

Arne Vajhøj - 16 Aug 2007 01:41 GMT
> I'm a novice at Java and I can't figure out something that is simple
> in other languages.  I need to display columns of output.  Some are
> integer, some decimal and some character.  The data varies in size
> from line to line.  How can get this to line up?  I'm writing to a
> JTextArea.  I've looked a JTables and got totally bewildered.

I think you should spend the time to learn JTable and friends.

It is the way to handle structured data.

Arne
Knute Johnson - 16 Aug 2007 04:27 GMT
>> I'm a novice at Java and I can't figure out something that is simple
>> in other languages.  I need to display columns of output.  Some are
[quoted text clipped - 7 lines]
>
> Arne

Arne:

I haven't played much with JTable.  Can you easily create a table for
different types of data and set the text alignment?  What would be the
advantage of a JTable over just drawing the text?

Thanks,

Signature

Knute Johnson
email s/nospam/knute/

Lasse Reichstein Nielsen - 16 Aug 2007 06:09 GMT
> I haven't played much with JTable.  Can you easily create a table for
> different types of data and set the text alignment?  

Fairly simple, yes.

> What would be the advantage of a JTable over just drawing the text?

That the JTable takes care of doing the table layout for you, you just
have to provide the cell data (a TableModel) and say how each type should
be rendered (some TableCellRenderer's), which you would anyway, and then
the rest is taken care of.

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Andrew Thompson - 16 Aug 2007 08:39 GMT
>> I haven't played much with JTable.  Can you easily create a table for
>> different types of data and set the text alignment?  
>
>Fairly simple, yes.

The JavaDocs has a pretty good run-down on JTables,
<http://java.sun.com/docs/books/tutorial/uiswing/components/table.html>
but here is a minimalist example..

<sscce>
import javax.swing.*;

class TableTest {

 public static void main(String[] args) {
   Object[][] data = {
     { "January",new Float(43.2),"Midge Green",new Integer(014) },
     { "February",new Float(48.9),"John Smiley",new Integer(401) },
     { "March",new Float(45.6),"Minh Truong",new Integer(208) }
   };
   Object[] titles =
     {"Month", "High Sales", "Salesman", "Emp. ID"};
   JTable table = new JTable(data, titles);
   JOptionPane.showMessageDialog(null,
     new JScrollPane(table) );
 }
}
</sscce>

>> What would be the advantage of a JTable over just drawing the text?
>
>That the JTable takes care of doing the table layout for you, you just
>have to provide the cell data (a TableModel) and say how each type should
>be rendered (some TableCellRenderer's),

Yes, that table could use right alignment for the
'High Sales', and a renderer that displays the leading
'0' of the (supposedly 3 digit) 'Emp. ID' (as well as
right alignment?).

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Ian Wilson - 16 Aug 2007 12:19 GMT
> I haven't played much with JTable.  Can you easily create a table for
> different types of data and set the text alignment?  What would be the
> advantage of a JTable over just drawing the text?

With JTable you get the following for free (no extra coding effort)
- user can resize columns using drag & drop
- user can re-order columns using drag & drop
etc

A JTable allows you to add features with less effort, e.g.
- user can edit data in-place
- you can embed other components (e.g. JSpinners) in the table.

With a little work you can have the JTable allow sorting by clicking on
the column headings. Google TableSorter. There's bound to be lots of
table-oriented code examples and reusable code for JTable that there
isn't for JTextArea.
Knute Johnson - 16 Aug 2007 21:29 GMT
>> I haven't played much with JTable.  Can you easily create a table for
>> different types of data and set the text alignment?  What would be the
[quoted text clipped - 13 lines]
> table-oriented code examples and reusable code for JTable that there
> isn't for JTextArea.

Thanks everybody.  I'll have to play around with it some.

Signature

Knute Johnson
email s/nospam/knute/

Arne Vajhøj - 17 Aug 2007 02:02 GMT
>>> I'm a novice at Java and I can't figure out something that is simple
>>> in other languages.  I need to display columns of output.  Some are
[quoted text clipped - 9 lines]
> different types of data and set the text alignment?  What would be the
> advantage of a JTable over just drawing the text?

Other people has answered that.

(and probably better than I could)

Arne
Roedy Green - 16 Aug 2007 22:28 GMT
On Wed, 15 Aug 2007 14:43:14 -0700, A Watcher
<stocksami@earthlink.net> wrote, quoted or indirectly quoted someone
who said :

>I'm a novice at Java and I can't figure out something that is simple
>in other languages.  I need to display columns of output.  Some are
>integer, some decimal and some character.  The data varies in size
>from line to line.  How can get this to line up?  I'm writing to a
>JTextArea.  I've looked a JTables and got totally bewildered.   Thanks
>for any help.

it is not easy.  You need a JTable.  There is some minimal code posted
at http://mindprod.com/products2.html#TZ
see http://mindprod.com/jgloss/jtable.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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.