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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:15:58+00:00 2026-05-15T13:15:58+00:00

I am working on a viewer, which uses a JList to show thumbnails of

  • 0

I am working on a viewer, which uses a JList to show thumbnails of the pages of a document. The user can open a page by selecting it through in the JList, or throught other mechanisms, like entering the number in a text box.

When using the latter alternative, I want that the JList also selects the page. I do this using setSelectedIndex(), but this triggers an event, which causes the page to be loaded again, as if the user had clicked and selected the page in the JList, and this is causing me some problems.

How I see it, the index should be set some way (perhaps in the model) so that only the UI of the JList updates, without firing an event that the index has changed.

Is this possible? Or is there a better way to solve my issue?

  • 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-15T13:15:59+00:00Added an answer on May 15, 2026 at 1:15 pm
    1. You can remove all ListSelectionListener from the list, make a selection and then add them again.

    2. You can create your own ListSelectionModel with a method that doesn’t throw the event and set it as a selection model to your JList, and then use getSelectionModel().yourSelectIndexMethod(index).

    3. You can also divert all your other methods of selection to the list, just find the corresponding entry if selecting the page by other means and select the item in the list. This way the item is selected and the page is loaded once.

    Code for option 2:

    public class ListTest extends JPanel{
    
    private static final String[] items = new String[]{"1", "2", "3"};
    private JList mylist;
    private JComboBox myCombo;
    private JTextArea myTA;
    
    public ListTest() {
        setLayout(new BorderLayout());
        myCombo = new JComboBox(items);
        myCombo.addActionListener(new ActionListener(){
    
            @Override
            public void actionPerformed(ActionEvent e){
                valueSelectedCombo(myCombo.getSelectedIndex());
            }
        });
        JPanel pn = new JPanel();
        pn.setLayout(new BoxLayout(pn, BoxLayout.X_AXIS));
        pn.add(myCombo);
        pn.add(Box.createHorizontalGlue());
        pn.add(new JButton(new AbstractAction("Clear"){
    
            @Override
            public void actionPerformed(ActionEvent e){
                myTA.setText("");
            }
        }));
        add(pn, BorderLayout.NORTH);
        add(new JScrollPane(getJList()), BorderLayout.WEST);
        add(new JScrollPane(myTA = new JTextArea()), BorderLayout.CENTER);
    }
    
    private void valueSelectedList(int index){
        myTA.setText(myTA.getText() + "\n" + items[index]);
    }
    
    private void valueSelectedCombo(int index){
        myTA.setText(myTA.getText() + "\n" + items[index]);
        ((CustomSelectionModel)mylist.getSelectionModel()).setSelectionSilent(index);
    }
    
    private JList getJList(){
        if (mylist == null){
            mylist = new JList(items);
            mylist.setSelectionModel(new CustomSelectionModel());
            mylist.addListSelectionListener(new ListSelectionListener(){
    
                @Override
                public void valueChanged(ListSelectionEvent e){
                    if (!e.getValueIsAdjusting()){
                        valueSelectedList(mylist.getSelectedIndex());
                    }
                }
            });
    
            mylist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            mylist.setPreferredSize(new Dimension(100, 106));
    
        }
        return mylist;
    }
    
    private static class CustomSelectionModel extends DefaultListSelectionModel{
    
        private boolean isSilent = false;
    
        public void setSelectionSilent(int firstIndex){
            isSilent = true;
            setSelectionInterval(firstIndex, firstIndex);
            isSilent = false;
        }
        protected void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting){
            if (isSilent){
                return;
            }
            super.fireValueChanged(firstIndex, lastIndex, isAdjusting);
        }
    }
    
    public static void main(String[] args){
        JFrame frame = new JFrame("test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        // Add content to the window.
        frame.add(new ListTest());
    
        // Display the window.
        frame.pack();
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a page ( http://oscarspad.co.uk/Gallery/ ) which uses the Galleria plugin to
An open-source project I am working on uses Visio drawings for documentation, which are
I'm working on an image viewer application (OSX) and currently it can load and
Which PE viewer application do you prefer to use when working with Win32 libraries?
I'm working on a image viewer that has a draggable scrollbar. When the user
I'm currently working on an intracompany web site which will be viewed on cell
I've googled around but couldn't find a working MongoDB viewer or data browser. An
I'm working on a album viewer. At the top I want a horizontal container
I am working on a GPS data logger and Map viewer application using Microsoft
Part of an app I am working on includes a log file viewer, with

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.