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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:11:16+00:00 2026-05-14T02:11:16+00:00

Long story short, I’m making a custom Swing component that’s basically a JTable with

  • 0

Long story short, I’m making a custom Swing component that’s basically a JTable with a panel on its side. The table should of course be scrollable and have a table header, but since I only want the header to be above the actual JTable and not above the side panel, I had to pull some tricks to make it work. But that part works fine.

However, in the process I’ve somehow managed to break the mouse-drag-column-resizing functionality of the JTableHeader. I am completely clueless as to why and what I can do about it.

Below is a minimal working sample to illustrate my problem.

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.Scrollable;
import javax.swing.table.JTableHeader;


final class FooTable extends JPanel implements Scrollable {

    public FooTable() {
        initComponents();
    }

    // Generated by NetBeans IDE
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        sidePanel = new javax.swing.JPanel();
        table = new javax.swing.JTable();

        setLayout(new java.awt.GridBagLayout());

        sidePanel.setMinimumSize(new java.awt.Dimension(70, 0));
        sidePanel.setPreferredSize(new java.awt.Dimension(70, 0));
        sidePanel.setLayout(null);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 1.0;
        add(sidePanel, gridBagConstraints);

        table.setFont(table.getFont().deriveFont(table.getFont().getSize()+1f));
        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", "A", "I", "-"},
                {"2", "B", "II", "--"},
                {"3", "C", "III", "---"},
                {"4", "D", "IV", "----"},
                {"5", "E", "V", "-----"},
                {"6", "F", "VI", "------"},
                {"7", "G", "VII", "-------"},
                {"8", "H", "VIII", "--------"},
                {"9", "I", "IX", "---------"},
                {"10", "J", "X", "----------"}
            },
            new String [] {
                "Column 1", "Column 2", "Column 3", "Column 4"
            }
        ));
        table.setRowHeight(24);
        table.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        add(table, gridBagConstraints);
    }// </editor-fold>

    public JScrollPane createScrollView() {
        JScrollPane jsp = new JScrollPane(this);
        JViewport jvp = new JViewport();
        final JTableHeader th = new JTableHeader();

        th.setTable(table);
        th.setColumnModel(table.getColumnModel());
        th.setResizingAllowed(true);

        jvp.setView(new JPanel() {

            {
                setLayout(null);
                add(th);

                th.setLocation(70, 0);

                FooTable.this.addComponentListener(new ComponentAdapter() {

                    @Override
                    public void componentResized(ComponentEvent e) {
                        th.setSize(FooTable.this.getWidth(), th.getPreferredSize().height);
                    }
                });
                setPreferredSize(new Dimension(th.getPreferredSize().width, th.getPreferredSize().height));
            }
        });
        jsp.setColumnHeader(jvp);
        return jsp;
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel sidePanel;
    private javax.swing.JTable table;
    // End of variables declaration


    //
    // Scrollable implementation
    //
    public Dimension getPreferredScrollableViewportSize() {
        Dimension d = new Dimension();
        d.width = sidePanel.getPreferredSize().width + table.getPreferredSize().width;
        d.height = sidePanel.getPreferredSize().height + table.getPreferredSize().height;
        return d;
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
        return table.getScrollableUnitIncrement(visibleRect, orientation, direction);
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return table.getScrollableBlockIncrement(visibleRect, orientation, direction);
    }

    public boolean getScrollableTracksViewportWidth() {
        return table.getScrollableTracksViewportWidth();
    }

    public boolean getScrollableTracksViewportHeight() {
        return table.getScrollableTracksViewportHeight();
    }

    //
    // Test program
    //
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FooTable fooTable = new FooTable();
        f.add(fooTable.createScrollView());
        f.pack();
        f.setVisible(true);
    }

}
  • 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-14T02:11:16+00:00Added an answer on May 14, 2026 at 2:11 am

    I think what you are missing is also telling the JTable about the JTableHeader. In the method createScrollView try adding the following just before returning:

        table.setTableHeader(th);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Long story short, I have a substantial Python application that, among other things, does
Long story short: We've got a system that we're moving from one server to
Long story short, I have two regex patterns. One pattern matches things that I
Long story short, I have a SQL file that I want to import as
Long story short, I'm working on a .NET profiler that at one point gets
Long story short: I have some controller logic that requests a value from the
To make a long story short I have to use processing in a class
Long story short: I've been layout out UI's with Html/CSS for about 8 years
Long story short, I'm taking a bunch of excel documents one by one, and
Long story short, I need to get up to speed with Joomla fast .

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.