How to set border on jPanel?

162,720

Solution 1

Possibly the problem is your two constructor overloads, one that sets the border, the other that doesn't:

public GoBoard(){
    this.linien = 9;
    this.setBorder(BorderFactory.createEmptyBorder(0,10,10,10)); 
}

public GoBoard(int pLinien){
    this.linien = pLinien;

}

If you create a GoBoard object with the second constructor and pass an int parameter, the empty border will not be created. To fix this, consider changing this so both constructors set the border:

// default constructor
public GoBoard(){
    this(9);  // calls other constructor
}

public GoBoard(int pLinien){
    this.linien = pLinien;
    this.setBorder(BorderFactory.createEmptyBorder(0,10,10,10)); 
}

edit 1: The border you've added is more for controlling how components are added to your JPanel. If you want to draw in your one JPanel but have a border around the drawing, consider placing this JPanel into another JPanel, a holding JPanel that has the border. For e.g.,

class GoTest {
   private static final int JB_WIDTH = 400;
   private static final int JB_HEIGHT = JB_WIDTH;

   private static void initGui() {
      JFrame frame = new JFrame("GoBoard");
      GoBoard jboard = new GoBoard();
      jboard.setLayout(new BorderLayout(10, 10));

      JPanel holdingPanel = new JPanel(new BorderLayout());
      int eb = 20;
      holdingPanel.setBorder(BorderFactory.createEmptyBorder(0, eb, eb, eb));
      holdingPanel.add(jboard, BorderLayout.CENTER);
      frame.add(holdingPanel, BorderLayout.CENTER);
      jboard.setPreferredSize(new Dimension(JB_WIDTH, JB_HEIGHT));

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //!! frame.setSize(400, 400);
      frame.pack();
      frame.setVisible(true);
   }

// .... etc....

Solution 2

JPanel jPanel = new JPanel();

jPanel.setBorder(BorderFactory.createLineBorder(Color.black));

Here not only jPanel, you can add border to any Jcomponent

Solution 3

An empty border is transparent. You need to specify a Line Border or some other visible border when you set the border in order to see it.

Based on Edit to question:

The painting does not honor the border. Add this line of code to your test and you will see the border:

    jboard.setBorder(BorderFactory.createEmptyBorder(0,10,10,10)); 
    jboard.add(new JButton("Test"));  //Add this line
    frame.add(jboard);

Solution 4

Swing has no idea what the preferred, minimum and maximum sizes of the GoBoard should be as you have no components inside of it for it to calculate based on, so it picks a (probably wrong) default. Since you are doing custom drawing here, you should implement these methods

Dimension getPreferredSize()
Dimension getMinumumSize()
Dimension getMaximumSize()

or conversely, call the setters for these methods.

Solution 5

BorderLayout(int Gap, int Gap) or GridLayout(int Gap, int Gap, int Gap, int Gap)

why paint Border() inside paintComponent( ...)

    Border line, raisedbevel, loweredbevel, title, empty;
    line = BorderFactory.createLineBorder(Color.black);
    raisedbevel = BorderFactory.createRaisedBevelBorder();
    loweredbevel = BorderFactory.createLoweredBevelBorder();
    title = BorderFactory.createTitledBorder("");
    empty = BorderFactory.createEmptyBorder(4, 4, 4, 4);
    Border compound = BorderFactory.createCompoundBorder(empty, xxx);
    Color crl = (Color.blue);
    Border compound1 = BorderFactory.createCompoundBorder(empty, xxx);
Share:
162,720
DarkLeafyGreen
Author by

DarkLeafyGreen

Tackling Complexity in the Heart of Software

Updated on July 09, 2022

Comments

  • DarkLeafyGreen
    DarkLeafyGreen almost 2 years

    My projects constists of two classes, GoBoard extends JPanel.

    GoTest.java:

    import javax.swing.*;
    import java.awt.Graphics;
    import java.io.*;
    import java.awt.*;
    
    import javax.swing.border.Border;
    import javax.swing.border.LineBorder;
    
    class GoTest{
        private static void initGui(){
            JFrame frame = new JFrame("GoBoard");
            GoBoard jboard = new GoBoard();
            jboard.setLayout(new BorderLayout(10,10));
            jboard.setBorder(BorderFactory.createEmptyBorder(0,10,10,10)); 
            frame.add(jboard);
    
    
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 400);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args){
            javax.swing.SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    initGui();
                }
            });
        }
    }
    

    GoBoard.java:

    import javax.swing.*;
    import java.awt.Graphics;
    import javax.swing.border.Border;
    class GoBoard extends JPanel{
        private int linien;
    
        public GoBoard(){
            this(9);    
        }
    
        public GoBoard(int pLinien){
            this.linien = pLinien;
            this.setBorder(BorderFactory.createEmptyBorder(0,10,10,10)); 
        }
    
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            int d = 0;
            int h = 0;
            for(int i = 0; i < this.linien; i++){
                g.drawLine(0,h, getWidth(), h);
                g.drawLine(d,0,d,getHeight());
                h += getHeight()/this.linien;
                d +=getWidth()/this.linien;
            }
        }
    }
    

    I want to set a border to have padding according to the frame where I display the panel. However I get no border. Any idea?

  • jzd
    jzd about 13 years
    @ArtWorkAD, are you sure you are calling your first constructor then? An SSCCE would be nice.
  • jzd
    jzd about 13 years
    @ArtWorkAD, please edit your question with additional code instead of pasting links in comments to code. Thanks