Results 1 to 5 of 5
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Why can't I add both a JList and a JScrollPane to my JFrame?

    Hi everyone, here is the code:

    Java Code:
    public class ListFrame extends JFrame
    {
    	JList myJList;
    	String[] colorrs;
    	
    	public ListFrame()
    	{
    		super("This is a List Frame");
    		this.setLayout(new FlowLayout());
    		
    		colorrs = new String[6];
    		colorrs[0] = "Red";
    		colorrs[1] = "Blue";
    		colorrs[2] = "Green";
    		colorrs[3] = "Black";
    		colorrs[4] = "White";
    		colorrs[5] = "Purple";
    		
    		myJList = new JList();
    		myJList.setListData(colorrs);
    		myJList.setVisibleRowCount(4);
    		
    		JScrollPane scrollPane = new JScrollPane(myJList);
    		this.add(scrollPane);
    		
    		
    		//this.add(myJList);
    		
    		
    	}// end Constructor
    }//end Class
    With this code, I get a nice JScrollPane that includes myJList with the options.

    But:
    If I remove the comment slashes from last statement I have(//this.add(myJList);), I am expecting to see both the JScrollPane and myJList.

    However I only get an ugly JList with no scrolling option. JScrollPane gets invisible.

    Any help?

    Thanks.

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

    Default Re: Why can't I add both a JList and a JScrollPane to my JFrame?

    A component can only be added once to a visible GUI. If you try to add it twice, the last component only is seen. To remedy the situation, use two JLists that share the same model.

    For example,

    Java Code:
    import java.awt.FlowLayout;
    import javax.swing.*;
    
    public class ListFrame extends JFrame
    {
        JList myJList;
        String[] colorrs;
         
        public ListFrame()
        {
            super("This is a List Frame");
            this.setLayout(new FlowLayout());
             
            colorrs = new String[6];
            colorrs[0] = "Red";
            colorrs[1] = "Blue";
            colorrs[2] = "Green";
            colorrs[3] = "Black";
            colorrs[4] = "White";
            colorrs[5] = "Purple";
             
            myJList = new JList();
            myJList.setListData(colorrs);
            myJList.setVisibleRowCount(4);
             
            JScrollPane scrollPane = new JScrollPane(myJList);
            this.add(scrollPane);
             
             JList newList = new JList(myJList.getModel());
            this.add(newList);
             
             
        }
        
        public static void main(String[] args) {
          ListFrame listFrame = new ListFrame();
          listFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          listFrame.pack();
          listFrame.setLocationRelativeTo(null);
          listFrame.setVisible(true);
       }
    }
    Last edited by Fubarable; 12-27-2011 at 10:30 PM.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why can't I add both a JList and a JScrollPane to my JFrame?

    Thanks for the quick and very clear answer.

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

    Default Re: Why can't I add both a JList and a JScrollPane to my JFrame?

    Quote Originally Posted by fatabass View Post
    Thanks for the quick and very clear answer.
    You are quite welcome!

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Why can't I add both a JList and a JScrollPane to my 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. JList inside JScrollPane is not displaying
    By _SAS in forum AWT / Swing
    Replies: 2
    Last Post: 07-31-2011, 08:45 PM
  2. Link one JList to another JList
    By mib1bee in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 07:10 PM
  3. Link one JList to another JList
    By mib1bee in forum Advanced Java
    Replies: 1
    Last Post: 12-30-2010, 06:35 PM
  4. Link one JList to another JList
    By mib1bee in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 06:24 PM
  5. jList - add/remove items and query - using jFrame Form
    By ArneBassez in forum AWT / Swing
    Replies: 2
    Last Post: 12-01-2010, 12:42 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
  •