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 / July 2004

Tip: Looking for answers? Try searching our database.

JTable size adjustment after addrow(), JScrollPane

Thread view: 
Maximilian - 27 Jul 2004 10:06 GMT
Hi all.

Basically I'm doing the following:

--snip--
DefaultTableModel myTableModel=
    new DefaultTableModel(new String[] {"header1","header2"},0);
JTable myTable=new JTable();
myTable.setModel(myTableModel);

TableScrollPane.setViewportView(myTable);
--snap--

When I do some myTable.addrow(...)s, the table is not resizing istself
appropriately, resulting in data being "cut off" at the bottom.

Does anyone know how to correctly adjust a table's preferred size (or
whatever) after changing its contents to fit the new row count and thus
making it usable in a ScrollPane?

Many thanks in advance,

Maximilian
Maximilian - 27 Jul 2004 20:55 GMT
> When I do some myTable.addrow(...)s, the table is not resizing istself
> appropriately, resulting in data being "cut off" at the bottom.
[quoted text clipped - 4 lines]
>
> Many thanks in advance,

Just given the case that somebody had the same problem...
That's what I was looking for (at least the code does what i wanted):

myTable = new JTable() {
public Dimension getPreferredSize() {
    int HeightSum=0;
        for(int i=0; i<getRowCount(); i++){
            HeightSum+=getRowHeight(i);
        }
        return new Dimension(SizeOfSolumns(),HeightSum);
}
public Dimension getPreferredScrollableViewportSize(){
    return getPreferredSize();
}

public int SizeOfColumns() {
    int WidthSum=0;
    for (int i=0; i<getColumnCount(); i++) {
        WidthSum+=SizeOfColumn(i);
    }
    if(getParent().getSize().width<WidthSum)
        return WidthSum;
    return getParent().getSize().width;
}
public int SizeOfColumn(int vColIndex) {
    DefaultTableColumnModel colModel =
        (DefaultTableColumnModel)getColumnModel();
    TableColumn col = colModel.getColumn(vColIndex);
    int width = 0;

    TableCellRenderer renderer = col.getHeaderRenderer();
    if (renderer == null) {
        renderer = getTableHeader().getDefaultRenderer();
    }
    Component comp = renderer.getTableCellRendererComponent(
        this, col.getHeaderValue(), false, false, 0, 0);
    width=comp.getSize().width;

    return width;
  }
};

Regards,

Maximilian
Christian Kaufhold - 28 Jul 2004 13:12 GMT
>> When I do some myTable.addrow(...)s, the table is not resizing istself
>> appropriately, resulting in data being "cut off" at the bottom.

Please post a compilable example that shows that problem.


>> Does anyone know how to correctly adjust a table's preferred size (or
>> whatever) after changing its contents to fit the new row count and thus
>> making it usable in a ScrollPane?

There is no need to do anything like that.

The code you posted also does not help things in any way, and will pro-
bably also confuse the column layout.

Christian


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.