Results 1 to 3 of 3
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By LucienMontierre

Thread: Change JList contents after it's already been displayed?

  1. #1
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Question Change JList contents after it's already been displayed?

    I have a JList I'm using in my frame;
    This is how it is set up in the constructor of my class:

    Java Code:
    String tobesplit = "Item1;Item2;Item3";
    String liststring[] = tobesplit.split(";");
    listbox1 = new JList(liststring);
    In this way, the JList displays Item1 for index 0, item2 for index 1, and so on.

    What I'm trying to do is make it so that my JButton changes the contents of the JList.

    For example, let's say I wanted the contents of my JList to change to the items of another string array called "newliststring".

    Java Code:
    	else if (event.getSource() == button1){
    listbox1 = new JList(newliststring);
    }
    No errors, but it doesn't do what I want... It doesn't seem to do anything at all.

    What code would I use to change the contents of my JList once it's already been displayed?

    Help is much appreciated, Thanks.

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

    Default Re: Change JList contents after it's already been displayed?

    What your code does is create a new JList with the new data, but it doesn't do anything to the JList that is displayed in the GUI. The key is that you must understand the difference between reference variables and objects. Your code above will change the object that your listbox1 variable refers to, but it does nothing to the object that is displayed, other than to have listbox1 not refer to it anymore.

    Your best bet is to change the JList model (use a DefaultListModel) or the data held by the model (if changing a few items, not all items), not the JList itself.
    LucienMontierre likes this.

  3. #3
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Change JList contents after it's already been displayed?

    Thanks, works great!

    Here's my final code:

    Java Code:
    	listboxmodel1 = new DefaultListModel();
    	listbox1 = new JList(listboxmodel1);
    Java Code:
    	else if (event.getSource() == button1){
    			load loadobject = new load();
    			listboxmodel1.removeAllElements();
    			String list[] = loadobject.loadfile().split(";");
    			int loop = 0;
    			while (loop < list.length){
    			listboxmodel1.addElement(list[loop]);
    			loop++;
    			}
    Fubarable likes this.

Similar Threads

  1. change contents of shell (SWT)
    By ubuntu7 in forum SWT / JFace
    Replies: 5
    Last Post: 08-09-2012, 05:58 AM
  2. how to read a file and change its contents.
    By mointariq in forum New To Java
    Replies: 1
    Last Post: 04-30-2012, 07:19 PM
  3. Replies: 5
    Last Post: 05-09-2011, 06:28 AM
  4. Replies: 3
    Last Post: 04-06-2011, 03:48 AM
  5. [SOLVED] JList Expanding when not all contents visible
    By Singing Boyo in forum New To Java
    Replies: 8
    Last Post: 06-01-2009, 05:29 AM

Posting Permissions

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