Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #4383

Re: Sharing ListSelection

From "Stefan Rybacki" <stefan.rybacki@THRWHITE.remove-dii-this>
Subject Re: Sharing ListSelection
Message-ID <48f8ec9b@news.uni-rostock.de> (permalink)
Newsgroups comp.lang.java.gui
References <nospam-087E52.13401417102008@news.motzarella.org>
Date 2011-04-27 15:50 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
John B. Matthews schrieb:
> In article <48f8a154$1@news.uni-rostock.de>,
>  Stefan Rybacki <noemail@noemail.foobar> wrote:
> 
>...
> 
> How many ListSelectionListener(s) are returned by your 
> DefaultListSelectionModel's getListSelectionListeners() method?
> 

Here is a little example that illustrates what is happening.

  public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JList list1 = new JList();
    JList list2 = new JList();

    DefaultListSelectionModel selectionModel = new
DefaultListSelectionModel();
    final DefaultListModel model = new DefaultListModel();

    JButton addButton=new JButton("Add Item");
    addButton.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        model.add(0, "AddedItem");
      }

    });

    frame.add(list1, BorderLayout.WEST);
    frame.add(list2, BorderLayout.EAST);
    frame.add(addButton, BorderLayout.SOUTH);

    list1.setModel(model);
    list2.setModel(model);
    list1.setSelectionModel(selectionModel);
    list2.setSelectionModel(selectionModel);

    for (int i=0;i<10;i++)
      model.addElement(String.format("Item %d", i));

    SwingUtilities.invokeLater(new Runnable() {

      @Override
      public void run() {
        frame.setSize(640, 480);
        frame.setVisible(true);
      }

    });
  }

Just select an item in either list and vice versa, the selection is
applied to the other list as well.
But after clicking "Add Item" you can see the selection moving to the
item above the previously selected due to the use of two lists on one
selection model. Which again, is right behaviour in the sense of the
lists adapting model changes to the selection model. But can anybody
think of an elegant way to solve this?

Stefan

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Find similar


Thread

Sharing ListSelectionMode "Stefan Rybacki" <stefan.rybacki@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
  Re: Sharing ListSelection "Sabine Dinis Blochberger" <sabine.dinis.blochberger@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
    Re: Sharing ListSelection "Stefan Rybacki" <stefan.rybacki@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
      Re: Sharing ListSelection "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
        Re: Sharing ListSelection "Stefan Rybacki" <stefan.rybacki@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
          Re: Sharing ListSelection "John B. Matthews" <john.b..matthews@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
            Re: Sharing ListSelection "Stefan Rybacki" <stefan.rybacki@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
              Re: Sharing ListSelection "John B. Matthews" <john.b..matthews@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
                Re: Sharing ListSelection "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
                Re: Sharing ListSelection "Stefan Rybacki" <stefan.rybacki@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
                Re: Sharing ListSelection "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
                Re: Sharing ListSelection "Stefan Rybacki" <stefan.rybacki@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
                Re: Sharing ListSelection "John B. Matthews" <john.b..matthews@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
                Re: Sharing ListSelection "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000
            Re: Sharing ListSelection "Stefan Rybacki" <stefan.rybacki@THRWHITE.remove-dii-this> - 2011-04-27 15:50 +0000

csiph-web