Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By jashburn
  • 1 Post By jim829

Thread: How to create insets for a JPanel?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default How to create insets for a JPanel?

    Is there a way to create insets for the JPanel without using the GridBagLayout, or createEmptyBorder() because the panel already wants to have an etched border?

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: How to create insets for a JPanel?

    One way is to override the getInsets() method in your JPanel and return an Insets
    object with the desired insets. Read the API documentation for more info.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: How to create insets for a JPanel?

    Another way is by using a CompoundBorder, with an EmptyBorder as the outside border, and an EtchedBorder as the inside border. E.g.,
    Java Code:
    Border insetEtchedBorder = new CompoundBorder(
        new EmptyBorder(5, 5, 5, 5), new EtchedBorder(EtchedBorder.LOWERED));
    See also How to Use Borders (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Zarah likes this.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: How to create insets for a JPanel?

    I would recommend you use jashburn's way of doing it. Overriding methods which are not documented to be overridden can be risky as the internals of those classes could change in the future and possible break your application.

    Regards,
    Jim
    Zarah likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. How can I create a JPanel inside a JFrame?
    By dredd in forum New To Java
    Replies: 1
    Last Post: 04-05-2013, 09:59 PM
  2. How to create a jbutton to save the jpanel as a jpeg??
    By wet543dftgd in forum New To Java
    Replies: 4
    Last Post: 01-11-2012, 07:23 AM
  3. How do you set insets on a panel?
    By DerekRaimann in forum AWT / Swing
    Replies: 3
    Last Post: 12-19-2010, 02:39 PM
  4. weird insets change in JFrame subclass
    By sowilo in forum AWT / Swing
    Replies: 4
    Last Post: 11-18-2009, 12:27 AM
  5. [SOLVED] How to set insets for JComboBox.......?
    By prabhurangan in forum AWT / Swing
    Replies: 3
    Last Post: 07-01-2008, 06:25 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
  •