Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to reset JList?

Posted on 2007-03-31
6
Medium Priority
?
2,399 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 375 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Assisted Solution

by:ADSLMark
ADSLMark earned 375 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

715 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