Results 1 to 2 of 2
  1. #1
    stevieger is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default GUI, JList with scrollbar

    I'm trying to program a a gui that gives a list to select from, problem I'm having is the the list is long and is running outside the boundaries of the window so I have to expand the size of thewindow to see the whole list. I want to solve this by just having a scroll bar and then my window size won't have to change. Below is the window constructor I'm using and I've been trying to use a JScrollBar with no luck. Something makes me think it has to do with the flow layout and the area of the JList isn't set so I feel like that could be a component as well, I'm just unsure what or how to do this. Any help is appreciated.
    Java Code:
    public class Window extends JFrame {
    
    	public Window(){
    		super("Recipe Select");
    		this.setSize(400, 400);
    		this.setLayout(new FlowLayout());
    		
    				
    		String file_name = "C:/test.txt";
    		String[] aryLines = null;
    		
    		try {
    			ReadFile file = new ReadFile(file_name);
    			aryLines = file.OpenFile();
    			
    		}
    		catch (IOException e) {
    			System.out.println( e.getMessage() );
    		}
    	
    		
    		JLabel message = new JLabel("Choose recipe to edit:");
    		this.add(message);
    		
    		JList list = new JList(aryLines);
    		this.add(list);
    		
    		JButton button = new JButton("Edit");
    		this.add(button);
    		
    	
    		
    
    		this.setVisible(true);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
    		
    			
    		
    		}
    		
    	}

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

    Default Re: GUI, JList with scrollbar

    Moved from New to Java.

    I don't see a JScrollBar or JScrollPane in the code you posted. Read the API for JScrollPane and follow the link to the Swing tutorial on How to Use Scroll Panes. There are also examples in the tutorial on How to Use Lists, linked from the API for JList.

    Separately, note that naming your own class the same as one that exists in the standard JDK can lead to obscure problems. I strongly recommend you name that class something other than Window.

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

Similar Threads

  1. JList with a scrollbar
    By tom2zip in forum New To Java
    Replies: 2
    Last Post: 01-19-2012, 08:23 AM
  2. Replies: 4
    Last Post: 01-10-2012, 01:05 PM
  3. scrollbar not coming on jlist
    By silversurfer2in in forum AWT / Swing
    Replies: 8
    Last Post: 06-22-2010, 11:07 AM
  4. ScrollBar in a JList
    By lupo in forum AWT / Swing
    Replies: 2
    Last Post: 05-20-2010, 10:42 PM
  5. JList with ScrollBar
    By Psyclone in forum AWT / Swing
    Replies: 5
    Last Post: 02-12-2010, 04:02 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
  •