Results 1 to 6 of 6
  1. #1
    NoBullshit is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Problems refreshing a JList with its ListSelectionListener

    Hi! I'm new in the forum, so this is my "hello java-forums.org". First of all, thanks to all the people who make this space possible. And sorry about my English...

    I have a little (big annoying) problem with a JList and its proper listeners (ListSelectionListener). My intention is to have a JList that lists the contents of a directory. And I got it. I added some ListSelectionListeners (such as nodes in the directory) in order to do some actions when an item of the JList is clicked, each one of the items has an individual function. The thing is that the first time I charge the JList, it works correctly, or as I want it to do, BUT when I try to refresh the content of the JList, the listeners can't be implemented (at least by the previous way that just worked). Why the first time worked, and the second didn't work? I'm confused, I don't think it's a problem of threads, but the way I implement the listeners maybe... This is the code, not very long and a lot of stuff that is not relevant, but able to run:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.io.*;
     
     
    public class Prueba1 extends JFrame implements ListSelectionListener, ActionListener
    {
    JButton refrescar;
    JList lista;
    DefaultListModel listaPorDefecto;
    File directorio;
    GridBagConstraints c;
    Container contenedor = getContentPane();
     
    public Prueba1()
        {
        super("Prueba JList actualizar");
        contenedor.setLayout(new GridBagLayout());
        c = new GridBagConstraints();
     
    //We set the list with the content of the directory
        listaPorDefecto = new DefaultListModel();
        directorio = new File("C:\\users\\usuario\\desktop");
        String[] listaFicheros = directorio.list();
        for (int i=0; i<listaFicheros.length; i++)
            {listaPorDefecto.add(i, listaFicheros[i]);}
        lista = new JList(listaPorDefecto);
     
    //Here is the listener that NOW WORKS but won't work a bit later, when we press the JButton "refrescar" in order to refresh
        lista.addListSelectionListener(this);
     
    //Here the button "refrescar" and its listener
        refrescar = new JButton("Refrescar");
        refrescar.addActionListener(this);
     
    //We add the unique two elements of our JFrame
        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.BOTH;
        contenedor.add(lista, c);
     
        c.gridy++;
        contenedor.add(refrescar, c);
     
    //We set the window
        configurarVentana();
        }
     
    public void configurarVentana()
        {
        setVisible(true);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
     
    //The JList listener method
    public void valueChanged(ListSelectionEvent e)
        {
        System.out.println("Sí oye.");
        System.out.println("Fuente: " + e.getSource());
     
        if (e.getSource() == lista)
            {
            System.out.println("Le diste a: " + lista.getSelectedValue());
            System.out.println("Su índice es el: " + lista.getSelectedIndex());
            }
     
        }
     
    //The button's listener method
    public void actionPerformed(ActionEvent e)
        {
        System.out.println("Sí oye.");
        System.out.println("Fuente: " + e.getSource());
     
        if (e.getSource() == refrescar)
            {
            System.out.println("Escucha en resfrescar funcionando correctamente.");
            System.out.println("Procede al refresco.");
    
    // And here is we're I suppose I'm doing something wrong, but I don't know what... 
            lista.removeAll();
            lista.removeListSelectionListener(this);
            listaPorDefecto.removeAllElements();
     
            String[] listaFicheros = directorio.list();
            for(int i=0; i<listaFicheros.length; i++)
                {listaPorDefecto.add(i, listaFicheros[i]);}
     
            lista = new JList(listaPorDefecto);
     
            c.gridx = 0;
            c.gridy = 0;
            contenedor.add(lista, c);
     
    //And finally, this is THE LINE THAT DOESN'T WORK.
            lista.addListSelectionListener(this);
     
            System.out.println("Se supone que ahora está otra vez.");
            }
        }
     
    public static void main(String[]args)
        {Prueba1 p = new Prueba1();}
     
     
    }
    I've been trying hard, but nothing succed... Could anyone tell me why is this happening? What's wrong, if I do exactly the same that I did before (in the same code, I mean), but the second time doesn't work?
    Thank you anyway!

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Problems refreshing a JList with its ListSelectionListener

    Java Code:
    lista = new JList(listaPorDefecto);
    This just create a new JList in memory. It does not add the list to the GUI.

    Don't do this. If you want to change the data being displayed in the list then just change the model:

    Java Code:
    lista.setModel( listaPorDefecto );

  3. #3
    NoBullshit is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Problems refreshing a JList with its ListSelectionListener

    Thank you, camickr, but that is not the problem: it is about the listeners that should be added to the JList lista when you click the jbutton refrescar ("refreshing" in spanish). It is not the problem I'm asking about, but thank you, it is also an important point to know!

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Problems refreshing a JList with its ListSelectionListener

    You are adding the listener to an invisible JList.

  5. #5
    NoBullshit is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Problems refreshing a JList with its ListSelectionListener

    Oooohhh man! Thank you!!! I misundertood! Shame on me! Thank you a lot, I was becoming crazy with this piece of... problem xDD Seriously, thanks, camickr!! ;)

  6. #6
    LottieJam is offline Member
    Join Date
    Jul 2014
    Location
    Senegal
    Posts
    1
    Rep Power
    0

    Default 5 Things That Get one of the most powerful Anti-aging Items

    Nobody about the planet would ever choose to deal with the procedure of receiving older. It does not only make certain they are really feel significantly much more frail as time passes. nonetheless , the when youthful beauty they branched out would at some point develop into lost basically since the days pass. Having stated that, this genuinely can be a thing that is definitely significantly organic for everyone. If you'd prefer to decelerate aging, you'd have to have to possess to turn to a handful of products, by way of example using the very greatest anti-aging products inside the marketplace. If you'd like to possess folks comprehensive browsing lips as in case you had inside your early 20's, you'd go search to seek out the best lip plumper within the marketplace. Should you be going by way of a lot of stress within the workplace also as your eyes seem weary, you'll have the ability to just look for a dark dark below eyes therapy which might probably present you having a far more exuberant and stress-free look.

    Nonetheless, you will discover a few what particularly you will want to help keep in mind ahead of buying these issues. It truly is in fact finest to look for traits which will make them correctly suited for the troubles utilizing the whole course of action of aging. Listed beneath are five from the greatest components to appear for when you're looking for anti-aging points:

    >Anti Aging serum - Each and every single anti-aging resolution obtainable need to possess a appropriate dose from the element. It truly will be the 1 element that may be accountable for causing you to look a lot more youthful. This can include retinol and vit a as major elements, and could seriously aid make your skin elastic and smooth.

    >Peptides - These elements lead towards the proper charge of bovine collagen levels, as a result generating the skin look enhanced along with a lot additional youthful. With prevalent usage, the skin will begin searching much less wrinkly.

    >Moisturizer - premature aging is triggered out of the skin getting far as well dry. Irrespective of whether or not it does come to be dry, most likely to an excellent deal bigger possibility for seeing facial lines surface about the skin. An anti-aging item of top rated rated high top quality also requirements to function as a moisturizer in it to assist get rid of facial lines and provide that glow for the skin. It ought to also give enough defense against harsh climate situations.

    >Antioxidation - Anti-aging products ought to incorporate anti-oxidants to assist guard against any damaging radicals and chemical substances which enter our physiques just about every day. this could reverse any harm triggered for the skin, and aging will undoubtedly be slowed down lowered, so far as seems is worried.

    >Alpha Hydroxy - Several anti-aging factors that are recognized to become in the greatest high top quality include it. This genuinely can help you grow new skin even faster. As opposed to needing to cope with flaky, wrinkly old skin, you happen to be in a position to certainly create further skin cells with this distinct.

Similar Threads

  1. JList problems
    By grimx in forum New To Java
    Replies: 3
    Last Post: 01-13-2012, 06:55 AM
  2. performance problems with JList/default list model
    By prescott in forum AWT / Swing
    Replies: 3
    Last Post: 11-16-2011, 05:06 AM
  3. How to use ListSelectionListener in Jtable?
    By Vignesh Karthick in forum AWT / Swing
    Replies: 0
    Last Post: 02-10-2011, 06:01 AM
  4. JList displaying problems
    By D.Calladine in forum New To Java
    Replies: 0
    Last Post: 12-03-2010, 12:27 AM
  5. Refreshing JList - need help
    By Unsub in forum New To Java
    Replies: 1
    Last Post: 04-13-2010, 07:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •