Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: blmblm@myrealbox.com Newsgroups: comp.lang.java.programmer Subject: Re: problems with editable JList Date: 17 Feb 2014 20:02:20 GMT Organization: None Lines: 123 Message-ID: References: X-Trace: individual.net 8YYkaGmnH58FoRF2OO8ftgKJdoE8DF+ONEDjZwSQ69pF+C3nqg X-Orig-Path: not-for-mail Cancel-Lock: sha1:+TDT8fm2bzA0jHv8wWsWOl90hhg= X-Newsreader: trn 4.0-test76 (Apr 2, 2001) Xref: csiph.com comp.lang.java.programmer:27538 In article , markspace wrote: > On 2/17/2014 7:53 AM, blmblm@myrealbox.com wrote: > > > What I want: > > > > After adding a new element, the new element is selected. > > ... > > How I'm trying to accomplish this: > > > > In my extension of JList, I attach a ListDataListener to the list > > data model, and in the listener's methods that deal with addition and > > removal events I use setSelectedIndex() or clearSelection() to change > > > I think your event listeners are tripping you up. They're probably > firing twice, or continuously until an error happens, after each time > you try to modify the list including modifications by the event listener > itself. I'd have said not -- based on output of the even-more-debug-prints version I'm reasonably confident that I'm not getting that kind of recursion/loop, and anyway I only modify the selection in code to process add/remove events, rather than in .... But then again, both kinds of events (add/remove and selection changes) are handled by code in plaf.basic.BasicListUI, and it does something that, judging by comments in its code, is meant to avoid what you're describing. Be that as it may, though: The big difference between your code and mine is that you moved the code to set the selection into the button event listeners. I don't know why I didn't think of that, but when I take that approach .... Success!! I'll post updated code later .... Real life is interfering right now! Thanks!!!! > > I made a simple program to modify a list, and it works fine. I didn't > use any event listeners. Try starting with this, see if you can use the > same template to get "Remove" working. "Add" seems to work fine. > > > /* > * To change this license header, choose License Headers in Project > Properties. > * To change this template file, choose Tools | Templates > * and open the template in the editor. > */ > package quicktest; > > import java.awt.BorderLayout; > import java.awt.event.ActionEvent; > import java.awt.event.ActionListener; > import javax.swing.Box; > import javax.swing.DefaultListModel; > import javax.swing.JButton; > import javax.swing.JFrame; > import javax.swing.JList; > import javax.swing.SwingUtilities; > > /** > * > * @author Brenden Towey > */ > public class JListTest > { > > public static void main( String[] args ) > { > SwingUtilities.invokeLater( new Runnable() > { > public void run() > { > JFrame frame = new JFrame(); > > final DefaultListModel listModel = new DefaultListModel(); > Object[] test = { "One", "Two", "Three" }; > for( Object li : test ) > listModel.addElement( li ); > > final JList list = new JList( listModel ); > frame.add( list ); > > Box vbox = Box.createVerticalBox(); > JButton addButton = new JButton( "Add" ); > vbox.add( addButton ); > JButton removeButton = new JButton( "Remove" ); > vbox.add( removeButton ); > frame.add( vbox, BorderLayout.EAST ); > > addButton.addActionListener( new ActionListener() > { > int count = 0; > @Override > public void actionPerformed( ActionEvent e ) > { > listModel.addElement( "Add "+ ++count ); > list.setSelectedIndex( listModel.getSize()-1 ); > } > } ); > > frame.pack(); > frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); > frame.setSize( 350, 350 ); > frame.setLocationRelativeTo( null ); > frame.setVisible( true ); > } > } ); > } > } > > -- B. L. Massingill ObDisclaimer: I don't speak for my employers; they return the favor.