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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:22:40+00:00 2026-05-22T21:22:40+00:00

Since JTable could be sorted. How do I set it auto select the last

  • 0

Since JTable could be sorted.
How do I set it auto select the last inserted row whenever a row is inserted?

Thanks

  • 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-22T21:22:41+00:00Added an answer on May 22, 2026 at 9:22 pm

    your TableModel can retun number of rows, and new/added could be last by using convertRowIndexToView you can get row in TableView, then pass to the changeSelection
    public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)

    EDIT: example (for enjoy harcoded numbersOfRows – 3 instead of correct numbersOfRows – 1):

    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);
                                TableModel tblModel = table.getModel();
                                int numbersOfRows = 0;
                                numbersOfRows = tblModel.getRowCount();
                                int lastRow = 0;
                                lastRow = table.convertRowIndexToView(numbersOfRows - 3);
                                table.changeSelection(lastRow, 0, false, false);
                            } 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

I have a Jtable and I want to highlight a row by adding a
Trying to order a jtable by the Days of week column since the day
If you have JTable set with table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) and then you click drag on a
I have a uploader program that has a JTable displaying a set of rows
Since I started studying object-oriented programming, I frequently read articles/blogs saying functions are better,
Since CS3 doesn't have a web service component, as previous versions had, is there
Since both a Table Scan and a Clustered Index Scan essentially scan all records
Since Graduating from a very small school in 2006 with a badly shaped &
Since the WMI class Win32_OperatingSystem only includes OSArchitecture in Windows Vista, I quickly wrote
Since debate without meaningful terms is meaningless , I figured I would point at

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.