How to apply EtchedBorder only to a specific side of the border?

I have a JPanel where I want to add EtchedBorder.Lowered only to the left border. I know this is possible with MatteBorders, but I was wondering if there is a way to do this with EtchedBorder?

+4
source share
4 answers
  • use JSeparator instead of EtchedBorder.Lowered,

  • but JSeparator by default is just a string, but you can create fun borders

enter image description here

import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSeparator; import javax.swing.SwingUtilities; public class NestedLayout { private JFrame frame = new JFrame(); private JPanel leftPanel = new JPanel(); public NestedLayout() { leftPanel.setLayout(new BorderLayout()); leftPanel.setBorder(BorderFactory.createEmptyBorder( 10, //top 10, //left 10, //bottom 10)); //right leftPanel.add(new JSeparator(JSeparator.VERTICAL), BorderLayout.CENTER); leftPanel.setPreferredSize(new Dimension(40, 40)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(leftPanel, BorderLayout.WEST); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new NestedLayout(); } }); } } 
+5
source

You can join two panels and add an “opposite” matte border on them to recreate the etching effect:

 import javax.swing.*; import java.awt.*; public class MainFrame extends JFrame { public MainFrame() { setLayout(new BorderLayout()); JPanel left = new JPanel(); left.setPreferredSize(new Dimension(200, 400)); left.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.GRAY)); add(left, BorderLayout.WEST); JPanel center = new JPanel(); center.setPreferredSize(new Dimension(200, 400)); center.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 0, Color.WHITE)); add(center, BorderLayout.CENTER); setVisible(true); pack(); } public static void main(String[] args) { new MainFrame(); } } 

enter image description here

+4
source

You can easily create your own border class by subclassing EtchedBorder or AbstractBorder.

This code will do exactly what you requested: EtchedBorder, drawn on one side only. But it doesn’t look very good. EtchedBorder simply draws two lines (one dark, one light) on each side. It gets a three-dimensional view of how the lines meet in the corners. Without full drawing, it does not look 3-dimensional.

 setBorder(new EtchedBorder() { public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { g.setColor(getShadowColor(c)); g.drawLine(x, y + height - 2, x, y); g.setColor(getHighlightColor(c)); g.drawLine(x+1, y + height - 3, x+1, y + 1); } }); 
+2
source

An extension of the answer given by Yanfleya, where you create the “opposite” of two different panels to reproduce the appearance of the engraved border.

You can create it in one panel using CompoundBorder , for example:

  CompoundBorder border = BorderFactory.createCompoundBorder( BorderFactory.createMatteBorder(0, 0, 1, 0, Color.WHITE), BorderFactory.createMatteBorder(0, 0, 1, 0, Color.GRAY) ); // This would create A line on the bottom 

The advantage of this method is that you can also do two sides, which then look like this: (Note that this is just a grid of nine panels and only the middle has a border that is also thicker than 1 See the code below, I used setBackground on panels to show where each panel was))

enter image description here !

 import javax.swing.*; import java.awt.*; public class Test { public static void main(String[] args) { JFrame f = new JFrame("A Demo"); f.setLayout(new GridLayout(3,3)); f.setSize(400,400); JPanel j1 = new JPanel(); //j1.setBackground(new Color(255,0,0)); JPanel j2 = new JPanel(); //j2.setBackground(new Color(200,0,0)); JPanel j3 = new JPanel(); //j3.setBackground(new Color(150,0,0)); JPanel j4 = new JPanel(); //j4.setBackground(new Color(100,0,0)); JPanel j5 = new JPanel(); //j5.setBackground(new Color(0,255,0)); JPanel j6 = new JPanel(); //j6.setBackground(new Color(0,200,0)); JPanel j7 = new JPanel(); //j7.setBackground(new Color(50,0,0)); JPanel j8 = new JPanel(); //j8.setBackground(new Color(0,150,0)); JPanel j9 = new JPanel(); //j9.setBackground(new Color(0,100,0)); j5.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createMatteBorder(0, 0, 2, 2, Color.WHITE), BorderFactory.createMatteBorder(0, 0, 2, 2, Color.GRAY) )); f.add(j1); f.add(j2); f.add(j3); f.add(j4); f.add(j5); f.add(j6); f.add(j7); f.add(j8); f.add(j9); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } 
+2
source

Source: https://habr.com/ru/post/1416661/


All Articles