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?
JPanel jPanel = new JPanel();
jPanel.setBorder(BorderFactory.createLineBorder(Color.black));
Here not only jPanel, you can add border to any Jcomponent
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);
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);
To get fixed padding, I will set layout to java.awt.GridBagLayout
with one cell.
You can then set padding for each cell. Then you can insert inner JPanel
to that cell and (if you need) delegate proper JPanel
methods to the inner JPanel
.
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.
Source: Stackoverflow.com