I'm not sure why my JTable is not showing any headings:
import javax.swing.JOptionPane;
import javax.swing.JTable;
public class Currency {
public static void editCurrencies(){
String[] headings = new String[] {"Code", "Rate"};
Object[][] data = new Object[][] {
{"AUD", new Float(2.6124)},
{"EUR", new Float(1.4961)},
{"GBP", new Float(1.0000)},
{"USD", new Float(1.8463)}
};
JTable table = new JTable(data, headings);
JOptionPane.showConfirmDialog(
MyApp.frame,
table,
"Currencies and exchange rates",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,
null
);
} // method editCurrencies
} // class Currency
>java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)
I'm running from Eclipse 3.1.0 where the Java Build Path includes
jre1.5.0_06
I was expecting column headings in the table, can anyone point out what
I am doing wrong?
Vova Reznik - 28 Mar 2006 18:46 GMT
Somebody has to do it.
1. JScrollPane does it automatically:
JOptionPane.showConfirmDialog(null,
new JScrollPane(table),
"Currencies and exchange rates", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE, null);
2. Or manually place JTableHeader
JOptionPane.showConfirmDialog(null,
new Object[] {table.getTableHeader(), table },
"Currencies and exchange rates", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE, null);
> I'm not sure why my JTable is not showing any headings:
>
[quoted text clipped - 32 lines]
> I was expecting column headings in the table, can anyone point out what
> I am doing wrong?
Ian Wilson - 29 Mar 2006 11:48 GMT
>> I'm not sure why my JTable is not showing any headings:
>>
[quoted text clipped - 32 lines]
>> I was expecting column headings in the table, can anyone point out
>> what I am doing wrong?
> Somebody has to do it.
>
[quoted text clipped - 13 lines]
> JOptionPane.OK_CANCEL_OPTION,
> JOptionPane.PLAIN_MESSAGE, null);
Thanks, I tried both methods. Interestingly the resulting dialog size is
quite different, without a JScrollPane everything is packed quite
compactly into the JDialog. With a JScrollPane I get a lot of wasted
space. I'll have to look into sizing, which brings to mind a follow-up
question ...
I'm beginning to feel uncomfortable with my choice of Java reference
books - I have O'Reilly's "Learning Java", "Java CookBook" and an old
edition of "Java Swing". The first two work for me but I'm finding the
"Java Swing" book to be pretty impenetrable - can anyone suggest
anything better for someone new to Swing components?
Vova Reznik - 29 Mar 2006 15:24 GMT
About wasted space with JScrollPane -
It is time for you to read about LayoutManager.
Default layout for the content pane is BorderLayout
that count preferred sizes. Default preferred size
for JScrollPane is about 400x400.
Tutorial on java.com is good enough.
http://java.sun.com/docs/books/tutorial/uiswing/index.html
>>> I'm not sure why my JTable is not showing any headings:
>>>
[quoted text clipped - 63 lines]
> "Java Swing" book to be pretty impenetrable - can anyone suggest
> anything better for someone new to Swing components?
Ian Wilson - 29 Mar 2006 16:30 GMT
>>>> public class Currency {
>>>> public static void editCurrencies(){
[quoted text clipped - 59 lines]
> Tutorial on java.com is good enough.
> http://java.sun.com/docs/books/tutorial/uiswing/index.html
Thanks, I ended up adding this:
table.setPreferredScrollableViewportSize(new Dimension(
table.getColumnCount()*30, // ignored if < min(colwidth)*cols?
table.getRowHeight()*3)); // show 3 rows
Vova Reznik - 29 Mar 2006 16:54 GMT
> Thanks, I ended up adding this:
>
> table.setPreferredScrollableViewportSize(new Dimension(
> table.getColumnCount()*30, // ignored if < min(colwidth)*cols?
> table.getRowHeight()*3)); // show 3 rows
Than you don't need scroll pane.
And, better
table.getRowHeight()* table.getRowCount()
Ian Wilson - 29 Mar 2006 17:50 GMT
Object[][] data = new Object[][] {
{"AUD", new Float(2.6124)},
{"EUR", new Float(1.4961)},
{"GBP", new Float(1.0000)},
{"USD", new Float(1.8463)}
>> Thanks, I ended up adding this:
>>
[quoted text clipped - 5 lines]
> And, better
> table.getRowHeight()* table.getRowCount()
You perhaps overlooked that my table at present has 4 rows. I picked 3
to test scrolling. :-)