[Java swing] Using Border in Java
The simple border
The open border
The border with title
Combining the border
From time to time not writing about Swing, in this article I will guide you to learn how to use the Border. Literally is the contour in Java. We will turn to find out 4 contour as the category listed.
Using simple border
To create the Border, you use the following syntax, and depending on the type of border you want to create.
Border border = BorderFactory.create…
Now we will code to create some kind of simple border as shown below.
– Line border solid line is the type of border colors with certain
– Empty border border is empty, it is often used to create space, use as padding used to set the distance between content and external.
– Raised etched border border definition format is well, embossed onto.
– Lowered etched border is etched border but sank down.
– Raised bevel border bevel border format is emerging.
– Lowered bevel border create forms that border corner sink.
package nguyenvanquan7826.border; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; /** * --------------------- @author nguyenvanquan7826 --------------------- * ------------------ website: cachhoc.net ------------------- */ public class DemoSimpleBorders extends JFrame { /** create border type */ private Border lineBorder = BorderFactory.createLineBorder(Color.blue); private Border raisedEtched = BorderFactory .createEtchedBorder(EtchedBorder.RAISED); private Border loweredEtched = BorderFactory .createEtchedBorder(EtchedBorder.LOWERED); private Border raisedBevel = BorderFactory.createRaisedBevelBorder(); private Border loweredBevel = BorderFactory.createLoweredBevelBorder(); private Border empty = BorderFactory.createEmptyBorder(); /** * create border empty for JPanel contain content */ private Border panelBorder = BorderFactory .createEmptyBorder(20, 20, 20, 20); public DemoSimpleBorders() { add(createContent()); setDisplay(); } private void setDisplay() { setTitle("Demo Simple Border"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); } /** * create JPanel contain six JPanel with borders */ private JPanel createContent() { JPanel panel = new JPanel(new GridLayout(3, 2, 10, 10)); panel.setBorder(panelBorder); panel.add(createPaneBorder(lineBorder, "line border")); panel.add(createPaneBorder(empty, "empty border")); panel.add(createPaneBorder(raisedEtched, "raised etched")); panel.add(createPaneBorder(loweredEtched, "lowered etched border")); panel.add(createPaneBorder(raisedBevel, "raised bevel border")); panel.add(createPaneBorder(loweredBevel, "lowered bevel border")); return panel; } /** * create JPanel contain a border */ private JPanel createPaneBorder(Border border, String content) { JPanel panel = new JPanel(new BorderLayout()); panel.setBorder(border); JLabel lb = new JLabel(content, JLabel.CENTER); panel.add(lb); return panel; } public static void main(String[] args) { new DemoSimpleBorders(); } }
Use matte border
Matte Border is a form that allows us to use color or image to create contour. It also gives us the size to the edge again. However when taking pictures, you should set the data size is -1 Meanwhile size will appear unchanged, not lost. Code examples,.
package nguyenvanquan7826.border; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.net.URL; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.Border; /** * --------------------- @author nguyenvanquan7826 --------------------- * ------------------ website: cachhoc.net ------------------- */ public class DemoMatteBorder extends JFrame { private Border matteBorderColor; private Border matteBorderIcon; /** * create border empty for JPanel contain content */ private Border panelBorder = BorderFactory .createEmptyBorder(20, 20, 20, 20); public DemoMatteBorder() { createBorder(); add(createContent()); setDisplay(); } private void setDisplay() { setTitle("Demo Matte Border"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); } /** * create JPanel contain six JPanel with borders */ private JPanel createContent() { JPanel panel = new JPanel(new GridLayout(2, 1, 10, 10)); panel.setBorder(panelBorder); panel.add(createPaneBorder(matteBorderColor, "matte border with color")); if (matteBorderIcon != null) { panel.add(createPaneBorder(matteBorderIcon, "matte border with icon")); } else { panel.add(createPaneBorder(matteBorderColor, "don't found icon")); } return panel; } /** * create JPanel contain a border */ private JPanel createPaneBorder(Border border, String content) { JPanel panel = new JPanel(new BorderLayout()); panel.setBorder(border); JLabel lb = new JLabel(content, JLabel.CENTER); panel.add(lb); return panel; } /** * create types border */ private void createBorder() { matteBorderColor = BorderFactory.createMatteBorder(1, 1, 5, 5, Color.blue); ImageIcon icon = createIcon("icon.gif"); if (icon != null) { matteBorderIcon = BorderFactory.createMatteBorder(-1, -1, -1, -1, icon); } } /** * create a icon from a path */ private ImageIcon createIcon(String path) { URL url = getClass().getResource(path); if (url != null) { return new ImageIcon(url); } else { return null; } } public static void main(String[] args) { new DemoMatteBorder(); } }
Using the title border
Title Border is a form titled border. You can put the title in the left, right, between, on the lower border,… Also you can set the border as a border in certain forms that have been created, otherwise the default blue border will be blurred.
package nguyenvanquan7826.border; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.Border; import javax.swing.border.TitledBorder; /** * --------------------- @author nguyenvanquan7826 --------------------- * ------------------ website: cachhoc.net ------------------- */ public class DemoTitleBorder extends JFrame { /** * create border empty for JPanel contain content */ private Border panelBorder = BorderFactory .createEmptyBorder(20, 20, 20, 20); public DemoTitleBorder() { add(createContent()); setDisplay(); } private void setDisplay() { setTitle("Demo Title Border"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); } /** * create JPanel contain six JPanel with borders */ private JPanel createContent() { JPanel panel = new JPanel(new GridLayout(6, 1, 10, 10)); panel.setBorder(panelBorder); Border titleBorder; Border blueBorder = BorderFactory.createLineBorder(Color.BLUE); titleBorder = BorderFactory.createTitledBorder("title"); panel.add(createPaneBorder(titleBorder, "default title border")); titleBorder = BorderFactory.createTitledBorder(blueBorder, "title"); panel.add(createPaneBorder(titleBorder, "title border with color blue")); titleBorder = BorderFactory.createTitledBorder(blueBorder, "title", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION); panel.add(createPaneBorder(titleBorder, "title border center just and default prsition")); titleBorder = BorderFactory.createTitledBorder(null, "title", TitledBorder.CENTER, TitledBorder.ABOVE_TOP); panel.add(createPaneBorder(titleBorder, "title border center just and above top")); titleBorder = BorderFactory.createTitledBorder(blueBorder, "title", TitledBorder.RIGHT, TitledBorder.BELOW_TOP); panel.add(createPaneBorder(titleBorder, "title border right just and below top")); titleBorder = BorderFactory.createTitledBorder(null, "title", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.BELOW_BOTTOM); panel.add(createPaneBorder(titleBorder, "title border default just and below bottom")); return panel; } /** * create JPanel contain a border */ private JPanel createPaneBorder(Border border, String content) { JPanel panel = new JPanel(new BorderLayout()); panel.setBorder(border); JLabel lb = new JLabel(content, JLabel.CENTER); panel.add(lb); return panel; } public static void main(String[] args) { new DemoTitleBorder(); } }
Using Compound border
Compound Border is a combination of the border together. We can use it to create beautiful borders at will or can create content with padding or margin border through a combination of empty border with the other border by border Compound.
package nguyenvanquan7826.border; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.Border; import javax.swing.border.TitledBorder; /** * --------------------- @author nguyenvanquan7826 --------------------- * ------------------ website: cachhoc.net ------------------- */ public class DemoCompoundBorder extends JFrame { /** create border type */ private Border lineBorder = BorderFactory.createLineBorder(Color.blue); private Border raisedBevel = BorderFactory.createRaisedBevelBorder(); private Border loweredBevel = BorderFactory.createLoweredBevelBorder(); /** * create border empty for JPanel contain content */ private Border panelBorder = BorderFactory .createEmptyBorder(20, 20, 20, 20); public DemoCompoundBorder() { add(createContent()); setDisplay(); } private void setDisplay() { setTitle("Demo Compound Border"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); } /** * create JPanel contain six JPanel with borders */ private JPanel createContent() { JPanel panel = new JPanel(new GridLayout(3, 1, 10, 10)); panel.setBorder(panelBorder); Border compound = BorderFactory.createCompoundBorder(raisedBevel, loweredBevel); panel.add(createPaneBorder(compound, "raisedBevel and loweredBevel")); compound = BorderFactory.createCompoundBorder(lineBorder, compound); panel.add(createPaneBorder(compound, "add lineBorder to compound")); Border titleBorder = BorderFactory.createTitledBorder(compound, "title", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION); panel.add(createPaneBorder(titleBorder, "title border with compound")); return panel; } /** * create JPanel contain a border */ private JPanel createPaneBorder(Border border, String content) { JPanel panel = new JPanel(new BorderLayout()); panel.setBorder(border); JLabel lb = new JLabel(content, JLabel.CENTER); panel.add(lb); return panel; } public static void main(String[] args) { new DemoCompoundBorder(); } }
Posts made in series java swing guide.
References and further reading: use border BorderFactory
muon set the font color for the title then how?
Above examples of border color, then you.