Solved

How to reset JList?

Posted on 2007-03-31
6
2,207 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 77
SHA2 certs for IIS AND Java? 2 136
Facing this issue for maven proxy setting 2 45
Why doesn't this text field show up on my Applet frame? 2 43
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
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 …
This video teaches viewers about errors in exception handling.
Suggested Courses

752 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