Results 1 to 6 of 6
  1. #1
    K3R
    K3R is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Question Canvas Only Shows on Resize


    Hey,

    I'm creating a simple program using a JFrame with a canvas (LogoCanvas()) that is used to display a logo created in another class. I'm trying to display the canvas (logo) in the 'EAST' side of a JPanel with a BorderLayout layout.

    My issue is that the logo only displays itself when I resize the running JFrame window. When I simply run the program the logo is invisible.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import javax.swing.*;
    
    public class MyGUI extends JFrame
    {
        private JButton cdListBtn = new JButton("View CD List");
        private JButton mp3ListBtn = new JButton("View MP3 List");
        private JButton cartBtn = new JButton("View Cart");
        private JLabel titleLbl = new JLabel("Liam's GUI");
        private JPanel northPnl, southPnl;
        private LogoCanvas canvas;
        
        Font font = new Font("Tahoma", Font.BOLD, 12);
        
        public MyGUI()
        {
            super("Follow a GUI Plan");
            
            //Creates new canvas containing logo
            canvas = new LogoCanvas();
            
            //Sets fonts for three buttons and label
            cdListBtn.setFont(font); 
            mp3ListBtn.setFont(font);
            cartBtn.setFont(font);
            titleLbl.setFont(new Font("Tahoma", Font.PLAIN, 20));
            
            //Centers label so it appears in center of JPanel
            titleLbl.setHorizontalAlignment(SwingConstants.CENTER);
            
            //Creates north panel and adds title label and logo
            northPnl = new JPanel();
            northPnl.setOpaque(false);
            northPnl.setLayout(new BorderLayout());
            northPnl.add(titleLbl, BorderLayout.CENTER);
            northPnl.add(canvas, BorderLayout.EAST);
            
            //Creates south panel and adds three buttons (flow layout)
            southPnl = new JPanel();
            southPnl.setOpaque(false);
            southPnl.setLayout(new FlowLayout());
            southPnl.add(cdListBtn);
            southPnl.add(mp3ListBtn);
            southPnl.add(cartBtn);
            
            //Adds north and south panels to frame
            getContentPane().add("North", northPnl);
            getContentPane().add("South", southPnl);
            
            setSize(400, 300);
            setLocation(300, 300);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            getContentPane().setBackground(new Color(0xCC, 0xE6, 0xFF));
            setVisible(true);
        }
        
        public static void main(String[] args)
        {
            new MyGUI();
        }
    }
    Any advice would be greatly appreciated. Thanks.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Canvas Only Shows on Resize

    Try calling pack on the JFrame prior to calling setVisible

  3. #3
    K3R
    K3R is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Canvas Only Shows on Resize

    Quote Originally Posted by doWhile View Post
    Try calling pack on the JFrame prior to calling setVisible

    Adding pack(); to the MyGUI method didn't work. Adding it to the line before setVisible compressed the window size and adding it anywhere else had no effect.

    Sorry for my ignorance, I'm a student and I've never had to call "pack" before.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Canvas Only Shows on Resize

    Why do you setOpaque(false)? What are you trying to achieve by that?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Canvas Only Shows on Resize

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    K3R
    K3R is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Canvas Only Shows on Resize

    Quote Originally Posted by DarrylBurke View Post
    Why do you setOpaque(false)? What are you trying to achieve by that?

    db

    To remove the background from the north and south JPanels. I wouldn't normally, but it states to do so in the assignment brief I've been given.

Similar Threads

  1. Source tree shows error when there is none
    By madroadbiker in forum NetBeans
    Replies: 0
    Last Post: 05-10-2011, 01:29 PM
  2. when i implement forward it shows the error in server
    By akiravelmont in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 03-25-2011, 04:17 AM
  3. Environment variable shows different from editable value
    By projecttoday in forum New To Java
    Replies: 6
    Last Post: 12-01-2010, 12:47 AM
  4. Replies: 0
    Last Post: 07-05-2009, 12:31 PM
  5. JTable in Frame only shows 3 rows
    By Laura Warren in forum New To Java
    Replies: 4
    Last Post: 12-24-2008, 05:48 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
  •