Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: Having Trouble with JPanels

  1. #1
    guadalcanal is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Thumbs up Having Trouble with JPanels[SOLVED]

    Hello everyone!

    My problem is that JPanels which I'm dynamically adding to another JPanel are not showing up how I'd like them to at runtime. Here's an SSCCE of my project.
    Main Class:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.BoxLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    
    public class Main {
    	static JFrame window;
    	static JPanel scrollingImages = new JPanel();
    	static PreviewContainer[] preview = new PreviewContainer[10];
    public static void main(String[] args){
    		
    		window = new JFrame("Layer Manager 2.0");
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setSize(700,500);
    		window.setVisible(true);
    		window.setLayout(new BorderLayout());
    		
    		createJPanels();
    	}
    
    	public static void createJPanels(){
    		
    		//Preview Panel
    		scrollingImages.setLayout(new BoxLayout(scrollingImages,BoxLayout.Y_AXIS));
    		scrollingImages.setBackground(Color.red);
    		createPreviews();
    	}
    		
    		public static void createPreviews(){
    			for(int i=0;i<10;i++){
    				preview[i] = new PreviewContainer(110,150,100);
    				scrollingImages.add(preview[i]);
    				scrollingImages.revalidate();
    				scrollingImages.repaint();
    			}
    			JScrollPane previewList = new JScrollPane(scrollingImages, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    			window.add(previewList,BorderLayout.WEST);
    			window.add(previewList,BorderLayout.WEST);
    			System.out.println("Previews Generated.");
    		}
    }
    PreviewContainer class
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    
    public class PreviewContainer extends JPanel{
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	int height, imageWidth, imageHeight;
    	public PreviewContainer(int height, int imageWidth, int imageHeight){
    		this.height = height;
    		this.imageWidth = imageWidth;
    		this.imageHeight = imageHeight;
    	}
    	@Override
    	public void paintComponent(Graphics g){
    		int width = imageWidth+20;
    		g.setColor(Color.BLACK);
    		g.drawRect(0, 0, width, height);
    	}
    }
    My issue is that the rectangles of the PreviewContainers are much bigger than the ScrollingImages JPanel, but ScrollingImages doesn't resize to fit them in.

    Thank you for your help!
    -Guad
    Last edited by guadalcanal; 09-22-2014 at 04:55 AM. Reason: Solved

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Having Trouble with JPanels

    You haven't set the size of your panels. You should use setPreferredSize() in your panels
    since you are using a layout manager. Painting beyond your dimensions does not resize your panels.

    Regards,
    Jim
    guadalcanal likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    guadalcanal is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Having Trouble with JPanels

    It works, thank you! I knew there had to be something like that but I didn't know how to do it!

Similar Threads

  1. JPanels ?
    By santa in forum New To Java
    Replies: 5
    Last Post: 02-25-2012, 07:10 PM
  2. CardLayout with JPanels
    By Angelo in forum AWT / Swing
    Replies: 17
    Last Post: 12-15-2011, 06:50 PM
  3. Replies: 2
    Last Post: 04-26-2011, 02:50 PM
  4. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 08:33 PM
  5. HELP with jPanels
    By maverik_vz in forum AWT / Swing
    Replies: 1
    Last Post: 03-12-2009, 11:46 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
  •