Solved

Add item to JList

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 81
MySQL  on Tomcat 8 46
type mismatch (Object[] to double[] 4 24
Java: How do I open the default windows program for a given file type 3 28
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

777 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