Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Return statements while building a JPanel!

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Return statements while building a JPanel!

    Hello, i'm having this where i'm trying to return a JPanel with tempPictures but only if they aren't already filled with "actual" pictures. However this causes problems because your never shore that the if statement will execute. I tried adding an else statement but i cant return null i must return a JPanel;

    	private JPanel buildTopShelf() {
    		if(myMediaHandler.mediaList.size() < 6) {
    		JPanel mediaPanel = new JPanel();
    		mediaPanel.setOpaque(false);
    		mediaPanel.setLayout(new GridLayout(1,6,22,0));
    		mediaPanel.setBounds(90, 14, 1020, 210);
     
    		java.net.URL where;
    		try {
    			where = new URL("https://dl.dropboxusercontent.com/u/16670644/Projekt/TempPic.png");
    			ImageIcon image = new ImageIcon(where);
    			for(int i = myMediaHandler.mediaList.size(); i < 6; i++) {
    				JLabel imageLabel = new JLabel(image);
    				mediaPanel.add(imageLabel);
    			}
     
    		} catch (MalformedURLException e) {
    			// TODO Auto-generated catch block
    			System.out.println("Error loading TempPic that shows allowed movie spots");
    			e.printStackTrace();
    		}
    		return mediaPanel;
    	}
    		else {
    			return null;
    		}
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Return statements while building a JPanel!

    trying to return a JPanel with tempPictures but only if they aren't already filled
    Then the caller of the method should test for the returned null value to know that the panel is filled.

    Not sure about this design.
    Should the method define and fill a panel with images and return that newly filled panel.
    Or The caller could test if the panel is full before calling the method and not call the method if the panel is full.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Return statements while building a JPanel!

    It turned out to be an error with the size(); from the ArrayList. Ones i fixed that and changed return null; to return mediaPanel; it worked the way i wanted.

Similar Threads

  1. JPanel within JPanel causing odd stretching behavior.
    By mstabosz in forum AWT / Java Swing
    Replies: 7
    Last Post: September 2nd, 2013, 06:32 AM
  2. Replies: 1
    Last Post: July 29th, 2013, 06:22 AM
  3. Help with NetBeans App using JPanel/JFrame with JButton (if statements)
    By deanbyrne95 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 3rd, 2013, 07:48 PM
  4. Other possible issues with if statements - book return program
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2012, 07:29 PM
  5. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM