Simple example for fomatting output in columns?

A

A Watcher

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

Manish Pandit

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...ocale, java.lang.String, java.lang.Object...)

-cheers,
Manish
 
K

Knute Johnson

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

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

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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

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

It is the way to handle structured data.

Arne
 
K

Knute Johnson

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

It is the way to handle structured data.

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

Lasse Reichstein Nielsen

Knute Johnson said:
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
 
A

Andrew Thompson

Lasse said:
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) );
}
}
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?).
 
I

Ian Wilson

Knute said:
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.
 
K

Knute Johnson

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

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

Roedy Green

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Knute said:
>
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?

Other people has answered that.

(and probably better than I could)

Arne
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top