0

enter image description here

Noticeably thicker on the left side. Using a Matte Border does not produce this. What I want to know is why LineBorder would create this and not MatteBorder.

This is a JButton being placed in a JPanel which is then being added to a JFrame.

Relevant code:

package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;



public class View extends JPanel{ 

    public View(){
        JPanel displayPanel = new JPanel();
        displayPanel.setBackground(Color.WHITE);
        displayPanel.setSize(500, 500);

        JButton backspace = createButton("\u2190", "backspace", Color.RED, null, new Controller());

        backspace.setOpaque(false);
        backspace.setSize(25, 25);
        backspace.setBorder(new LineBorder(Color.RED, 1));
        backspace.setMinimumSize(new Dimension(25, 25));
        backspace.setPreferredSize(new Dimension(25, 25));

        displayPanel.add(backspace);

        add(displayPanel);
    }


   private JButton createButton (String text, String ac, Color fg, Color bg, ActionListener handler){
       JButton button = new JButton();
       button.setText(text);
       button.setForeground(fg);
       button.setBackground(bg);
       if (ac!= null){
           button.setActionCommand(ac);
       }
       button.setFont(new Font(button.getFont().getName(), button.getFont().getStyle(), 20));
       button.addActionListener(handler);
       return button;
   }

    private class Controller implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            String foo = e.getSource().toString();
            System.out.println(foo);
            //display.setText(button.getActionListeners().toString());

        }

    }

}

And the main:

package test;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class Foo{
       public static void main (String args[]){
            final int height = 400;
            final int width =  330;
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
            EventQueue.invokeLater(new Runnable(){


        @Override
        public void run() {
         JFrame frame = new JFrame();
         frame.setSize(330, 400);

         frame.setLocation((screen.width - width )/2, (screen.height - height)/2);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setResizable(false);
         frame.setContentPane(new View());
         frame.setVisible(true);    
       }
      });   
     }
    }

If I switch it to

  backspace.setBorder(new MatteBorder(1, 1, 1, 1, Color.RED));

It displays as desired.

What is causing the thickness inconsistency in LineBorder?

TheFaster
  • 243
  • 4
  • 18
  • Post a [SSCCE](http://sscce.org/) that demonstrates this problem. I have never seen this issue whenever I use a LineBorder (just retested and still don't see a problem). So the problem may be with some other code. Or it could be a version/platform issue. So the SSCCE should just be the frame and the button and we should be able to compile and execute the code. – camickr Oct 03 '15 at 16:04
  • Done. Should be all the relevant code. – TheFaster Oct 03 '15 at 16:14
  • See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Oct 03 '15 at 16:22
  • `Should be all the relevant code.` - did you test it? Doesn't compile for me. There are no imports. There is no Controller class. There is no "view" variable. All the code should be contained in a single class file that we can copy/paste/compile/test. – camickr Oct 03 '15 at 16:24
  • @AndrewThompson I tried using a Flow Layout and it consistently re-sized the button without forcing it to keep it's form. That was the solution elsewhere on SO. What would be the correct solution in this case? – TheFaster Oct 03 '15 at 16:24
  • *"What would be the correct solution in this case?"* Most likely appropriate layouts and the other things mentioned above(1). Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 1) Or rather, here: ... – Andrew Thompson Oct 03 '15 at 16:30
  • ... here: Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 03 '15 at 16:32
  • @camickr Updated and tested. Try it now. – TheFaster Oct 03 '15 at 16:42

1 Answers1

0

On a lark, decided to remove 1.8 from my buildpath and then re-add the same Library back. Button draws fine now.

This is in Eclipse Luna, which is known to have some buildpath quirks (such as JavaFX not importing properly unless you do the above trick as well. )

TheFaster
  • 243
  • 4
  • 18