Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JTable Column Sizing

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    Tucson, AZ
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Lightbulb JTable Column Sizing

    I am having some difficulty resizing columns in a JTable. The code I am using works just fine in I have the Look and Feel set to Nimbus, but any other Look and Feel does not set the column widths based on the code below. Infact I get different results depending on what font has been set.
     
    	private void updateColWidths()
    	{
    		try
    		{
    			TableColumnModel columnModel = table1.getColumnModel();
    			for (int col = 0; col < table1.getColumnCount(); col ++)
    			{
    				int colWidth = 0;
    				for (int row = 0; row < table1.getRowCount(); row ++)
    				{
    					Component comp = table1.prepareRenderer(table1.getCellRenderer(row, col), row, col);
    					colWidth = Math.max(colWidth, comp.getPreferredSize().width);
    				}
     
    				TableColumn column = columnModel.getColumn(col);
    				TableCellRenderer headerRenderer = column.getHeaderRenderer();
                    if (headerRenderer == null) {
                        headerRenderer = table1.getTableHeader().getDefaultRenderer();
                    }
                    Object headerValue = column.getHeaderValue();
                    Component headerComp = headerRenderer.getTableCellRendererComponent(table1, headerValue, false, false, 0, col);
                    colWidth = Math.max(colWidth, headerComp.getPreferredSize().width);
    				column.setPreferredWidth(colWidth);
    			}
    		}
    		 catch(ClassCastException e) {}
    	}

    Here are some screen shots of the different look and feels' using Verdana 16 as the font.

    Nimbus nimbus.jpg

    Metal metal.jpg

    Motif motif.jpg

    GTK+ GTK.jpg

    Any ideas what might be causing this? I'd just leave this with Nimbus, but my Mac users ( it always the Mac users that stir-up trouble ) would like to use their default Look and Feel.


  2. #2
    Junior Member
    Join Date
    Dec 2012
    Location
    Tucson, AZ
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JTable Column Sizing

    I have found a solution which leads me to believe that get.PreferedSzie()width is not computing the proper width based on Look and Feel and the selected font. By adding a constant > 1 to colWidth when setting the preferred width all the columen are not properly sized. I selected a value of 5 as this provides a bit of a margin and makes the display look nice and clean.

Similar Threads

  1. JTable column widths reverting to defaults?
    By eewill in forum AWT / Java Swing
    Replies: 3
    Last Post: January 25th, 2012, 03:49 PM
  2. change JTable column name, gui does not update at once
    By renars in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 23rd, 2011, 10:39 AM
  3. [SOLVED] Quick JTable Column Sort Question
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: October 15th, 2010, 04:35 PM
  4. Excel Column Letter Referencing
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 21st, 2010, 03:47 PM
  5. Highlighting both row and column in JTable
    By bschneider14 in forum AWT / Java Swing
    Replies: 4
    Last Post: May 29th, 2010, 09:14 AM