Results 1 to 9 of 9
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default [SOLVED] JList Expanding when not all contents visible

    Hi there all. I'm trying to make a JList expand when not all contents in it are fully visible. Currently, it just keeps listing contents further to the side without expanding. If anyone has any hints I'd be interested. It's part of a very large program, so I'll post an SSCCE as soon as I make one. All help is appreciated.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    My SSCCE
    Java Code:
    import java.awt.Dimension;
    import javax.swing.*;
    
    //SSCCE of my class InventoryDisplay where the JList is
    public class InventoryDisplaySSCCE{
    
    	private static final long serialVersionUID = 1L;
    	
    	public static void main(String[] args){
    		DefaultListModel model = new DefaultListModel();
    		JList list = new JList(model);
    		JPanel p = new JPanel(new SpringLayout());
    		Dimension dim = new Dimension(200,200);
    		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    		list.setSize(dim);
    		JScrollPane scroll = new JScrollPane(list);
    		//size is 10 pixels more on each dimension to accomodate scrollbars
    		for(int i=1; i<=400; i++){//add objects to list for testing purposes
    			model.addElement("Object #" + i);
    		}
    		list.setVisibleRowCount(list.getHeight()/10);
    		Dimension dim2 = new Dimension(dim.width + 10, dim.height + 10);
    		//size is 10 pixels more than list to accomodate
    		//scrollbars
    		scroll.setMaximumSize(dim2);
    		scroll.setMinimumSize(dim2);
    		scroll.setPreferredSize(dim2);
    		p.add(scroll);
    		SpringUtilities.makeCompactGrid(p, 1, 1, 2, 2, 2, 2);//more complicated in the program
    		JFrame f = new JFrame();
    		f.setContentPane(p);
    		f.setVisible(true);
    		f.pack();
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }
    I tried using a JScrollPane with it, and it does what I want... sort of. I need a way to detect that there is an invisible object on the list, and call setVisibleRowCount when that happens to add an extra visible row. I changed to HORIZONTAL_WRAP for the layout because it should look nicer when it expands. I could just make it insanely tall, but that wouldn't work with an insane number of objects being held in it. It would also give me scrollbars when I don't want them.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    One thing that might help me...

    How would you get the X/Y location of the JList on the screen, not relative to the panel it is contained in.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Read the API. getLocationOnScreen()

    db

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Thanks DB.

    Got somewhere with it... now it expands, but it's throwing random NPE's and not wrapping properly... Very random NPE's, never at the same place. They don't seem to affect layout (though it may edit wrapping for the JList layout.) You'll have to compile it yourself to see what I mean...

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    //SSCCE of my class InventoryDisplay where the JList is
    public class InventoryDisplaySSCCE{
    
    	private static final long serialVersionUID = 1L;
    	JList list;
    	JScrollPane scroll;
    	JPanel p;
    	ListCheckModel model = new ListCheckModel();
    	public InventoryDisplaySSCCE(){
    		list = new JList(model);
    		p = new JPanel(new SpringLayout());
    		Dimension dim = new Dimension(200,200);
    		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    		list.setSize(dim);
    		scroll = new JScrollPane(list);
    		list.setVisibleRowCount(0);
    		Dimension dim2 = new Dimension(dim.width + 10, dim.height + 10);
    		scroll.setMaximumSize(dim2);
    		scroll.setMinimumSize(dim2);
    		scroll.setPreferredSize(dim2);
    		p.add(scroll);
    		SpringUtilities.makeCompactGrid(p, 1, 1, 2, 2, 2, 2);//more complicated in the program
    		JFrame f = new JFrame();
    		f.setContentPane(p);
    		f.setVisible(true);
    		f.pack();
    		for(int i=1; i<=400; i++){//add objects to list for testing purposes
    			model.addElement("Object #" + i);
    		}
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	//ListModel for the JList with an edit...
    	class ListCheckModel extends DefaultListModel{
    		
    		private static final long serialVersionUID = 1L;
    
    		public void addElement(Object o){//Override to check the JList
    			super.addElement(o);
    			Point p = new Point(list.getLocation().x + scroll.getWidth(), list.getLocation().y + list.getHeight());
    			try{
    			if(list.locationToIndex(p)!= -1){
    				if(list.locationToIndex(p) < size()&& list.locationToIndex(p)!= -1){
    					list.setVisibleRowCount(list.getVisibleRowCount()+1);
    					InventoryDisplaySSCCE.this.p.validate();
    				}
    			}
    			}catch(NullPointerException npe){//WHY IS THERE AN NPE???
    				System.out.println(list);
    				System.out.println(p);
    				System.out.println(list.locationToIndex(p));
    			}
    		}
    	}
    	public static void main(String[] args){
    		new InventoryDisplaySSCCE();
    	}
    }
    Also, was going to post a link to the SpringUtilities code...
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I made this up before seeing your last post.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    //SSCCE of my class InventoryDisplay where the JList is
    public class IDS{
    
        private static final long serialVersionUID = 1L;
    
        public static void main(String[] args){
            DefaultListModel model = new DefaultListModel();
            final JList list = new JList(model);
            JPanel p = new JPanel(new GridBagLayout());
            Dimension dim = new Dimension(200,200);
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
            // You can use this to store the dim but it has no affect on
            // sizing the list. We usually use the preferredSize for sizing
            // components in Swing.
    //        list.setSize(dim);
            JScrollPane scroll = new JScrollPane(list);
            //size is 10 pixels more on each dimension to accomodate scrollbars
            // The above is not necessary - java will take care of the
            // display of the list inside the scrollPane. 
            for(int i=1; i<=400; i++){//add objects to list for testing purposes
                model.addElement("Object #" + i);
            }
            int columnCount = 20;
            list.setVisibleRowCount(//list.getHeight()/10);
                                    model.getSize()/columnCount);
            // Java will do a lot of this kind of work for you.
    //        Dimension dim2 = new Dimension(dim.width + 10, dim.height + 10);
            //size is 10 pixels more than list to accomodate
            //scrollbars
            // SpringLayout may need these.
    //        scroll.setMaximumSize(dim2);
    //        scroll.setMinimumSize(dim2);
            scroll.setPreferredSize(dim);
            p.add(scroll, new GridBagConstraints());
            p.setOpaque(true);  // required for contentPanes
            final JFrame f = new JFrame();
            f.setContentPane(p);
            f.setSize(400,400);
    //        f.pack();
            f.setLocation(100,100);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // [i]setVisible[/i] should usually be the last call.
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    f.setVisible(true);
                    // Que this method on the EDT to avoid
                    // NullPointerException in BasicListUI
                    // layout code. This is an attempt to
                    // ensure that the gui has a chance to
                    // be realized and to stabilize before
                    // exploring it.
                    explore(list);
                }
            });
        }
    
        private static void explore(JList list) {
            // Get location of the list.
            Container topLevel = list.getTopLevelAncestor();
            System.out.println("topLevel = " +
                                topLevel.getClass().getSimpleName());
            Point screenLoc = list.getLocationOnScreen();
            Point locInParent = list.getLocation(); // parent is JViewport
            Point locInFrame = SwingUtilities.convertPoint(list, 0,0, topLevel);
            System.out.printf("screenLoc = [%d, %d]  locInParent = [%d, %d]  " +
                              "locInFrame = [%d, %d]%n",
                               screenLoc.x, screenLoc.y,
                               locInParent.x, locInParent.y,
                               locInFrame.x, locInFrame.y);
            // Navigating in the list.
            int visRowCount = list.getVisibleRowCount();
            int firstIndex = list.getFirstVisibleIndex();
            int lastIndex = list.getLastVisibleIndex();
            System.out.printf("visRowCount = %d  firstIndex = %d  lastIndex = %d%n",
                               visRowCount, firstIndex, lastIndex);
            // Where is the lastIndex located on list?
            Point lastIndexLoc = list.indexToLocation(lastIndex);
            System.out.printf("lastIndexLoc = [%d, %d]%n",
                               lastIndexLoc.x, lastIndexLoc.y);
            // Scroll to show the element at lastIndex.
            Rectangle r = list.getCellBounds(lastIndex, lastIndex);
            System.out.printf("r = [%d, %d, %d, %d]%n",
                               r.x, r.y, r.width, r.height);
            // NullPointerException came from this next line when
            // the [i]explore[/i] method was not queued up on the EDT.
            list.scrollRectToVisible(r);
            // What viewport size is required to show the whole list?
            Dimension d = list.getPreferredScrollableViewportSize();
            System.out.printf("d = [%d, %d]%n", d.width, d.height);
        }
    }

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm... Got your point about the NPE. Still doesn't wrap. Great way of handling columns, but it doesn't do quite what I want. I dont want to have to set the column number. Instead, I want it to determine when it has to wrap itself, because I have no way of determining how wide the components will be.

    I'll post the code again after I edit it if you need it.

    Thanks,
    Singing Boyo

    EDIT: Took another look at how you set the columns... So JList only adds columns if there are not enough rows to fit all components? Interesting.
    Last edited by Singing Boyo; 05-31-2009 at 12:25 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    So JList only adds columns if there are not enough rows to fit all components?
    Look in the JList api Method Summary for the setLayoutOrientation method, follow the link to the Method Detail section and read the Description for the HORIZONTAL_WRAP Value.

  9. #9
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Read some code examples and discovered just needed to call setVisibleRowCount(-1) and it wraps when needed, creating new rows as necessary.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. JTextField not visible?
    By McChill in forum New To Java
    Replies: 2
    Last Post: 05-20-2009, 12:17 AM
  2. [SOLVED] Prevent JTextField from Expanding Within JFrame
    By Singing Boyo in forum New To Java
    Replies: 5
    Last Post: 05-15-2009, 02:00 AM
  3. Collapsing/Expanding() All nodes in a TreeMenu using javascript
    By Hadley in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 01-30-2009, 11:18 PM
  4. Address bar not visible
    By Akashchopra521 in forum New To Java
    Replies: 0
    Last Post: 12-03-2008, 07:13 AM
  5. constructor ... is not visible
    By emceenugget in forum New To Java
    Replies: 2
    Last Post: 09-27-2008, 05:12 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
  •