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


Groups > comp.lang.java.programmer > #27536

Re: problems with editable JList

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From markspace <markspace@nospam.nospam>
Newsgroups comp.lang.java.programmer
Subject Re: problems with editable JList
Date Mon, 17 Feb 2014 10:33:33 -0800
Organization A noiseless patient Spider
Lines 92
Message-ID <ldtkm0$vlu$1@dont-email.me> (permalink)
References <bmepjbFoa1fU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Mon, 17 Feb 2014 18:33:36 +0000 (UTC)
Injection-Info mx05.eternal-september.org; posting-host="9358cda03e1e0dfd3c4b757daa608707"; logging-data="32446"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/vaobRBNeugLhXFTLb05+3uaJ35hkEpNI="
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0
In-Reply-To <bmepjbFoa1fU1@mid.individual.net>
Cancel-Lock sha1:ZZx/1ma2hqaPx26xa8Xgb4ACMZs=
Xref csiph.com comp.lang.java.programmer:27536

Show key headers only | View raw


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 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 );
          }
       } );
    }
}

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

problems with editable JList blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2014-02-17 15:53 +0000
  Re: problems with editable JList markspace <markspace@nospam.nospam> - 2014-02-17 10:33 -0800
    Re: problems with editable JList blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2014-02-17 20:02 +0000
      Re: problems with editable JList markspace <markspace@nospam.nospam> - 2014-02-17 12:19 -0800
        Re: problems with editable JList Lew <lewbloch@gmail.com> - 2014-02-18 02:31 -0800
          Re: problems with editable JList blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2014-02-18 12:46 +0000
            Re: problems with editable JList Lew <lewbloch@gmail.com> - 2014-02-18 16:07 -0800
              Re: problems with editable JList blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2014-02-19 14:10 +0000
        Re: problems with editable JList blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2014-02-18 12:39 +0000

csiph-web