Results 1 to 6 of 6
  1. #1
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Having issues with adding an item to JList.

    First of all, the source code which matters.
    Java Code:
    	DefaultListModel<String> listModel = new DefaultListModel<String>();
    	JList list = new JList(listModel);
    	JList list = new JList(listModel);
    	JTextField lnch_name;
    
    	public class lnch_menuadd_btn implements ActionListener {
    		public void actionPerformed(ActionEvent ev) {
    			listModel.addElement(lnch_name.getText());
    			Program.lnchaddGUIHide();
    			Program.lnchaddGUIClear();
    		}
    	}
    When the listener is activated, the JTextFields are successfully cleared and the frame is successfully closed, I'm confused because nothing seems to be added to the JList. No errors show up in console.
    Am I missing something? I don't see why it's not working.
    Also Program is just the object of the class, and the lnch_menuadd_btn action listener class is a inner class. Yes, lnch_name is initialized somewhere else.

    Edit: Whoops, I forgot to post this in the swing section. If it could be moved it would be appreciated.
    Last edited by Vinx; 08-08-2012 at 04:22 PM.

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

    Default Re: Having issues with adding an item to JList.

    Moved to Swing forum. I'm not sure your question is answerable given the information that has been presented. I suspect that your problem lies elsewhere in code not shown.

    Edit:
    For another thing your code won't even compile as presented since you have two JList variables with the same name. I also wonder if the JLists that have this list model are the ones that are in fact being displayed. But again this all depends on code not yet shown.
    Last edited by Fubarable; 08-08-2012 at 04:41 PM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Having issues with adding an item to JList.

    Quote Originally Posted by Vinx View Post
    Also Program is just the object of the class
    Please follow standard Java code conventions. A variable name should start with a lowercase letter. And only constants (static final variables) should include an underscore in the name; other variable names should use camelCase.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Re: Having issues with adding an item to JList.

    Quote Originally Posted by Fubarable View Post
    Moved to Swing forum. I'm not sure your question is answerable given the information that has been presented. I suspect that your problem lies elsewhere in code not shown.

    Edit:
    For another thing your code won't even compile as presented since you have two JList variables with the same name. I also wonder if the JLists that have this list model are the ones that are in fact being displayed. But again this all depends on code not yet shown.
    Sorry, I was just copy and pasting out of my code what I thought was related. I must have pasted the JList variables twice on accident.

    I believe this is what you're asking for.

    Java Code:
    	public void buildLunchGUI() {
    		JFrame lnch_frame = new JFrame("Dvonx - Lunch");
    		JMenuBar lnch_menubar = new JMenuBar();
    	//	lnch_frame.add(lnch_menubar);
    		lnch_frame.setJMenuBar(lnch_menubar);
    		JMenu lnch_file = new JMenu("File");
    		lnch_menubar.add(lnch_file);
    		JMenuItem lnch_help = new JMenuItem("Help");
    		JMenuItem lnch_close = new JMenuItem("Close");
    		lnch_file.add(lnch_help);
    		lnch_file.add(lnch_close);
    		JMenu lnch_add = new JMenu("Add");
    		JMenuItem lnch_additem = new JMenuItem("Item");
    		lnch_additem.addActionListener(new lnch_menuadd());
    		lnch_add.add(lnch_additem);
    		lnch_menubar.add(lnch_add);
    		JPanel lnch_northpanel = new JPanel();
    		lnch_frame.add(BorderLayout.NORTH, lnch_northpanel);
    		JLabel lnch_lbl_studentid = new JLabel("Student ID:");
    		lnch_studentid = new JTextField(5);
    		lnch_northpanel.add(lnch_lbl_studentid);
    		lnch_northpanel.add(lnch_studentid);
    		lnch_frame.setSize(500,500);
    		@SuppressWarnings("rawtypes")
    		DefaultListModel listModel = new DefaultListModel();
    		@SuppressWarnings({ "rawtypes", "unchecked" })
    		JList list = new JList(listModel);
    		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		list.setSelectedIndex(0);
    		list.setVisibleRowCount(20);
    		JScrollPane listScrollPane = new JScrollPane(list);
    		lnch_frame.add(listScrollPane, BorderLayout.WEST);
    		JPanel lnch_secondarypanel = new JPanel();
    		lnch_frame.add(BorderLayout.CENTER,lnch_secondarypanel);
    		lnch_console = new JTextArea(8,20);
    		lnch_console.setEditable(false);
    		lnch_frame.add(BorderLayout.SOUTH, lnch_console);
    		lnch_frame.setVisible(true);
    	}
    	
    	public void lnchaddGUIBuild() {
    		 lnch_add_frame = new JFrame();
    		 lnch_add_frame.setResizable(false);
    		 JPanel panel = new JPanel();
    		 lnch_add_frame.add(panel);
    		 lnch_lbl_name = new JLabel("Name:");
    		 JLabel lnch_lbl_cost = new JLabel("Cost:");
    		 JButton lnch_btn_add = new JButton("Add");
    		 lnch_btn_add.addActionListener(new lnch_menuadd_btn());
    		 panel.add(BorderLayout.CENTER,lnch_lbl_name);
    		 lnch_name = new JTextField(5);
    		 lnch_cost = new JTextField(5);
    		 panel.add(BorderLayout.CENTER,lnch_name);
    		 panel.add(BorderLayout.CENTER,lnch_lbl_cost);
    		 panel.add(BorderLayout.CENTER,lnch_cost);
    		 panel.add(BorderLayout.SOUTH,lnch_btn_add);
    		 lnch_add_frame.setSize(250,81);
    		 lnch_add_frame.setResizable(false);
    		 lnch_add_frame.setVisible(true);
    	}
    So I realize that I accidentally declared JList once again there, but for some reason if I put it at the top of the class(my usual trick so everything can access the object) my methods don't recognize the object when I try to refer to it.


    Edit: The code tags seem to be glitching, they put a lot of extra space on the end.

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

    Default Re: Having issues with adding an item to JList.

    I think that if you copy your code to a new class and start cutting out code unrelated to your problem, paring it down to the bare minimum that allows it to run and show your problem, your error will become obvious to you. If not, then you can post this small but complete and runnable program, and we can experience the problem directly and be much better able to help you. In other words, consider creating and posting an SSCCE.

  6. #6
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Re: Having issues with adding an item to JList.

    Never mind I was able to finally fix it by just entirely re-doing the JList stuff, I decided a SSCCE would take rather long for me to do due to the size of this project. Thank you for your help anyways, made me realize my code was a mess ;)

Similar Threads

  1. NullPointer adding an item to a LinkedList
    By sehudson in forum New To Java
    Replies: 7
    Last Post: 03-10-2011, 03:39 AM
  2. JList item text does not truncate when it's HTML
    By edsporty in forum New To Java
    Replies: 1
    Last Post: 03-02-2011, 08:09 PM
  3. Add JList item from resultset
    By mine0926 in forum NetBeans
    Replies: 1
    Last Post: 06-17-2010, 09:47 AM
  4. JList issues
    By aneesahamedaa in forum AWT / Swing
    Replies: 7
    Last Post: 12-15-2009, 04:16 PM
  5. Replies: 3
    Last Post: 11-11-2009, 09:00 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
  •