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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:04:29+00:00 2026-06-11T21:04:29+00:00

There is a JTable with columns in predifined order. Like +———————-+ |id | name

  • 0

There is a JTable with columns in predifined order. Like

+———————-+

|id | name |age |

I want to move columns in the order (for example name=0, age=1, id=2).

+———————-+

|name | age | id |

How can i achive that?
JTable has method moveColumns, but unfortunately this is not enough. Assume order is strored in map:.

Map<String, Integer> columnOrder = getOrder();
for(Map.Entry<String, Integer> e : columnOrder.entrySet()) {
    int columnIndex = jtable.convertColumnIndexToView(jtable.getColumn(e.getKey()).getModelIndex());
    jtable.getColumnModel().moveColumn(columnIndex, e.getValue());
}

This does not work, because moveColumn also moves other columns, so from docu:

void moveColumn(int columnIndex,
          int newIndex)

Moves the column and its header at columnIndex to newIndex. The old
column at columnIndex will now be found at newIndex. The column that
used to be at newIndex is shifted left or right to make room.

Can i (dynamically) somehow put the columns in the exactly same order as in my map?

  • 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-11T21:04:31+00:00Added an answer on June 11, 2026 at 9:04 pm

    The underlying problem is of course that Vector, which is used in the DefaultTableColumnModel to store the columns and their order has no decent move support. The move is achieved as a remove and add, which triggers the “move left or right” behavior.

    You can however use this knowledge to simply move the columns in the correct order (from lowest index to highest index).

    Starting from

    |id | name |age |
    

    to

    |name | age | id |
    

    can be achieved by first moving name to index 0. This will result in a

    |name|id|age
    

    vector. If you then move age to index 1, it will do a remove first

    |name|id
    

    followed by an insert at index 1

    |name|age|id
    

    By making sure the element is always moved to the left side of its original position, you know how the shifting will behave.

    Of course this depends on an implementation detail, but write a quick unit test to support your assumptions and use those implementation details (or write your own TableColumnModel 😉 )

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

Sidebar

Related Questions

I have a simple JTable, there are two columns that matter: quantity and value
Is there a way to tell a JTable's row filter that it should update
I am working in JTable and I have a requirement like this. Say There
I want to store the results of reading lucene index into jTable, so that
In C#, is there an equivalent to the Java JTable? Additionally, I'd like to
I want map a List of beans to a JTable. The idea is that
Is there a way to add a gap between JTable columns or rows? (without
There is JTable, and one of columns has Date format. I need to sort
I have a JTable with multiple columns that display doubles, some of which are
I am using a JScrollPane to display a JTable . I want the columns

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.