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 / GUI / January 2005

Tip: Looking for answers? Try searching our database.

Multiple JTables with headers in a JScrollPane

Thread view: 
ian.a.desouza@gmail.com - 06 Jan 2005 00:35 GMT
I'm trying to get multiple JTables each displayed entirely with their
own headers  to show up in one containing JScrollPane (through an
intermediary JPanel container).

Any ideas on how to do this, since by adding a JTable to a JPanel,
avoids the headers showing up (something to do with JTable.addNotify()
which calls the configureEnclosingScrollPane method, which states:

"If this JTable is the viewportView of an enclosing JScrollPane (the
usual situation), configure this ScrollPane by, amongst other things,
installing the table's tableHeader as the columnHeaderView of the
scroll pane. When a JTable is added to a JScrollPane in the usual way,
using new JScrollPane(myTable), addNotify is called in the JTable (when
the table is added to the viewport). JTable's addNotify method in turn
calls this method, which is protected so that this default installation
procedure can be overridden by a subclass."
This seems like a clumsy way of doing things if you ask me.
John McGrath - 06 Jan 2005 14:52 GMT
> I'm trying to get multiple JTables each displayed entirely with their
> own headers  to show up in one containing JScrollPane (through an
[quoted text clipped - 3 lines]
> avoids the headers showing up (something to do with JTable.addNotify()
> which calls the configureEnclosingScrollPane method, which states:

Get the headers using getTableHeader() and add them to the panel yourself.

Signature

Regards,

John McGrath

ian.a.desouza@gmail.com - 07 Jan 2005 21:54 GMT
Thanks John. That worked (though I wish Sun had something on the JTable
as a cleaner alternative)
John McGrath - 07 Jan 2005 22:58 GMT
> Thanks John. That worked (though I wish Sun had something on the JTable
> as a cleaner alternative)

What did you have in mind?  I cannot think of much they could do to make
it cleaner.  Making the header a separate component seems appropriate,
since the header normally is fixed while the main part of the table
scrolls.  They could have created a separate table implementation that
contains a table and a header, and forwards method calls to the
appropriate child component, but that seems pretty ugly, and it introduces
a lot of cruft.

I suppose they could have provided an additional component that would deal
with placing the header, but that is really simple to create:

  public class TablePanel extends JPanel {
     public TablePanel( JTable table ) {
        super( new BorderLayout() );
        add( table, BorderLayout.CENTER );
        add( table.getTableHeader(), BorderLayout.NORTH );
     }
  }

With the above component, instead of adding the JTable to your UI, you can
add new TablePanel(table) instead, and it will show the header above the
table.

I know that many people are surprised when they place a JTable outside of
a JScrollPane and the table header is not shown.  This behavior is well
documented at the top of the JTable Javadocs, but there are some people
who just jump in without carefully reading the docs (who?, ME? never!).

So I guess what it comes down to is that the relationship between the
table and its header is not at all complicated, but it is not intuitive
either.  While it certainly would be a good thing for it to be intuitive,
making it so could make the code more complex and overshadow the benefits.
I cannot think of a simple way to make it more intuitive.

Signature

Regards,

John McGrath

ian.a.desouza@gmail.com - 11 Jan 2005 21:11 GMT
Hi John,

I just wanted to group attributes of some particular entity. Each set
of attributes in a different table with all the rows showing. All the
tables in a vertical Box. Then have the enveloping Box in a
JScrollPane. When I did this I was surprized by the column headers not
showing (since I had defined the getColumnName in the table model --
that's my issue, since it doesn't mention that in the docs of
getColunmName of TableModel).

Anyway, I can understand if it causes headaches, since one would not
usually want the header to scroll. I like your idea of writing a
wrapper class that does the scrolling and has a method like
addTable(JTable table), that would extract the header, add it to the
box (which would be in the JScrollPane), then add the table itself; all
with the appropriate layout manangers.

Again, thanks for your help.


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.