Results 1 to 11 of 11
  1. #1
    Fuginator is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Post New to SWING / a tad confused on showing things...

    Here is the code for two files, I am expecting a blue ball to show up on a panel, in a frame. I can get it to work if I place my ball on the frame directly, but not if I place it on the panel, on the frame. Please keep in mind I am new to Java GUI in general...

    Assn4.java:
    Java Code:
    import javax.swing.*;
    
    public class Assn4 {
    
    	public static void main(String[] args) {
    		System.out.printf("Starting Program 4\n");
    		
    		JFrame frame = new JFrame("Balls");
    		frame.setSize(256, 256);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		JPanel panel = new JPanel();
    		
    		frame.add(panel);
    		
    		Ball x = new Ball();
    		frame.add(x);
    		// panel.add(x); <-- want this to work
    
    		panel.setVisible(true);
    		frame.setVisible(true);
    	}
    	
    }
    Ball.java:
    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class Ball extends JPanel implements ActionListener{
    
    	// Eclipse wants this serial thing...
    	private static final long serialVersionUID = -4316629291579494934L;
    	static int xsize;
    	static int ysize;
    	static Color color;
    	
    	public Ball() {
    		System.out.printf("Creating new ball\n");
    		xsize = 50;
    		ysize = 50;
    		color = Color.BLUE;
    	}
    	
    	public void paintComponent( Graphics g ){
    		System.out.printf("Painting ball\n");
    		g.setColor(color);
    		g.fillOval(15, 15, xsize, ysize);
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		System.out.printf("Ball action...\n");
    		this.repaint();
    	}
    
    }
    Thanks in advance for the insight.

    Andrew

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You want to pre-load the panel before adding it to the frame. Try adding the ball to the panel, then add the panel to the frame.

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

    Default

    Why do you want to place your Ball inside of a JPanel as it achieves no purpose? Why not simply add the Ball object to the JFrame (as you've done above)? If you need to nest JPanels, then the one holding the Ball object needs to have a layout that tells the Ball JPanel to completely fill it, for instance a BorderLayout. You will also want to set one of the JPanel's preferred size and call pack() on the JFrame before calling setVisible(true).

  4. #4
    David M. is offline Member
    Join Date
    Apr 2011
    Location
    Kansas
    Posts
    26
    Rep Power
    0

    Default

    You can't get it to show because you're adding the ball to the frame, not to the panel.

    Java Code:
    Ball x = new Ball();
    frame.add(x);
    See, you've added the ball to the frame.

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

    Default

    Quote Originally Posted by David M. View Post
    You can't get it to show because you're adding the ball to the frame, not to the panel.

    Java Code:
    Ball x = new Ball();
    frame.add(x);
    See, you've added the ball to the frame.
    David: Please re-read the original post. If he adds the Ball object directly to the JFrame (actually the JFrame's contentPane), it should show, and in fact he tells us that it does show. This code is not in error and in fact is the correct way to do this. This is because the contentPane uses a BorderLayout as its default layout, and adding a component to a BorderLayout without using BorderLayout constants will add that component to the BorderLayout.CENTER position and will have that component fill the container and display it.

    Original poster, please disregard David's well-meaning but mistaken post.

  6. #6
    Fuginator is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    You want to pre-load the panel before adding it to the frame. Try adding the ball to the panel, then add the panel to the frame.
    I have tried this with the code below, same result (empty looking window):

    Java Code:
    import javax.swing.*;
    
    public class Assn4 {
    
    	public static void main(String[] args) {
    		System.out.printf("Starting Program 4\n");
    		
    		JFrame frame = new JFrame("Balls");
    		frame.setSize(256, 256);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		JPanel panel = new JPanel();
    		
    		Ball x = new Ball();
    		// frame.add(x);
    		panel.add(x);
    
    		panel.setVisible(true);
    		frame.add(panel);
    				
    		frame.setVisible(true);
    	}
    	
    }

  7. #7
    Fuginator is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Why do you want to place your Ball inside of a JPanel as it achieves no purpose? Why not simply add the Ball object to the JFrame (as you've done above)? If you need to nest JPanels, then the one holding the Ball object needs to have a layout that tells the Ball JPanel to completely fill it, for instance a BorderLayout. You will also want to set one of the JPanel's preferred size and call pack() on the JFrame before calling setVisible(true).
    This is a good question - I just assumed that everything had to be in a panel to be 'correct' - this is not true, correct?

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

    Default

    Quote Originally Posted by Fuginator View Post
    This is a good question - I just assumed that everything had to be in a panel to be 'correct' - this is not true, correct?
    No, that is not 'correct'. When you add something to a JFrame, you are actually adding it to the JFrame's contentPane which is a JPanel. But also your Ball class is a JPanel. Your use of another JPanel is unnecessary and is messing you up. I strongly urge you to read the Oracle tutorial on the layout managers (google "adding components to a container") to see why this is messing you up and how to use layout managers to your advantage.

  9. #9
    Fuginator is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    No, that is not 'correct'. When you add something to a JFrame, you are actually adding it to the JFrame's contentPane which is a JPanel. But also your Ball class is a JPanel. Your use of another JPanel is unnecessary and is messing you up. I strongly urge you to read the Oracle tutorial on the layout managers (google "adding components to a container") to see why this is messing you up and how to use layout managers to your advantage.
    I see - thanks for the insight.

    - Andrew

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    When you create a custom component it is your responsibility to determine the preferred size of the component so the layout manager can use this information to layout the component. Add something like the following to your Ball class:

    Java Code:
    public Dimension getPreferredSize()
    {
         return new Dimension(80, 80);
    }
    Yes it is unnecessary to add the Ball component to another panel first and if will work if you use:

    Java Code:
    frame.add(ball).
    However, it won't work if you use:

    Java Code:
    frame.add(ball, BorderLayout.NORTH);
    The key issue with this problem is the custom component needs to have a preferred size and you should not rely on a layout manager to adjust the size of the component.
    Last edited by camickr; 05-09-2011 at 02:53 AM.

  11. #11
    Fuginator is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Need Help. with two simple things
    By Fred1 in forum New To Java
    Replies: 32
    Last Post: 04-28-2011, 02:35 AM
  2. Checking if 2 things are false
    By ile4 in forum New To Java
    Replies: 4
    Last Post: 11-16-2010, 10:40 AM
  3. Showing NetBeans-generated Swing forms
    By _SAS in forum AWT / Swing
    Replies: 0
    Last Post: 06-19-2010, 02:45 AM
  4. XML, and other things.
    By Tortex in forum New To Java
    Replies: 5
    Last Post: 03-28-2010, 05:53 PM
  5. so whats going on? (things aren't showing up)
    By Adrien in forum AWT / Swing
    Replies: 9
    Last Post: 02-20-2010, 07:22 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •