By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,649 Members | 2,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,649 IT Pros & Developers. It's quick & easy.

Very frustrating problem with JList

P: 10
Hi, here is some of my code

public class ControlIL extends JFrame implements ActionListener, ListSelectionListener {

private JList Items;

//builds the UI
public ControlIL(){

DefaultListModel model = new DefaultListModel();
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.setListData(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
Share this Question
Share on Google+
2 Replies


100+
P: 539
Hi, here is some of my code

public class ControlIL extends JFrame implements ActionListener, ListSelectionListener {

private JList Items;

//builds the UI
public ControlIL(){

DefaultListModel model = new DefaultListModel();
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.setListData(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 DefaultListModel and the JList.....

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

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

regards,
sukatoa
Apr 22 '08 #2

Expert 10K+
P: 11,448
public class ControlIL extends JFrame implements ActionListener, ListSelectionListener {

private JList Items;

//builds the UI
public ControlIL(){

DefaultListModel model = new DefaultListModel();
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

Post your reply

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