Results 1 to 1 of 1
  1. #1
    Join Date
    Apr 2010
    Rep Power

    Default Random size of JComponent

    Hi, can anyone please help me?

    I have JFrame with JLable and JPanel named pnlDraw

    pnlDraw serve for adding Canvas.
    Method in the JFrame that adds Canvas:
    Java Code:
    public Component addCanvas(JComponent comp) {
            comp.setBorder(new LineBorder(;
            comp.setBounds(0, 0, pnlDraw.getWidth(), pnlDraw.getHeight());
            return comp;
    Mostly if i anything change, save and run, Canvas is not same size as pnlDraw.
    But in next run is the size usually correct.
    (I said mostly and usually - not allways)
    If Canvas has the wrong size, the size is allways the same, and independend of the pnlDraw's size.
    I probably use default Netbeans group layout. But I tried Null layout too.

    SOLVED: Key to the solution is pack() method after creating, positioning and sizing JFrame. After then i have added Canvas.


    Last edited by Karl-von-bahnhof; 04-13-2010 at 10:54 PM.

Similar Threads

  1. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 05:37 PM
  2. easy wayto adding jcomponent to a jtable cell
    By aruna1 in forum New To Java
    Replies: 7
    Last Post: 03-30-2009, 08:36 AM
  3. Detecting mouseEntered event in JComponent
    By djhallx in forum AWT / Swing
    Replies: 1
    Last Post: 02-26-2009, 11:39 PM
  4. JComponent gradient background
    By snipered in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2008, 01:38 AM
  5. polygon-shaped JComponent
    By zenMarko in forum New To Java
    Replies: 2
    Last Post: 11-04-2008, 07:06 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