Solved

Add item to JList

Posted on 2006-11-09
1
1,214 Views
Last Modified: 2010-07-27
As I'm learning Java, I'm having a little difficulty with the JList control. I'd like to know how to add/remove items dynamically at runtime, and have seen this example posted:

// create a model
DefaultListModel items = new DefaultListModel();

// create JList
JList list = new JList(items);

// add/remove items
items.add(0, "Item 1") // inserts into first position
items.addElement("Last Item"); // adds to end of list
items.remove(0); // remove first item
items.removeAllElements() // remove everything

However, my existing JList control that is already on the form is called jList1, and I'm curious how I'd make this above code work with my existing jList control. My first problem is I have to change DefaultListModel to javax.swing.DefaultListModel in order for it to compile, and I'm not sure where I'm allowed to place the preceeding example code (I'm wanting to place it in my button's click event).

Thanks for any help!
0
Comment
Question by:mk553
1 Comment
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 17910611
jList1.setModel(items);

> My first problem is I have to change DefaultListModel to javax.swing.DefaultListModel

add import:

import javax.swing.*;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 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

15 Experts available now in Live!

Get 1:1 Help Now