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 1808758
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:18:10+00:00 2026-05-17T06:18:10+00:00

I followed some tips in this guide: JTable with horizontal scrollbar but still having

  • 0

I followed some tips in this guide: JTable with horizontal scrollbar but still having problems.

I have two column headers: Name and Description (for our purposes in this ex.)

I want to be able to scroll horizontally whenever an entry is added (ie: Name/Description is just 1 line). However, Swing doesn’t seem to default to that behavior!

I have a JTable embedded in a ScrollPane. JScrollPane has the following parameters: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED and JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED

If I disable with: myTable.AutoResizeMode(AUTO_RESIZE_OFF) the column headers don’t fill up the whole table (maybe just 1/2 of the table). I have to manually resize the “Description” Column to see the whole thing.

How can I have autoresizing, but the horizontal scrolling still works?

  • 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-17T06:18:10+00:00Added an answer on May 17, 2026 at 6:18 am

    You can extend JTable as follows:

    public class JHorizontalFriendlyTable extends JTable {
    
      @Override
      public Dimension getPreferredSize() {
        if (getParent () instanceof JViewport) {
          if (
            ((JViewport) getParent()).getWidth() > super.getPreferredSize().width)
          ) {
            return getMinimumSize();
          }
        }
        return super.getPreferredSize(); 
      }
    
      @Override
      public boolean getScrollableTracksViewportWidth () {
        if (autoResizeMode != AUTO_RESIZE_OFF) {
          if (getParent() instanceof JViewport) {
            return (((JViewport) getParent()).getWidth() > getPreferredSize().width);
          }
          return true;
        }
        return false;
      }
    }
    

    sample usage:

    jScrollPane1 = new JScrollPane();
    TableModel jTable1Model = new DefaultTableModel(...); 
    JTable jTable1 = new JHorizontalFriendlyTable();
    jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    jScrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    jScrollPane1.setViewPortView(jTable1);
    jTable1.setModel(jTable1Model);
    jTable1.setPreferredSize(new java.awt.Dimension(1051,518));
    jTable1.setPreferredScrollableViewPortSize(new java.awt.Dimension(1000,528));
    jTable1.getSize(new java.awt.Dimension(1051, 528));
    
    if (jTable1.getPreferredScrollableViewPortSize().getWidth() > 
      ((JViewPort) jTable1.getParent()).getPreferredSize().getWidth())
      {
      jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
      jTable1.doLayout();
    }
    
    jTable1.setDragEnabled(false);
    jTable1.setColumnSelectionAllowed(false);
    jTable1.getTableHeader().setReorderingAllowed(false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have followed some tutorials on how to create custem event accessors. This is
I'm trying to use ASP.net profiles I've followed some instructions which means I have
I ran across some routes that followed this format: resources :foobar, except: create I
I have a file containing some lines of code followed by a string pattern.
This might be a noob question, but can't find an answer anywhere. I have
What are some of the different Usability tips/tricks and best practices followed with WinForms-based
I'm still finding my way around the iPhone SDK, I've followed some good tutorials
I have followed some tutorials on setting up a WCF web service with security
I am using Paperclip to upload multiple item images. I followed some of this
I have been having this problem wherein my threads stop execution and go into

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.