Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Norm

Thread: Having trouble updating JList using JComboBox

  1. #1
    jchinson is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default Having trouble updating JList using JComboBox

    I have a JComboBox with a list of cabins 1-10. I want the names of the kids that are in cabin x to appear when I click on cabin x. I get a nullPointerException whenever I run the following:

    Java Code:
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.event.ListDataEvent;
    import javax.swing.event.ListDataListener;
    import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemListener;
    
    public class WOW {
    	public static final int MAX_KIDS = 500;
    	JLabel cabinLabel;
    	static Hashtable<String, Kid> kids = new Hashtable<String, Kid>(MAX_KIDS);
    	private static JList list;
    	
    	public static void main(String[] args) {
    		
    		Kid kid = new Kid("Cody", 20, 4, "e", "a", "f", "d", "s", "a", "j", "l", "o", "i", "u", "y");
    		Kid kid2 = new Kid("wil", 23, 6, "a", "s", "f", "a", "R", "e", "g", "3", "g", "b", "c", "g");
    		Kid kid3 = new Kid("Lemuelle", 21, 6, "a", "s", "f", "a", "R", "e", "g", "3", "g", "b", "c", "g");
    		kids.put(kid.getName(), kid);
    		kids.put(kid2.getName(), kid2);
    		kids.put(kid3.getName(), kid3);
    		mainScreen();
    		
    	}
    
    	
    	
    	public static void mainScreen() {
    
    		final JFrame frame = new JFrame();
    		final Container pane = frame.getContentPane();
    		JLabel cabinLabel = new JLabel("Select a cabin");
    		String[] cabins = {"Cabin 1", "Cabin 2", "Cabin 3", "Cabin 4", 
    				"Cabin 5", "Cabin 6", "Cabin 7", "Cabin 8", "Cabin 9", "Cabin 10"};
    		JComboBox cabinList = new JComboBox(cabins);
    		//final DefaultListModel listModel = new DefaultListModel();
    		//JList list = new JList();
    		
    		
    		frame.setTitle("WOW");
    		frame.setSize(800,500);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		
    		pane.setLayout(new GridLayout(3,2));
    		pane.add(cabinLabel);
    		pane.add(cabinList);
    		
    		cabinList.addActionListener(
    				new ActionListener() {
    					public void actionPerformed(ActionEvent e) {
    						JComboBox cabinList = (JComboBox)e.getSource();
    						String cabinSelected = (String)cabinList.getSelectedItem();
    						
    						updateCabinList(cabinSelected);
    						
    						
    						list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    						list.setLayoutOrientation(JList.VERTICAL);
    						
    						pane.add(list);
    						frame.pack();
    						frame.validate();
    					}
    				});
    		
    		
    		frame.validate();
    	}
    	
    	
    	public static void updateCabinList(String cabin) {
    		String[] names = new String[50];
    		int curName = 0;
    		int cabinNumber = (cabin.charAt(cabin.length() - 1)) - '0';
    		if(cabinNumber == 0) cabinNumber = 10;
    		Enumeration<Kid> e = kids.elements();
    		
    		while(e.hasMoreElements()) {
    			Kid next = e.nextElement();
    			int c = next.getCabin();
    			if(c == cabinNumber) {
    				names[curName] = next.getName();
    				curName++;
    			}
    		}
    		
    		DefaultListModel listModel = new DefaultListModel();
    		listModel.addElement(names);
    		list.setModel(listModel);
    		
    	}
    	
    	
    	
    }

    Anybody know what I am doing wrong? Thanks.
    Last edited by jchinson; 07-03-2011 at 04:52 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    I get a nullPointerException
    Please copy and paste here the full text of the error message. It has the source file line number

    Look at the line with the error and see what variable is null. Then back track in your code to see why that variable is null.
    Fubarable likes this.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    jchinson is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    Here is my error message

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at WOW.updateCabinList(WOW.java:183)
    	at WOW$1.actionPerformed(WOW.java:121)
    	at javax.swing.JComboBox.fireActionEvent(Unknown Source)
    	at javax.swing.JComboBox.setSelectedItem(Unknown Source)
    	at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    	at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
    	at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    The problem is when I call list.setModel(listModel) in the updateCabinList method. I've never used list models, or worked with guis for that matter, so I'm not quite sure how to do this. Thanks for your help!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It has nothing to do with lists/models per se, but a variable that you're trying to use on that line is null, and in fact it can only be one of the two possible variables since you initialize the other just beforehand. So look back and see why you've not initialized the other.

  6. #6
    jchinson is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    Thanks Fubarable! I never initialized my JList. I get no errors now, but instead of displaying the names that are in the model it just displays

    [Ljava.lang.String;@7977b9b

    or some other string after the @ sign. Any idea why this is?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    [Ljava.lang.String;@7977b9b
    That looks like what is returned when you println a String array. It is the default returned by the Object toString() method.
    If you want to see the contents of an array, use the Arrays.toString() method.

  8. #8
    jchinson is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    use the Arrays.toString() method.
    This works, but I have to add an object to the list model so it ends up displaying the contents of the whole array together

    Java Code:
    [Cody, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
    but i want to display only the non-null items in the list, individually. This is what I have now

    Java Code:
    public static void updateCabinList(String cabin) {
    		String[] names = new String[50];
    		int curName = 0;
    		int cabinNumber = (cabin.charAt(cabin.length() - 1)) - '0';
    		if(cabinNumber == 0) cabinNumber = 10;
    		Enumeration<Kid> e = kids.elements();
    		
    		while(e.hasMoreElements()) {
    			Kid next = e.nextElement();
    			int c = next.getCabin();
    			if(c == cabinNumber) {
    				names[curName] = next.getName();
    				curName++;
    			}
    		}
    		
    		DefaultListModel listModel = new DefaultListModel();
    		listModel.addElement(Arrays.toString(names));
    		list.setModel(listModel);
    		
    		
    	}

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    Yes the Arrays.toString is mainly for debugging.
    i want to display only the non-null items in the list, individually. This is what I have now
    Does it output what you want?

  10. #10
    jchinson is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    Does it output what you want?
    Yes, I get the correct names but I'm not sure how to display each name as a separate clickable item in the JList, rather than all the contents of the array together.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    Your addElement() call adds the whole array's contents as a single element.
    If you want separate elements, write a loop to go thru the array and add its contents one by one.

  12. #12
    jchinson is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Link one JList to another JList
    By mib1bee in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 07:10 PM
  2. Link one JList to another JList
    By mib1bee in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 06:24 PM
  3. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  4. Got some trouble with JComboBox
    By hungleon88 in forum AWT / Swing
    Replies: 16
    Last Post: 09-15-2008, 11:26 AM
  5. Problem with JComboBox and Jlist
    By java_fun2007 in forum New To Java
    Replies: 2
    Last Post: 05-07-2008, 08:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •