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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:28:46+00:00 2026-05-22T20:28:46+00:00

Is there any method in JTable (or in a related class) that is called

  • 0

Is there any method in JTable (or in a related class) that is called when user click on the column header to sort the relative column?
I would like to perform some operations when a user click happens on a column header, before that the sorting operation begin. is that possible?

  • 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-22T20:28:47+00:00Added an answer on May 22, 2026 at 8:28 pm

    javax.swing.RowFilter

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    import javax.swing.UIManager.LookAndFeelInfo;
    import javax.swing.event.*;
    import javax.swing.table.*;
    
    public class TestTableRowTable {
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                            if (info.getName().equals("Nimbus")) {
                                UIManager.setLookAndFeel(info.getClassName());
                                break;
                            }
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
    
                    final JTable table = new JTable(10, 6);
                    for (int i = 0; i < table.getRowCount(); i++) {
                        table.setValueAt(i, i, 0);
                    }
                    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
                    table.setRowSorter(sorter);
                    final AbstractTableModel model = new AbstractTableModel() {
    
                        private static final long serialVersionUID = 1L;
    
                        @Override
                        public int getColumnCount() {
                            return 1;
                        }
    
                        @Override
                        public Object getValueAt(int row, int column) {
                            return table.convertRowIndexToModel(row);
                        }
    
                        @Override
                        public int getRowCount() {
                            return table.getRowCount();
                        }
                    };
                    JTable headerTable = new JTable(model);
                    headerTable.setShowGrid(false);
                    headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                    headerTable.setPreferredScrollableViewportSize(new Dimension(30, 0));
                    headerTable.getColumnModel().getColumn(0).setPreferredWidth(30);
                    headerTable.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
    
                        @Override
                        public Component getTableCellRendererComponent(JTable x, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    
                            boolean selected = table.getSelectionModel().isSelectedIndex(row);
                            Component component = table.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(table, value, false, false, -1, -2);
                            ((JLabel) component).setHorizontalAlignment(JLabel.CENTER);
                            if (selected) {
                                component.setFont(component.getFont().deriveFont(Font.BOLD));
                            } else {
                                component.setFont(component.getFont().deriveFont(Font.PLAIN));
                            }
                            return component;
                        }
                    });
                    table.getRowSorter().addRowSorterListener(
                            new RowSorterListener() {
    
                                @Override
                                public void sorterChanged(RowSorterEvent e) {
                                    model.fireTableDataChanged();
                                }
                            });
                    table.getSelectionModel().addListSelectionListener(
                            new ListSelectionListener() {
    
                                @Override
                                public void valueChanged(ListSelectionEvent e) {
                                    model.fireTableRowsUpdated(0, model.getRowCount() - 1);
                                }
                            });
                    JScrollPane pane = new JScrollPane(table);
                    pane.setRowHeaderView(headerTable);
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.getContentPane().add(pane);
                    frame.getContentPane().add(new JButton(new AbstractAction("Toggle filter") {
    
                        private static final long serialVersionUID = 1L;
                        private RowFilter<TableModel, Object> filter = new RowFilter<TableModel, Object>() {
    
                            @Override
                            public boolean include(javax.swing.RowFilter.Entry<? extends TableModel, ? extends Object> entry) {
                                return ((Number) entry.getValue(0)).intValue() % 2 == 0;
                                //return ((String) entry.getValue(0)).length()>0 ;
                                //return ((Date) entry.getValue(0)).getTime()< ;
                            }
                        };
    
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            if (sorter.getRowFilter() != null) {
                                sorter.setRowFilter(null);
                            } else {
                                sorter.setRowFilter(filter);
                            }
                        }
                    }), BorderLayout.PAGE_END);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        private TestTableRowTable() {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any method to tell the user that his score submitted successfully or
Is there any Visual Studio Add-In that can do the remove method refactoring? Suppose
Is there any tricky way to use Java reserved words as variable, method, class,
Is there any method (that doesn't use loop or recursion) to create and fill
Is there any method (that doesn't use loop or recursion) to create and fill
I want to inherit more than one class is there any method? For instance
Is there any method that shows if the image is in landscape orientation or
Is there any method? My computer is AMD64. ::std::string str; BOOL loadU(const wchar_t* lpszPathName,
Is there any method to generate MD5 hash of a string in Java?
Is there any JavaScript method similar to the jQuery delay() or wait() (to delay

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.