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

  • Home
  • SEARCH
  • 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 8722985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:31:36+00:00 2026-06-13T07:31:36+00:00

just looking for a quick answer: is it possible to use a JTable as

  • 0

just looking for a quick answer: is it possible to use a JTable as a JScrollPane’s columnHeader?

I have a configed JTable with different column width and column title, and plan to use the header as the columnHeader of a scrollpane. How can I achieve this? I set the table with

scrollPane.setColumnHeaderView(table);

but it doesn’t show up.

so thanks to Guillaume Polet, it should be

scrollpane.setColumnHeaderView(table.getTableHeader());

but all the columns have the same width now although I set them with different values in my table. How could I let the table columns show different width?

  • 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-06-13T07:31:37+00:00Added an answer on June 13, 2026 at 7:31 am

    If I understand you correctly, you want the column headers of your table to appear in the column headers of the viewport but you want something else in the viewport view?

    Then you need to grab the table header and set it as the column header of the viewport.

    Here is an example:

    import java.awt.BorderLayout;
    import java.util.Vector;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestTableHeader {
    
        protected void initUI() {
            Vector<Vector<Object>> data = new Vector<Vector<Object>>();
            Vector<String> colNames = new Vector<String>();
            for (int i = 0; i < 5; i++) {
                colNames.add("Col-" + (i + 1));
            }
    
            table = new JTable(data, colNames);
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            scrollpane = new JScrollPane();
            scrollpane.setColumnHeaderView(table.getTableHeader());
            scrollpane.setViewportView(new JLabel("some label in the viewport view"));
            frame.add(scrollpane, BorderLayout.CENTER);
            frame.pack();
            frame.setVisible(true);
        }
    
        private JTable table;
        private JScrollPane scrollpane;
    
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
                UnsupportedLookAndFeelException {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TestTableHeader().initUI();
                }
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Really quick here... I think I have the answer, but just looking for some
Maybe a browser plugin? Just looking for a dirty, quick way to test some
I just looking how to use LINQ for grouping a list. Class Item Public
I am just looking at mapKit and decided to make a quick button to
I have a question, just looking for suggestions here. So, my application is 'modernizing'
Just a quick one really, I'm just looking for a bit of clarification. I'm
To troubleshoot an installation, sometimes I just want a quick answer to what version
I'm looking for a quick-and-dirty solution to this, I have to set up a
So pragmatically, I've got a quick and dirty answer to what I'm looking for
Just looking for a good PHP Image Library, I want to display images with

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.