Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to reset JList?

Posted on 2007-03-31
6
2,095 Views
Last Modified: 2008-02-01
how to clear all items inside a JList?

I have tried using

trackList.removeAll(()

but does no effect.

I also tried using code below:

Object[] blank = {""};
trackList.setListData(blank);

.. it will clear up, but change this list as if setVisibleRowCount = 1.

Thanks & regards


0
Comment
Question by:isuhendro
  • 4
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
ADSLMark earned 125 total points
ID: 18828619
Here is an example application of JList (hand-made!):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Listing
    extends JFrame
    implements ActionListener
{
    private JButton butAdd;
    private JButton butClear;
    private JList list;
    private DefaultListModel model;
    private int num;

    public Listing()
    {
        this.setSize(640,480);
        this.setLayout(new FlowLayout());
        this.setTitle("List Example");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.num = 1;

        this.model = new DefaultListModel();

        this.butClear = new JButton("Clear!");
        this.butClear.addActionListener(this);

        this.butAdd = new JButton("Add");
        this.butAdd.addActionListener(this);

        this.list = new JList(model);

        this.add(list);
        this.add(butAdd);
        this.add(butClear);
    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() == this.butClear)
        {
            this.model.clear();
        }
        else if(ae.getSource() == this.butAdd)
        {
            this.model.add(this.model.getSize(),"Track "+this.num);
            this.num++;
        }
    }

    public static void main(String[] args)
    {
        new Listing().setVisible(true);
    }
}


As you can see, it works with a DefaultListModel, where you have a clear() method to clear the list.

Mark
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18828621
I also have a probable cause of your problem, the GUI doesn't get updated after a "removeAll()", so maybe all the elements are removed, but the GUI doesn't know yet. It might work if you do a repaint().

Mark
0
 

Author Comment

by:isuhendro
ID: 18828727
hello Mark,

thanks for your reply. I prefer not to using model for this JList, otherwise will need to change codes at other part.

btw repaint() will not help as well..
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 10

Assisted Solution

by:ADSLMark
ADSLMark earned 125 total points
ID: 18828754
Well, then you are bound to the solution you mentioned:

this.data = new String[]{};
this.list.setListData(this.data);

Set the list data to be empty. Don't forget to also set your real data holder (if it's an attribute of your class) to empty, otherwise when you reuse the variable to reset the listdata, it will still contain the old values.

Mark
0
 

Author Comment

by:isuhendro
ID: 18830731
ok then Mark... just found out that the problem with setVisibleCountRow become 1, is related to the BorderLayout... not to the list itself.. so i will use previous solution. Btw what is the advantage of using DefaultListModel ?
Thanks & regards.
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18831254
Better design, more functionality for free.
Take a look at the model-view-controller design pattern. You have one model with maybe multiple views and multiple controllers. This way the model/view/controller are separated from each other which makes extending it easy etc.
Many advantages by using established design patterns.. anyway, it's not necessary. :-)

Mark
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 56
servlet example issue 6 46
tomcat startup error 5 102
ejb mdb examples 1 8
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question