Why can't I add EmptyBorder to a custom JComponent?

My code cannot set the border of a custom JComponent object new EmptyBorder(50, 50, 50, 50). This is similar to my monitor (as you can see, it obviously works for JButton, but not for custom JComponent):

enter image description here

import java.awt.*;

import javax.swing.*;
import javax.swing.border.*;

public class Sandbox
{
    public static void main(String[] args)
    {
        // Instantiate components needed.
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        frame.add(panel);
        WideComponent wc = new WideComponent();
        JButton button = new JButton("Test");

        // Add borders
        EmptyBorder empty1 = new EmptyBorder(50, 50, 50, 50);
        wc.setBorder(empty1);
        EmptyBorder empty2 = new EmptyBorder(50, 50, 50, 50);
        button.setBorder(empty2);

        // Add stuff to panel
        panel.add(wc);
        panel.add(button);

        // Add panel to frame, show frame
        frame.add(panel);
        frame.setVisible(true);
        frame.pack();
    }
}

class WideComponent extends JComponent
{
    final int WIDTH = 500;
    final int HEIGHT = 150; 

    // drawing method
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
    }

    // JComponent sizing methods
    public Dimension getPreferredSize()
    {
        return new Dimension(WIDTH, HEIGHT);
    }
    public Dimension getMaximumSize() { return getPreferredSize(); }
    public Dimension getMinimumSize() { return getPreferredSize(); }    
}
+4
source share
1 answer

Your borders work fine - but it's just that the empty frame turns blue because you draw the entire component and the border is part of that component. For instance:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class Sandbox {
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      JPanel panel = new JPanel();
      frame.add(panel);
      WideComponent wc = new WideComponent();
      JButton button = new JButton("Test");

      wc.setBorder(createBorder());
      button.setBorder(createBorder());

      panel.add(wc);
      panel.add(button);

      frame.add(panel);
      frame.setVisible(true);
      frame.pack();
   }

   private static Border createBorder() {
      Border innerBorder = BorderFactory.createLineBorder(Color.red);
      int eb = 50;
      Border outerBorder = BorderFactory.createEmptyBorder(eb, eb, eb, eb);
      Border comboBorder = BorderFactory.createCompoundBorder(outerBorder,
            innerBorder);
      return comboBorder;
   }
}

class WideComponent extends JComponent {
   final int WIDTH = 500;
   final int HEIGHT = 150;

   public WideComponent() {
      setOpaque(true);
   }

   // drawing method
   public void paintComponent(Graphics g) {
      super.paintComponent(g);
       g.setColor(Color.BLUE);
       g.fillRect(0, 0, WIDTH, HEIGHT);
   }

   // JComponent sizing methods
   public Dimension getPreferredSize() {
      return new Dimension(WIDTH, HEIGHT);
   }

   public Dimension getMaximumSize() {
      return getPreferredSize();
   }

   public Dimension getMinimumSize() {
      return getPreferredSize();
   }
}

If you need an empty frame around your WideComponent, then paste it inside the JPanel and give the border to the JPanel socket. For example, the above code can be changed with the addition of:

  JPanel nestingJPanel = new JPanel();
  nestingJPanel.add(new WideComponent());
  nestingJPanel.setBorder(createBorder());

  panel.add(wc);
  panel.add(button);
  panel.add(nestingJPanel);
+2

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


All Articles