Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Border question

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Border question

    I am having trouble understanding exactly how borders work within the context of a JPanel, especially in regards to using paintComponent. Say I have a JPanel that needs 100x100 of free space to draw and I want a border 10 pixels long on each side; do I have to account for that in my setSize() call? For example, would i do setSize(100,100) or setSize(120, 120)?

    And if I want to use paintComponent to draw a rectangle so that it touches the left side of this border, do I need to set the border width as my x-value? I have tried this but it always ends up a few extra pixels to the left. Can anyone give me a definitive explanation on what effect borders have on the absolute positioning of graphics?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Border question

    I don't know the answer to this right off hand, but if I needed to know, one possible way to find out is to create a small test program that checks out what happens with different settings of border size and component size. Note that I much prefer working with a component's preferredSize rather than its size, and then let my layout managers set the actual size taking the preferredSize into account.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Border question

    For example:

          JLabel label = new JLabel("Foo");
          label.setPreferredSize(new Dimension(200, 200));
          label.setBorder(BorderFactory.createLineBorder(Color.black, 85));
          JOptionPane.showMessageDialog(null, label);

    And now I see the answer and thus know the answer.

Similar Threads

  1. Problem with border in program
    By kc120us in forum AWT / Java Swing
    Replies: 2
    Last Post: January 25th, 2012, 07:57 PM
  2. JFrame invisible border issue
    By gargamel7 in forum AWT / Java Swing
    Replies: 2
    Last Post: September 25th, 2011, 05:32 PM
  3. how two create two 8*8 button border
    By mr.p4r4d0x in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 26th, 2011, 11:25 AM
  4. About Title border in jscrollpane
    By subhvi in forum AWT / Java Swing
    Replies: 2
    Last Post: December 15th, 2009, 12:45 AM