Solved

How to reset JList?

Posted on 2007-03-31
6
1,977 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now