Results 1 to 5 of 5
  1. #1
    dragon_baby is offline Member
    Join Date
    May 2012
    Posts
    10
    Rep Power
    0

    Default Update Jlist in JFrame

    I am trying to make a update a JList on the push of a button, but i cant seem to get it working...
    Java Code:
    public class Interface extends JFrame {
    	private JList list;
    	private JScrollPane scrollPane;
    	private JButton button;
    
    	public static void main(String[] args){
    		Interface userInterface = new Interface();
    		userInterface.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		userInterface.setSize(300, 200);
    		userInterface.setVisible(true);
    	}
    	public Interface(){
    		super("MyTitle");
    		setLayout(new FlowLayout());
    		list = new JList();
    		list.setVisibleRowCount(4);
    		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		scrollPane=new JScrollPane(list);
    		add(scrollPane);
    		button=new JButton("Update List");
    		button.setSize(100, 50);
    		add(button);
    		Handler handler = new Handler();
    		button.addActionListener(handler);
    	}
    	private class Handler implements ActionListener{
    		@Override
    		public void actionPerformed(ActionEvent e) {
    ...//making the String temp[] and populating it
    			JList list2=new JList(temp);
    			scrollPane=new JScrollPane(list2);
    			scrollPane.setVisible(true);
    
    		}
    Also, when I print the temp variable (System.out.printf(temp.toString());) i get the pointer to the memory locations instead of the values, any idea why? in the debugger, the values are correct.

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

    Default Re: Update Jlist in JFrame

    Quote Originally Posted by dragon_baby View Post
    I am trying to make a update a JList on the push of a button, but i cant seem to get it working...
    It's always helpful for you to tell us the details. What's not working? Are you seeing any errors or exceptions? Are you seeing bad behavior?


    Java Code:
    public class Interface extends JFrame {
    	private JList list;
    Is this the JList that you're trying to fill?


    Java Code:
    	private class Handler implements ActionListener{
    		@Override
    		public void actionPerformed(ActionEvent e) {
    ...//making the String temp[] and populating it
    			JList list2=new JList(temp);
    			scrollPane=new JScrollPane(list2);
    			scrollPane.setVisible(true);
    If so, you are creating a totally new JList here, one that is completely unrelated to the first JList, and one that is never added to the JFrame.

    So first and foremost, please clarify your question greatly by letting us know more about exactly what you're trying to do, and exactly how it's not working.

    Also, when I print the temp variable (System.out.printf(temp.toString());) i get the pointer to the memory locations instead of the values, any idea why? in the debugger, the values are correct.
    You are seeing the default return of your class's toString() method. If you want it to show something different, you'll want your class to override the public String toString() method and have it return a String that makes sense and that uses the class fields to do so.

    Edit: If your goal is to place the Strings into the JList that's already being displayed, then get that list's DefaultListModel via its getModel() method and start adding elements to the model in your ActionListener's actionPerformed() method.
    Last edited by Fubarable; 05-26-2012 at 02:24 PM.

  3. #3
    dragon_baby is offline Member
    Join Date
    May 2012
    Posts
    10
    Rep Power
    0

    Default Re: Update Jlist in JFrame

    Quote Originally Posted by Fubarable View Post
    It's always helpful for you to tell us the details. What's not working? Are you seeing any errors or exceptions? Are you seeing bad behavior?
    no errors, no exceptions, but also no response... which would be categorized as bad behavior...
    Quote Originally Posted by Fubarable View Post
    Java Code:
    public class Interface extends JFrame {
    	private JList list;
    Is this the JList that you're trying to fill?
    it is, and i put it in the scrollPane
    Quote Originally Posted by Fubarable View Post

    Java Code:
    	private class Handler implements ActionListener{
    		@Override
    		public void actionPerformed(ActionEvent e) {
    ...//making the String temp[] and populating it
    			JList list2=new JList(temp);
    			scrollPane=new JScrollPane(list2);
    			scrollPane.setVisible(true);
    If so, you are creating a totally new JList here, one that is completely unrelated to the first JList, and one that is never added to the JFrame.
    I tried to replace list with list2 by replacing the scrollPane by a new one built around list2...
    i tried first to replace list by the new list based on my temp String array but that didnt work either
    Quote Originally Posted by Fubarable View Post

    You are seeing the default return of your class's toString() method. If you want it to show something different, you'll want your class to override the public String toString() method and have it return a String that makes sense and that uses the class fields to do so.
    ok, got it
    Quote Originally Posted by Fubarable View Post
    Edit: If your goal is to place the Strings into the JList that's already being displayed, then get that list's DefaultListModel via its getModel() method and start adding elements to the model in your ActionListener's actionPerformed() method.
    this actually is what i'm trying, can you elaborate a bit? some example code maybe?

    Thanks A Lot
    EDIT:
    Appearantly all i needed was this little line:
    Java Code:
    list.setListData(temp);
    i just needed to put this as only line after generating the temp list.
    Last edited by dragon_baby; 05-26-2012 at 03:10 PM. Reason: Problem solved

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

    Default Re: Update Jlist in JFrame

    Don't create a new JList. Rather get the JList's model, a DefaultListModel object, by calling getModel() on the list. Then call addElement(...) on the model when you want to add elements to the list. Try it, and it will likely work for you.

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

    Default Re: Update Jlist in JFrame

    Moved from New to Java

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

Similar Threads

  1. call JFrame by select JList
    By raj.mscking@gmail.com in forum AWT / Swing
    Replies: 2
    Last Post: 01-30-2012, 09:54 AM
  2. Replies: 4
    Last Post: 12-29-2011, 09:23 AM
  3. How update Jlist in java Chat
    By pappyj77 in forum Threads and Synchronization
    Replies: 59
    Last Post: 09-13-2011, 02:26 AM
  4. How do I update a JFrame?
    By soulmed in forum New To Java
    Replies: 4
    Last Post: 04-20-2011, 09:45 PM
  5. Cannot get my JList to update!
    By rangvald in forum AWT / Swing
    Replies: 1
    Last Post: 11-17-2009, 02:57 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
  •