473,796 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Very frustrating problem with JList

10 New Member
Hi, here is some of my code

public class ControlIL extends JFrame implements ActionListener, ListSelectionLi stener {

private JList Items;

//builds the UI
public ControlIL(){

DefaultListMode l model = new DefaultListMode l();
JList Items = new JList(model);
}



I want to be able to add to the List when a certain button I have is pressed, Im not new to Java, i know how to add to the List using a vector but it only works when I add to the List within the ControlIL function, however, outside of the ControlIL function(where I have all my actionPerformed code and other logic and functions, anytime I try to add to the list using Items.setListDa ta(vector), that does not work, I get null pointer excep.; and when I try model._____ nothing even pops up as a possibility for the use of model, the variable cannot be found.

Any help would be greatly appreciated!

Thank You!
Apr 22 '08 #1
2 1961
sukatoa
539 Contributor
Hi, here is some of my code

public class ControlIL extends JFrame implements ActionListener, ListSelectionLi stener {

private JList Items;

//builds the UI
public ControlIL(){

DefaultListMode l model = new DefaultListMode l();
JList Items = new JList(model);
}



I want to be able to add to the List when a certain button I have is pressed, Im not new to Java, i know how to add to the List using a vector but it only works when I add to the List within the ControlIL function, however, outside of the ControlIL function(where I have all my actionPerformed code and other logic and functions, anytime I try to add to the list using Items.setListDa ta(vector), that does not work, I get null pointer excep.; and when I try model._____ nothing even pops up as a possibility for the use of model, the variable cannot be found.

Any help would be greatly appreciated!

Thank You!
Well, if you use that list in the rest of your program... why don't you use that as a global....

both of them.... the DefaultListMode l and the JList.....

Try to have an experiment about model.addElemen t(Object) when button is pressed....

Try to observe what happens to your list.....

regards,
sukatoa
Apr 22 '08 #2
JosAH
11,448 Recognized Expert MVP
public class ControlIL extends JFrame implements ActionListener, ListSelectionLi stener {

private JList Items;

//builds the UI
public ControlIL(){

DefaultListMode l model = new DefaultListMode l();
JList Items = new JList(model);
}
You assign your new JList to a local variable with the same name as your member
variable. The local variable 'shadows' the member variable and cannot be seen
and is not assigned to and therefore stays equal to null. Simply remove the
local variable and assign to the member variable.

kind regards,

Jos
Apr 22 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2215
by: Yong | last post by:
Hello, I wonder if somebody can help: I want to design a jsplitpanel that has two panels: left and right. In the left panel I can use jList to display a list of items. There will be an left arrow button and a right arrow button in between the two panels, which allow me to select items from the left panel and put in to the right panel. I can also remove items from the right panels by
1
4458
by: David | last post by:
Hello I'm writting an apllication and i like to display and offscreen image. However my code doesn't seem to work. It compiles and runs properly but What i want is to associate the button of the menu to switch an image (actually to add rectangels offscreen and bring it to the front). The following codes runs properly. You just need to create a file images.properties and put some images in a folder called images. By the way this is...
1
2414
by: Salha | last post by:
Hello there, Can you help me in following problem: Am working on a chemistry dictionary. I want to have all the chemistry terms in a JList and whenever i select a term from a JList, it displays the meaning in a text area (as any encyclopedia) and the term in a textfield. My questions: 1. How can i load the chemistry terms in to the JList and meanings in the textarea and store them? 2. How can i select a term and diplays it in the...
0
1390
by: paoparaopao | last post by:
Hi, I want to drag rows up/down in a JList; for example, drag 3rd row to 1st row. I have setDragEnabled(true) for JList, but it doesn't work! How do I drag rows in JList? Thanks!
5
4144
by: wajedali | last post by:
Hi I am having trouble rendering a combo box with icon near the text. Below is a simple code which demonstrates my problem. import java.awt.Component; import java.util.HashMap; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JComboBox; import javax.swing.JLabel;
8
3170
by: sukatoa | last post by:
I came from List in awt.... Im currently having an experiment about JList.... say like 140,280 ( h,w ) i have 4 values that have been set on JList using addElement from DefaultListModel....
0
1640
by: dvjava | last post by:
Hi, I need to come up with a java program (JList) to monitor products/goods for my operation. The program should let me enter products (items) or delete them, one at a time. Once entered, a product (item) should be highlighted or painted in “Red” or default color, indicating it needs attention. Once I finish considering a product in the list, I would like to toggle that product (item) to change the color to normal, indicating this...
2
2893
by: Gangreen | last post by:
Hi all, I've bumped into an odd problem creating a GUI in swing. As you will see in the screenshots, I have a tabbed layout. Each tab is a Jpanel. On this panel I have put a JList, containing a series of objects. My intention is to have a single column of objects in the list, and have it to scroll when the objects exceed the vertical space. My code is this: private JComponent createListBox() { Object test = {"Thingy 1","Thingy...
2
6298
by: Gangreen | last post by:
hi, I have an arrayList in my program that I want to display in a Jlist. I need the JList to update his contents when the elements in the arraylist change/ an element is added/deleted/... I'm lost on how to do this. Passing the arrayList as an array into the Jlistconstructor will just display the elements, but no updates will be made according to the API. Apparently ListDataListeners should be used, and I don't seem to be able to find a...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10201
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.