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

    Question Custom JComponent does not appear in window

    Hello all!

    For a personal project of mine, I have been trying to get a custom jcomponent into a jpanel into a frame. But it does not seem to be working; I only get a blank window. I have boiled it down to the following code:

    Java Code:
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.Graphics;
    
    public class Test extends JComponent
    {
        public void paintComponent(Graphics g)
        {
            g.drawString("hello world", 100, 100);
        }
        
        public static void main(String[] args)
        {
            JFrame f = new JFrame("Test");
            f.setSize(500, 500);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            JPanel p = new JPanel();
            Test t = new Test();
            p.add(t);
            f.add(p);
            
            f.setVisible(true);
        }
    }
    Does anybody know the problem?
    Thanks in advance!

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

    Default Re: Custom JComponent does not appear in window

    The problem is that JPanels use FlowLayout by default and FlowLayout sizes contained components at their preferredSize. The preferredSize of your JComponent is 0,0. Try overriding getPreferredSize(...)

    Java Code:
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.Graphics;
     
    public class Test extends JComponent
    {
        public void paintComponent(Graphics g)
        {
            g.drawString("hello world", 100, 100);
        }
    
        @Override
        public Dimension getPreferredSize() {
           return new Dimension(300, 300);
        }
         
        public static void main(String[] args)
        {
            JFrame f = new JFrame("Test");
            // f.setSize(500, 500); // !! get rid of this !!
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             
            JPanel p = new JPanel();
            Test t = new Test();
            p.add(t);
            f.add(p);
    
            f.pack();  // !! add !!
             
            f.setVisible(true);
        }
    }
    Caveat: code neither compiled nor tested

  3. #3
    KritSandvich is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: Custom JComponent does not appear in window

    That did it! Thanks!

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

    Default Re: Custom JComponent does not appear in window

    You're welcome!

Similar Threads

  1. Center text in a JComponent?
    By Zman3359 in forum New To Java
    Replies: 4
    Last Post: 04-17-2011, 11:44 PM
  2. Question on getGraphics() in a JComponent
    By Shayke_ in forum Java 2D
    Replies: 2
    Last Post: 02-10-2011, 08:31 PM
  3. Replies: 2
    Last Post: 10-01-2010, 08:18 PM
  4. SetBackground for JComponent
    By tulasi.neppali in forum AWT / Swing
    Replies: 5
    Last Post: 09-12-2010, 11:14 PM
  5. JComponent gradient background
    By snipered in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2008, 01:38 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
  •