Solved

How to reset JList?

Posted on 2007-03-31
6
2,153 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
Industry Leaders: 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
use lov values 2 72
Tomcat: Unable to run tomcat service. 2 29
going to wrong jsp page 2 47
batch vs regular insert in spring DAO 2 22
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

749 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