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 / April 2007

Tip: Looking for answers? Try searching our database.

How do I set table header height...  without side effects?

Thread view: 
Qu0ll - 13 Apr 2007 14:31 GMT
These comments relate to Java 6 Update 1 and Update 2 but may apply to
earlier versions (including Java 5) as well.

I am trying to set the height of a table's header by using the following
(very simple) code:

table.getTableHeader().setPreferredSize(new
Dimension(getTableHeader().getWidth(), 22));

This works in that the table header height is set accordingly but there is a
very nasty side effect.  That is, if the table is set up to scroll
horizontally, the header and the body of the table seem to move somewhat
independently. Specifically, if you scroll to the right it will at first
seem that the header has scrolled accordingly but then after a few seconds
delay the header goes back to the start position as though no scrolling had
happened. Nasty.

Can anyone think of a reason why this is happening? Is there a
better/simpler way to set the height of the header in a table that is
configured to scroll horizontally?

Here's a very simple test app that illustrates the problem. Just run it and
scroll to to the right to observe the problem. Why does setting the table
header height lead to such problems? What is the correct way to set the
height of the table header?

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class TableHeaderTest extends JFrame
{
private JTable table;
private JScrollPane pane;

public TableHeaderTest() {
 Object[][] data = new Object[][] {{1, 2, 3, 4, 5, 6},
                                         {2, 2, 3, 4, 5, 6},
                                         {3, 2, 3, 4, 5, 6},
                                         {4, 2, 3, 4, 5, 6}};
 String[] colNames = new String[] {"Column A", "Column B", "Column C",
         "Column D", "Column E", "Column F"};
 setLayout(new BorderLayout());
 pane = new JScrollPane();
 table = new JTable(data, colNames);
 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 table.getTableHeader().setPreferredSize(new Dimension(pane.getWidth(),
24));
 pane.setViewportView(table);
 add(pane, BorderLayout.CENTER);
 pack();
 setPreferredSize(new Dimension(280, 250));
 setSize(new Dimension(280, 250));
 setLocationRelativeTo(null);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
 EventQueue.invokeLater(new Runnable() {
  public void run() {
   new TableHeaderTest().setVisible(true);
  }
 });
}
}

Signature

And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)

Jeff Higgins - 14 Apr 2007 00:00 GMT
>  table.getTableHeader()
       .setPreferredSize(new Dimension(pane.getWidth(), 24));

table.getTableHeader()
    .setPreferredSize(new Dimension(table.getColumnModel()
        .getTotalColumnWidth(), 24));
Qu0ll - 14 Apr 2007 01:04 GMT
>>  table.getTableHeader()
>        .setPreferredSize(new Dimension(pane.getWidth(), 24));
>
> table.getTableHeader()
>     .setPreferredSize(new Dimension(table.getColumnModel()
>         .getTotalColumnWidth(), 24));

Thanks, that's got it.  I need to track table resize events too and re-set
the height as well accordingly.

Signature

And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)



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.