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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:16:57+00:00 2026-06-03T13:16:57+00:00

I have a JTable where I have set table.setAutoCreateRowSorter(true); . My main objective is

  • 0

I have a JTable where I have set table.setAutoCreateRowSorter(true);. My main objective is to have one row that is always at the top, regardless of any column sort order. First thing I did was modify the compareTo method for my cell datatype so that it would understand a flag that means “less than anything else” and therefore sort that item always to the top. Unfortunately, that made it so that the given row would always be at the very top or the very bottom, depending on the sort order for the column. So when the sort order is descending, what I need is to set the cell so that its effective value is greater than anything else. Only problem is I can’t figure out how to get the column sort order.

On this page, one suggested answer is along the lines of

if (myTable.getRowSorter().getSortKeys().get(column).getSortOrder == SortOrder.ASCENDING) {
    ...
}

Unfortunately, getSortKeys() always returns an empty list. So I’m back at square one. I also considered adding a RowSorterListener, but this appears to be a listener for when the sorter is changed, not when the sort order is changed.

Any help? Thanks!

EDIT: Here is code that demonstrates part of my problem. Note the System.out.println, where the result is always zero. I would like to get the row sort order for the columns, but none seems to exist.

package tablecolumn;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.table.AbstractTableModel;

public class Tablecolumn extends JFrame {
    JScrollPane scroll;
    JTable table;
    MyTableModel model;

    static String[] columnnames = new String[] {"A", "B", "C"};

    public class MyTableModel extends AbstractTableModel {
        public List<List<String>> entries;

        MyTableModel() {
            entries = new ArrayList<List<String>>();
            for (int i=0; i<5; i++) {
                List<String> cols = new ArrayList<String>();
                cols.add("X" + i);
                cols.add("Y" + i);
                cols.add("Z" + i);
                entries.add(cols);
            }
        }

        @Override
        public int getRowCount() {
            return entries.size();
        }

        @Override
        public int getColumnCount() {
            return columnnames.length;
        }

        @Override
        public Object getValueAt(int r, int c) {
            List<? extends RowSorter.SortKey> rsk = table.getRowSorter().getSortKeys();
            System.out.println(rsk.size());
            return entries.get(r).get(c);
        }

        @Override
        public Class getColumnClass(int c) { 
            return String.class; 
        }

        @Override
        public String getColumnName(int c) { return columnnames[c]; }        

        @Override
        public boolean isCellEditable(int row, int col) { return false; }

    }

    public Tablecolumn() {
        scroll = new JScrollPane();
        model = new MyTableModel();
        table = new JTable();

        table.setModel(model);
        table.setAutoCreateRowSorter(true);
        table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.getTableHeader().setReorderingAllowed(false);
        table.setColumnSelectionAllowed(false);
        table.setRowSelectionAllowed(true);

        scroll.setViewportView(table);

        scroll.setMinimumSize(new Dimension(200, 200));
        scroll.setPreferredSize(new Dimension(800, 600));

        GroupLayout layout2 = new GroupLayout(this.getContentPane());
        this.setLayout(layout2);
        layout2.setVerticalGroup(
            layout2.createSequentialGroup()
                .addComponent(scroll)
        );
        layout2.setHorizontalGroup(
            layout2.createParallelGroup()
                .addComponent(scroll)
        );               

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        pack();
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Tablecolumn().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-06-03T13:16:58+00:00Added an answer on June 3, 2026 at 1:16 pm

    I think the problem is that if you use table.setAutoCreateRowSorter(true);, there’s a bug that makes myTable.getRowSorter().getSortKeys() always return an empty list. On the other hand, if you add a row sorter of your own, then it works.

    I needed to add this code:

    RowSorter<MyTableModel> sorter =
        new TableRowSorter<MyTableModel>(model);
    table.setRowSorter(sorter);
    

    Now, getSortKeys() will return a list of columns and their sort orders. That list is variable length, depending on which columns you’ve clicked, so you’ll have to iterate through the list looking at myTable.getRowSorter().getSortKeys().get(index).getColumn() and myTable.getRowSorter().getSortKeys().get(index).getSortOrder(), where the latter contains values of SortOrder.ASCENDING or SortOrder.DESCENDING.

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

Sidebar

Related Questions

I have a Jtable on which I called the method table1.setAutoCreateRowSorter(true); . So this
If you have JTable set with table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) and then you click drag on a
I have a JTable which contains a one column, cell render of each table
I have a jtable, everything works fine if I set the table data as
I have a jTable set so that it is read only, and selection is
I have a main controller class that shows a JFrame containing a JTable and,
I have a jTable with two columns.First column is set as Boolean(for checkbox) and
I have a JTable with few columns that are painted as checkboxes. What I
I have a JTable that I want to use to display some data (a
I have this JTable on my Swing app with the autoCreateRowSorter enabled. My table

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.