Solved

Add item to JList

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Way to decrease size of apk file 9 88
ejb wildfly example 2 29
how to debug htl and js pages 8 39
Need Help! Getting a syntax error and don't understand why 3 31
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

821 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