Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6067197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:34:57+00:00 2026-05-23T09:34:57+00:00

I have a JTable with 3 columns: – No. # – Name – PhoneNumber

  • 0

I have a JTable with 3 columns:

- No. #
- Name
- PhoneNumber

I want to make specific width for each column as follows:

enter image description here

and I want the JTable able to update the widths of its columns dynamically if needed (for example, inserting large number in the column #) and keeping same style of the JTable

I solved the first issue, using this code:

myTable.getColumnModel().getColumn(columnNumber).setPreferredWidth(columnWidth);

but I didn’t success to make myTable to update the widths dynamically ONLY if the current width of the column doesn’t fit its contents. Can you help me solving this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:34:57+00:00Added an answer on May 23, 2026 at 9:34 am

    Here I found my answer: http://tips4java.wordpress.com/2008/11/10/table-column-adjuster/
    The idea is to check some rows’ content length to adjust the column width.
    In the article, the author provided a full code in a downloadable java file.

    JTable table = new JTable( ... );
    table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
    
    for (int column = 0; column < table.getColumnCount(); column++)
    {
        TableColumn tableColumn = table.getColumnModel().getColumn(column);
        int preferredWidth = tableColumn.getMinWidth();
        int maxWidth = tableColumn.getMaxWidth();
    
        for (int row = 0; row < table.getRowCount(); row++)
        {
            TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
            Component c = table.prepareRenderer(cellRenderer, row, column);
            int width = c.getPreferredSize().width + table.getIntercellSpacing().width;
            preferredWidth = Math.max(preferredWidth, width);
    
            //  We've exceeded the maximum width, no need to check other rows
    
            if (preferredWidth >= maxWidth)
            {
                preferredWidth = maxWidth;
                break;
            }
        }
    
        tableColumn.setPreferredWidth( preferredWidth );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable with a number of columns. I want a particular column
I have a JTable and I need to be able to reorder the columns.
I have a JTable where the first column in each row is a checkbox.
Say I have following columns in my Jtable: {Item, Price} I want format the
I have a JTable with 6 columns. The first column is a JCheckBox column.
I have a JTable with some columns. I have a HashMap of the column
I have a JTable with two columns and both are JComboBox, for this purpose
In my JTable, I have two editable numeric columns. The editor for both columns
Hi I an issue with editors in a JTable. I have a column which
I have a JTable that I want to use to display some data (a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.