Results 1 to 10 of 10
  1. #1
    juhess88 is offline Member
    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Default issue with JFrame, JPanel (can't see my line - simple problem)

    I added a Line and Button to JPanel. And then I added the JPanel to the JFrame. I can only see the button. Can someone explain what I did wrong? Thanks in advance.

    I used two classes:

    Class Panel - where I add the Line and Button to the Frame.

    Java Code:
    package model;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    import javax.swing.JPanel;
    
    public class Panel {
    
    	public static void main(String[] args){
    		
    		JFrame frame = new JFrame("Practice");
    		
    		JPanel panel = new JPanel();
    		
    		JButton button = new JButton("Hello");
    		
    		frame.setVisible(true);
    		
    		frame.setSize(400, 500);
    		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Line object = new Line();
    		
    		object.drawing();
    		
    		panel.add(object);
    		
    		panel.add(button);
    		
    		frame.add(panel);
    		
    	}
    	
    }
    Class Line - where I draw the line

    Java Code:
    package model;
    
    import java.awt.Graphics;
    
    import javax.swing.JPanel;
    
    public class Line extends JPanel{
    	
    	public void drawing(){
    		
    		repaint();
    		
    	}
    	
    	public void paintComponent(Graphics g){
    		
    		super.paintComponents(g);
    		
    		g.drawLine(20, 40, 300, 280);
    		
    		
    	}
    
    }

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

    Default Re: issue with JFrame, JPanel (can't see my line - simple problem)

    Just a guess, but in the Line's paintComponent method place this:

    Java Code:
        public void paintComponent(Graphics g){
            super.paintComponents(g);
            g.drawLine(20, 40, 300, 280);
            System.out.println("Line size: " + getSize()); 
             
        }
    I think it will tell you why you're not seeing the line. Consider giving your Line class a getPreferredSize() method override to fix this.

  3. #3
    juhess88 is offline Member
    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Default Re: issue with JFrame, JPanel (can't see my line - simple problem)

    Not exactly.

    Nothing happened when i did it.

    but in line 28 I wrote

    frame.add(object) //instead of panel.add(object)

    and it worked and gave me the dimensions of my line.

    Why is this happening?

    I thought Jpanel is supposed to be a powerful drawing tool where I can do multiple actions on it at once

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

    Default Re: issue with JFrame, JPanel (can't see my line - simple problem)

    Quote Originally Posted by juhess88 View Post
    Not exactly.

    Nothing happened when i did it.
    You're not seeing a println statement showing the size of the Line JPanel?

    but in line 28 I wrote

    frame.add(object) //instead of panel.add(object)

    and it worked and gave me the dimensions of my line.

    Why is this happening?
    You're adding a JPanel with a preferredSize of 0, 0 to a FlowLayout-using container, and so the size of your Line JPanel will be 0.

    I thought Jpanel is supposed to be a powerful drawing tool where I can do multiple actions on it at once
    It is when used correctly.

    Again, consider giving the Line class a getPreferredSize() method override returning an appropriate Dimension. consider putting your JButton onto the Line JPanel.

    Avoid using setSize() anywhere and use pack() on the JFrame as well before setVisible(). Also, there's no benefit to calling repaint() on your Line JPanel so get rid of that (unless you're doing it as part of an animation).

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

    Default Re: issue with JFrame, JPanel (can't see my line - simple problem)

    For example

    Java Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import javax.swing.*;
    
    public class TestLine {
       public static void main(String[] args) {
          JFrame frame = new JFrame("Practice");
          Line panel = new Line();
          JButton button = new JButton("Hello");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          panel.add(button);
          frame.add(panel);
          
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    }
    
    class Line extends JPanel {
       private static final int PREF_W = 400;
       private static final int PREF_H = 500;
    
       public void paintComponent(Graphics g) {
          super.paintComponents(g);
          g.drawLine(20, 40, 300, 280);
          System.out.println("Line size: " + getSize());
       }
       
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    }
    Also, call pack() and setVisible(true) on the JFrame AFTER adding all components to the GUI, else it will paint itself prematurely.

  6. #6
    juhess88 is offline Member
    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Default Re: issue with JFrame, JPanel (can't see my line - simple problem)

    Thanks so much! you are amazing.

    Unfortunately I am still confused a tiny bit...

    Why am I adding the button to line? I thought I was supposed to create a panel Jpanel and add button and line to it.

    and then add the panel to the JFrame

  7. #7
    juhess88 is offline Member
    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Default Re: issue with JFrame, JPanel (can't see my line - simple problem)

    When I create my Line instance am I actually creating my own JPanel??

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

    Default Re: issue with JFrame, JPanel (can't see my line - simple problem)

    Quote Originally Posted by juhess88 View Post
    When I create my Line instance am I actually creating my own JPanel??
    You can answer that easily. Your Line class inherits all the public states and behaviors of its parent class, and so what class does Line extend?

  9. #9
    juhess88 is offline Member
    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Default Re: issue with JFrame, JPanel (can't see my line - simple problem)


  10. #10
    juhess88 is offline Member
    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Default Re: issue with JFrame, JPanel (can't see my line - simple problem)

    My problem got a little more complicated (and I tried doing this myself for a couple hours but I need some more guidance)

    I want to add a Line, Rectangle, Circle to the JFrame. But it only adds the circle and line.

    I think my mistake is that each of my classes creates it's own individual JPanel.

    How can I create a general JPanel and put Line, Circle, Rectangle in it?

    Also as you can see from line 18 circle.add(rectangle) added the rectangle to the circle panel. Why am I able to view the rectangle if it is it's own rectangle panel? And why can't I view the line when in line 20 I write circle.add(line)?


    Class Panel - adds the shapes to JFrame

    Java Code:
    package model;
    
    import java.awt.Dimension;
    import java.awt.Graphics;
    import javax.swing.*;
    
    public class Panel {
       public static void main(String[] args) {
    	   
          JFrame frame = new JFrame("Practice");
          
          Line line = new Line();
          Circle circle = new Circle();
          Rectangle rectangle = new Rectangle();
          
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
          circle.add(rectangle); //adds rectangle
          
          circle.add(line); //doesn't add line
          
          frame.add(circle);
          
          
          
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    }
    Class Line - draws a line

    Java Code:
    package model;
    
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.JPanel;
    
    class Line extends JPanel {
       private static final int PREF_W = 400;
       private static final int PREF_H = 500;
    
       public void paintComponent(Graphics g) {
          super.paintComponents(g);
          g.drawLine(20, 40, 300, 280);
          System.out.println("Line size: " + getSize());
       }
       
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    }
    Class Rectangle - draws rectangle

    Java Code:
    package model;
    
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.JPanel;
    
    class Rectangle extends JPanel {
       private static final int PREF_W = 400;
       private static final int PREF_H = 500;
    
       public void paintComponent(Graphics g) {
          super.paintComponents(g);
          g.drawRect(200, 220, 30, 40);
          System.out.println("Rectangle size: " + getSize());
       }
       
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    }
    Class Circle - draws circle

    Java Code:
    package model;
    
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.JPanel;
    
    class Circle extends JPanel {
       private static final int PREF_W = 400;
       private static final int PREF_H = 500;
    
       public void paintComponent(Graphics g) {
          super.paintComponents(g);
          g.drawOval(10, 20, 50, 50);
          System.out.println("Circle size: " + getSize());
       }
       
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    }

Similar Threads

  1. Replies: 5
    Last Post: 04-06-2012, 09:59 PM
  2. JPanel on JFrame wierd problem
    By IndiDev in forum AWT / Swing
    Replies: 3
    Last Post: 02-06-2012, 07:30 PM
  3. Replies: 2
    Last Post: 01-22-2011, 12:27 PM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  5. Simple JFrame Problem
    By DC1 in forum New To Java
    Replies: 4
    Last Post: 06-06-2008, 07:09 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
  •