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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:51:28+00:00 2026-05-24T15:51:28+00:00

Possible Duplicate: JTable Scrolling to a specified row index I have a JTable and

  • 0

Possible Duplicate:
JTable Scrolling to a specified row index

I have a JTable and I programmatically need to select a row by using this code:

  myTable.setRowSelectionInterval(i, j);

(where i and j are valid row and column numbers respectively).

The problem is, when you jump to a row, the JScrollPane does not move.
In this case, the table is quite long, and often the “selected row” is not visible on the screen, so the user has to stay scrolling up/down manually to find it. I would like to know how I can make the JScrollPane automatically jump to the specific location of the row.

Edit: Found this one liner which can do it:

table.scrollRectToVisible(table.getCellRect(row,0, 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-05-24T15:51:29+00:00Added an answer on May 24, 2026 at 3:51 pm

    just extends post by @Eng.Fouad +1, no works as I exactly expected (with kind help by StanislavL from another Java Swing forum)

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.DefaultTableModel;
    
    public class TableSelectionGood implements ListSelectionListener {
    
        private JTable[] tables;
        private boolean ignore = false;
    
        public TableSelectionGood() {
            Object[][] data1 = new Object[100][5];
            Object[][] data2 = new Object[50][5];
            Object[][] data3 = new Object[50][5];
            for (int i = 0; i < data1.length; i++) {
                data1[i][0] = "Company # " + (i + 1);
                for (int j = 1; j < data1[i].length; j++) {
                    data1[i][j] = "" + (i + 1) + ", " + j;
                }
            }
            for (int i = 0; i < data2.length; i++) {
                data2[i][0] = "Company # " + ((i * 2) + 1);
                for (int j = 1; j < data2[i].length; j++) {
                    data2[i][j] = "" + ((i * 2) + 1) + ", " + j;
                }
            }
            for (int i = 0; i < data3.length; i++) {
                data3[i][0] = "Company # " + (i * 2);
                for (int j = 1; j < data3[i].length; j++) {
                    data3[i][j] = "" + (i * 2) + ", " + j;
                }
            }
            String[] headers = {"Col 1", "Col 2", "Col 3", "Col 4", "Col 5"};
            DefaultTableModel model1 = new DefaultTableModel(data1, headers);
            DefaultTableModel model2 = new DefaultTableModel(data2, headers);
            DefaultTableModel model3 = new DefaultTableModel(data3, headers);
            final JTable jTable1 = new JTable(model1);
            jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            final JScrollPane sp1 = new JScrollPane();
            sp1.setPreferredSize(new Dimension(600, 200));
            sp1.setViewportView(jTable1);
            final JTable jTable2 = new JTable(model2);
            jTable2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            final JScrollPane sp2 = new JScrollPane();
            sp2.setPreferredSize(new Dimension(600, 200));
            sp2.setViewportView(jTable2);
            final JTable jTable3 = new JTable(model3);
            jTable3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            final JScrollPane sp3 = new JScrollPane();
            sp3.setPreferredSize(new Dimension(600, 200));
            sp3.setViewportView(jTable3);
            TableSelectionGood tableSelection = new TableSelectionGood(jTable1, jTable2, jTable3);
            JPanel panel1 = new JPanel();
            panel1.setLayout(new GridLayout(3, 0, 10, 10));
            panel1.add(sp1);
            panel1.add(sp2);
            panel1.add(sp3);
            JFrame frame = new JFrame("tableSelection");
            frame.add(panel1);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    
        public TableSelectionGood(JTable... tables) {
            for (JTable table : tables) {
                table.getSelectionModel().addListSelectionListener(this);
            }
            this.tables = tables;
        }
    
        private JTable getTable(Object model) {
            for (JTable table : tables) {
                if (table.getSelectionModel() == model) {
                    return table;
                }
            }
            return null;
        }
    
        private void changeSelection(JTable table, String rowKey) {
            int col = table.convertColumnIndexToView(0);
            for (int row = table.getRowCount(); --row >= 0;) {
                if (rowKey.equals(table.getValueAt(row, col))) {
                    table.changeSelection(row, col, false, false);
                    return;
                }
            }
            table.clearSelection();
        }
    
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting() || ignore) {
                return;
            }
            ignore = true;
            try {
                JTable table = getTable(e.getSource());
                int row = table.getSelectedRow();
                String rowKey = table.getValueAt(row, table.convertColumnIndexToView(0)).toString();
                for (JTable t : tables) {
                    if (t == table) {
                        continue;
                    }
                    changeSelection(t, rowKey);
                    JViewport viewport = (JViewport) t.getParent();
                    Rectangle rect = t.getCellRect(t.getSelectedRow(), 0, true);
                    Rectangle r2 = viewport.getVisibleRect();
                    t.scrollRectToVisible(new Rectangle(rect.x, rect.y, (int) r2.getWidth(), (int) r2.getHeight()));
                    System.out.println(new Rectangle(viewport.getExtentSize()).contains(rect));
                }
            } finally {
                ignore = false;
            }
        }
    
        public static void main(String[] args) {
            TableSelectionGood tableSelection = new TableSelectionGood();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: or is not valid C++ : why does this code compile ?
Possible Duplicate: comparing contents of two files using python I have a file name
Possible Duplicate: Using a javascript variable to set PHP variable I need to do
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: How to detect if JavaScript is disabled? I have a website which
Possible Duplicate: Difference between Convert.tostring() and .tostring() Hi Carrying on from this question What
Possible Duplicate: Overloading += in c++ Do I need to overload the += operator
Possible Duplicate: Lock the Android device programmatically I want to lock the screen through

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.