Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.glorb.com!news-out.readnews.com!transit3.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail From: "Stefan Rybacki" Subject: Re: Sharing ListSelection Message-ID: <48f8ec9b@news.uni-rostock.de> X-Comment-To: comp.lang.java.gui Newsgroups: comp.lang.java.gui In-Reply-To: References: Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92] Lines: 72 Date: Wed, 27 Apr 2011 15:50:17 GMT NNTP-Posting-Host: 96.60.20.240 X-Complaints-To: news@tds.net X-Trace: newsreading01.news.tds.net 1303919417 96.60.20.240 (Wed, 27 Apr 2011 10:50:17 CDT) NNTP-Posting-Date: Wed, 27 Apr 2011 10:50:17 CDT Organization: TDS.net Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.gui:4383 To: comp.lang.java.gui John B. Matthews schrieb: > In article <48f8a154$1@news.uni-rostock.de>, > Stefan Rybacki 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