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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:03:31+00:00 2026-06-07T23:03:31+00:00

I am relativly new to Java, and especially to swing. I develop with Netbeans

  • 0

I am relativly new to Java, and especially to swing. I develop with Netbeans 6.8.

I have a JList that uses a custom AbstractListModel. This AbstractListModel tracks changes to the data in a database, and calls fireIntervalAdded when a new element is to be added to the list. This works perfectly.

The problem I am having is that I would like the JList (or JScrollPane) to automaticly scroll to the new element in the list. I have read that I could use JList.ensureIndexIsVisible, but I do not know how to get the reference to the instance of the JList from its associated ListModel (since I am fireing the ‘fireIntervalAdded’ from within the ListModel).

To explain a bit, I have built a poller within the ListModel to poll new data from an SQL server. Since I consider the ListModel as the “data source” of the list, it made sence to put the data retreival (and polling in my case) logic inside this ListModel. The tutorials do not seem to cover this specific need for control scrolling from the Model itself (which sort-of makes sense since the ListModel could be used in multiple JList at once…..)

What can-I do to make the JList scroll to the last created element from within the ListModel code?

Also, I imagine that I could fix my problem if I could find an event in the JList that would be fired when a new element is added, but I have found none!

Thanks

— EDIT —

Ok, base on MadProgrammer’s response, here is what I built:

public class JListDataListener implements ListDataListener {
    JList _listToControl = null;
    public JListDataListener(JList listToControl) {
        this._listToControl = listToControl;
        this._listToControl.getModel().addListDataListener(this);
    }

    public void intervalAdded(final ListDataEvent e) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                _listToControl.ensureIndexIsVisible(e.getIndex1() + 1);
                            }
                        });
    }

    public void intervalRemoved(ListDataEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void contentsChanged(ListDataEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}
  • 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-07T23:03:33+00:00Added an answer on June 7, 2026 at 11:03 pm

    Personally, I wouldn’t try & change the ui directly from a model like this. You are better of attaching another listener to the model with a reference to the list in question, this makes the solution reusable.

    I might be tempted to extend jlist & use a specialised inner listener, but that comes down to your requirements

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

Sidebar

Related Questions

I'm relatively new to Java and would appreciate any help on this! I have
I'm relatively new to java (specifically swing) and have recently been making some fairly
I have a custom class that I've written that extends ImageView (for Android Java).
Like lots of askers on SO, I'm relatively new to java and have attempted
I'm relative new to C++ and my background is in Java. I have to
I'm relatively new to Java and am used to generics in C# so have
I'm relatively new to Java, eclipse and android so this could be a completely
I'm relatively new to java and android development so sorry if this is a
I am relatively new to programming and especially to Java... So it takes me
I'm relatively new to concurrency in Java (still have yet to read JCIP, but

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.