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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:49:53+00:00 2026-05-28T05:49:53+00:00

I have a JTable that is using a TableColumnModelListener() to detect when the column

  • 0

I have a JTable that is using a TableColumnModelListener() to detect when the column has been re-sized and I have some code I want to execute in the columnMarginChanged() method.

How do I determine whether the column was re-sized by the user or as a result of other code?

I am thinking I have to start with ChangeEvent.getSource() but I don’t know where to go from there.

Thank you.

  • 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-28T05:49:54+00:00Added an answer on May 28, 2026 at 5:49 am

    I can give you one possible approach. I was trying to solve the same problem, because I wanted to serialize information about column widths to disk, so that the next time the table opened up in my application, I could restore the column widths appropriately. Here goes:

    Step 1 – Override your JTable and add a boolean property to it

    class MyTable extends JTable {
    
        private boolean isColumnWidthChanged;
        public boolean getColumnWidthChanged() {
            return isColumnWidthChanged;
        }
    
        public void setColumnWidthChanged(boolean widthChanged) {
            isColumnWidthChanged = widthChanged;
        }
    
    }
    

    Step 2 – Add a TableColumnModelListener() to the table

    private class TableColumnWidthListener implements TableColumnModelListener
    {
        @Override
        public void columnMarginChanged(ChangeEvent e)
        {
            /* columnMarginChanged is called continuously as the column width is changed
               by dragging. Therefore, execute code below ONLY if we are not already
               aware of the column width having changed */
            if(!tableObj.hasColumnWidthChanged())
            {
                /* the condition  below will NOT be true if
                   the column width is being changed by code. */
                if(tableObj.getTableHeader.getResizingColumn() != null)
                {
                    // User must have dragged column and changed width
                    tableObj.setColumnWidthChanged(true);
                }
            }
        }
    
        @Override
        public void columnMoved(TableColumnModelEvent e) { }
    
        @Override
        public void columnAdded(TableColumnModelEvent e) { }
    
        @Override
        public void columnRemoved(TableColumnModelEvent e) { }
    
        @Override
        public void columnSelectionChanged(ListSelectionEvent e) { }
    }
    

    Step 3 – Add a mouse listener to the table header

    private class TableHeaderMouseListener extends MouseAdapter
    {
        @Override
        public void mouseReleased(MouseEvent e)
        {
            /* On mouse release, check if column width has changed */
            if(tableObj.getColumnWidthChanged())
            {
                // Do whatever you need to do here
    
                // Reset the flag on the table.
                tableObj.setColumnWidthChanged(false);
            }
        }
    }
    

    NOTE: In my application, the TableHeaderMouseListener and TableColumnWidthListener classes were private inner classes of my main application class. My main application class held on to a reference of the table being observed. Therefore, these inner classes had access to the table instance. Obviously, depending on your setup, you need to do the appropriate thing to make the table instance available to these other classes. Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable that I want to use to display some data (a
I have a JTable using a custom DefaultTableModel which has some Booleans in the
I have a JTable where I want to highlight some rows using a custom
I have some problems displaying data in JTable. My app is using a JTable
I am trying to make a JTable that has column spans available. Specifically, I
I have tried using the code below to display ImageIcons in JTable. But when
i have jtable filed with data. i want to create java code for Action
I have a JTable that is populated using a custom TableModel I created. I
I have a JTable that will have the last column data field change to
I am using an editable JTable that contains a column named Subject. When the

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.