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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:10:22+00:00 2026-06-03T09:10:22+00:00

I’ve got a JTable that updates frequently. It sits inside of a scroll pane.

  • 0

I’ve got a JTable that updates frequently. It sits inside of a scroll pane.

Occasionally we’ll see a message that we’d like to dig deeper into. Unfortunately due to the number of updates the message will scroll off the visible screen before we can finish looking at it.

I’d like to be able to freeze the viewport/scrolling while we examine the message but still allow the table model to update with new messages.

I can get the selected row to go to the top with this code:

        int firstSelectedRow = table.getSelectedRow();
        Rectangle rowLocation = table.getCellRect(firstSelectedRow, 0, false);
        scroll.getVerticalScrollBar().setValue(rowLocation.y);
        freezeScrolling.setText("Resume Updates");

but that only happens on button press and then it quickly sscrolls away.

Is there a way to tell the viewport/scroll pane to freeze on a selection and the to turn it off so that the scroll pane updates as you’d expect?

  • 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-03T09:10:24+00:00Added an answer on June 3, 2026 at 9:10 am

    OK, this one teased me a lot, so I spent some time to find a way for this. I found two options:

    1. The one I prefer because I find it much simpler: block the new entries to the table model and store them in a buffer. Whenever we unfreeze, we flush the buffer to the table model.
    2. The second option consists into locating the current top visible row. If the user scrolls, we capture the top visible row. As the model updates, we locate the captured row in the table and we set the view port position to that.

    Here is an SSCCE that illustrates both mechanisms (don’t mind the crapiness of the code design, I tested a bunch of stuffs — extending the JScrollPane is actually not a good idea, it can all be done externally).

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.AdjustmentListener;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.Vector;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.event.TableModelEvent;
    import javax.swing.event.TableModelListener;
    import javax.swing.table.DefaultTableModel;
    
    public class Tables {
    
        private static class JScrollPaneExtension extends JScrollPane implements TableModelListener, AdjustmentListener {
            private boolean frozen = false;
            private Vector<String> rowData;
            private final JTable table;
            private final MyTableModel tableModel;
    
            private JScrollPaneExtension(JTable table, MyTableModel tableModel) {
                super(table);
                this.table = table;
                this.tableModel = tableModel;
                tableModel.addTableModelListener(this);
                getVerticalScrollBar().addAdjustmentListener(this);
            }
    
            public boolean isFrozen() {
                return frozen;
            }
    
            public void setFrozen(boolean frozen) {
                if (frozen != this.frozen) {
                    this.frozen = frozen;
                    if (frozen) {
                        captureCurrentTopRowData();
                    } else {
                        rowData = null;
                    }
                }
            }
    
            private void captureCurrentTopRowData() {
                int row = table.rowAtPoint(getViewport().getViewPosition());
                if (row > -1) {
                    rowData = (Vector<String>) tableModel.getDataVector().get(row);
                }
            }
    
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                if (frozen) {
                    captureCurrentTopRowData();
                    scrollToRowData();
                }
            }
    
            @Override
            public void tableChanged(TableModelEvent e) {
                scrollToRowData();
            }
    
            private void scrollToRowData() {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        if (frozen) {
                            int index = tableModel.getDataVector().indexOf(rowData);
                            getViewport().setViewPosition(table.getCellRect(index, 0, true).getLocation());
                        }
                    }
                });
            }
    
        }
    
        public static class MyTableModel extends DefaultTableModel {
            private int count;
    
            private boolean frozen = false;
    
            private List<Vector<String>> buffer = new ArrayList<Vector<String>>();
    
            public MyTableModel() {
                addColumn("Test");
            }
    
            public void insertNewRow() {
                Vector<String> rowData = createNewRowData();
                if (isFrozen()) {
                    buffer.add(rowData);
                } else {
                    insertRow(0, rowData);
                }
            }
    
            private Vector<String> createNewRowData() {
                Vector<String> data = new Vector<String>(1);
                data.add("Hello-" + (count++));
                return data;
            }
    
            public boolean isFrozen() {
                return frozen;
            }
    
            public void setFrozen(boolean frozen) {
                if (frozen == this.frozen) {
                    return;
                }
                this.frozen = frozen;
                if (!frozen) {
                    flushBuffer();
                }
            }
    
            private void flushBuffer() {
                for (Vector<String> rowData : buffer) {
                    insertRow(0, rowData);
                }
            }
        }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            final MyTableModel model = new MyTableModel();
            JTable table = new JTable(model);
            final JScrollPaneExtension scroll = new JScrollPaneExtension(table, model);
            JPanel panel = new JPanel();
            final JButton freeze = new JButton("Freeze");
            freeze.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    if (model.isFrozen()) {
                        freeze.setText("Freeze model");
                    } else {
                        freeze.setText("Continue");
                    }
                    model.setFrozen(!model.isFrozen());
                }
            });
            final JButton freeze2 = new JButton("Freeze scroll");
            freeze2.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    if (scroll.isFrozen()) {
                        freeze2.setText("Freeze scroll");
                    } else {
                        freeze2.setText("Resume scroll");
                    }
                    scroll.setFrozen(!scroll.isFrozen());
                }
            });
            scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            frame.add(scroll);
            panel.add(freeze);
            panel.add(freeze2);
            frame.getContentPane().add(panel, BorderLayout.NORTH);
            frame.pack();
            frame.setVisible(true);
            Timer t = new Timer();
            t.scheduleAtFixedRate(new TimerTask() {
    
                @Override
                public void run() {
                    SwingUtilities.invokeLater(new Runnable() {
    
                        @Override
                        public void run() {
                            model.insertNewRow();
                        }
                    });
                }
            }, new Date(), 300);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

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.