Re: Simple example for fomatting output in columns?
Lasse Reichstein Nielsen wrote:
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?).
--
Andrew Thompson
http://www.athompson.info/andrew/
Message posted via http://www.javakb.com