empty JTable
I have a JTable whose values gets filled only dynamically.
At times, when the table is empty, I want to show a message like
"no records found" just under the table header.
Just like how in windows machine, when you make a search and if it doesnt
return any records, it prints "Search is complete. There are no results to display"
(stretching many columns) under the table header. I considered using JLabel
(as shown below) but there is always some space b/w the two components.
Let me know ur suggestions. Until then, lemme try out other Layout managers.
Just a code snippet:
class MyTableextends JFrame
{
privateJPaneltopPanel;
privateJTabletable;
privateJScrollPane scrollPane;
public MyTable()
{
setSize( 300, 150 );
topPanel =new JPanel();
topPanel.setLayout(new BorderLayout() );
getContentPane().add( topPanel );
String columnNames[] ={"Column 1","Column 2","Column 3"};
String dataValues[][] =
{
};
table =new JTable( dataValues, columnNames );
scrollPane =new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );
topPanel.add(new Label("No records available"),BorderLayout.SOUTH);
}
publicstaticvoid main( String args[] )
{
MyTable t =new MyTable();
t.setVisible(true );
}
}
[2314 byte] By [
krrajesh2a] at [2007-9-30 2:19:12]

And one more thing, in that code, when I setSize() in the main method (instead of constructor), I am able to see the added components only after I maximize the frame. Why ?!Thanks
Hello,try to set the vertical alignment from the label:JLabel label=new JLabel("No records available");label.setVerticalAlignment(JLabel.TOP);topPanel.add(label,
TiScha at 2007-7-16 13:28:05 >

Hi Tim,
Thanks for ur response. But that doesnt seem to solve the problem.
The JTable doesn't seem to give up its "extra" space. I cant setBounds() the Table
since it might also get filled with records. The only way I can think of now is to break
the table into two (one with just header, and the other with only contents -- and show the 2nd
only when there are records). But this seem rather a round-about way.
Let me know if there is a simple solution for this one.
Thanks.
table = new JTable( dataValues, columnNames );table.setPreferredScrollableViewportSize(table.getPreferredSize());
I tried that one, but again.....
As you suspected, the problem is with the LayoutManager you are using. Read this section from the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]Using Layout Managers[/url].
You are using setSize(...). Therefore any extra space is allocated to the component added to the center of the panel.
A BoxLayout is probably what you are looking for.
First of all, thanks for holding on to this thread to help me out.
LayoutManagers can help in effective usage of the available space. Given the components,
it can place them on the frame/panel wrt space constraints. But the JTable component
itself allocates some of its space for its table values, which is what I want to get rid of
(when no values are available). So, my suspect now is JTable itself!!
Any ideas ? Am I in the right track ?!
Place the JTable and the message component in a panel using CardLayout and overlay the JTable with the message. When the JTable is empty show the message. When not empty show the JTable.
> But the JTable component
itself allocates some of its space for its table values, which is what I want to get rid of
(when no values are available). So, my suspect now is JTable itself!!
Any ideas ? Am I in the right track ?!
No!!
The table does have some abitraray height, but I gave you code to get the preferredSize() of the table which will reset this height. This is not the problem.
As I told you in my previous reply, you are using setSize(300, 150) which gives the frame a height of 150. The label is only a height of maybe 20 pixels, so the remaining 130 pixels is allocated to the component that was added to the CENTER of the BorderLayout, which in your case is the table!!!!
Try adding the table to the North and the label to the South and you will see a different layout.
Another test is to get rid of your setSize(...) code and use the following instead:
MyTable t = new MyTable();
t.pack();
t.setVisible( true );
Read the tutorial!!!
Let me state it again. I want the table header to show up all time. I need to alternate betweentable contents (exclude header) and a message string. (Something similar to Windows "Search Files/Folders" option.)
Read the tutorial? Is that the stock answer these days?Mitch
> Read the tutorial!!!
This is always the correct answer :)
There is a saying "Go (forth and seek), further up you will find Glory."
So, I went further up NORTH with the same old BorderLayout and it worked.
The following did the trick:
table.setPreferredScrollableViewportSize(table.getPreferredSize());<<<===
scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.NORTH );
JLabel label=new JLabel("No records available");
label.setVerticalAlignment(JLabel.TOP); <<<====
topPanel.add(label, BorderLayout.CENTER);
Thanks evry1 for ur inputs, esp camickr.
KRR