Solved

Add item to JList

Posted on 2006-11-09
1
1,209 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi challenge 25 85
Python Assistance 7 36
Starting to learn JAVA, 7 48
JList custom Cell Renderer refresh 15 43
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

747 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