0

I am trying to put a border around the buttons inside of my JPanel. I have this:

 Border lineBorder = BorderFactory.createLineBorder(Color.black);

 welcomePanel.setBorder(lineBorder);

But that only puts the border around my entire application window, which makes sense. I am wanting to be able to place the border where I want.... I did this when placing my buttons in the desired location

button1.setBounds(10, 10, 60, 30);

And I looked in the API and saw a paintBorder method with parameters of int x, int y, int width, int height, which would make sense to me, but I couldn't get it to work.

Any advicewould be appreciated

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
javaGeek
  • 304
  • 1
  • 4
  • 11
  • 2
    As has been [told to you previously](http://stackoverflow.com/a/22844908/522444), you don't want to use null layouts. Using these will have you painting yourself into a corner, and your use of these is directly contributing to your current problems. **Edit**: Better still, study @MadProgrammer's answer for some excellent advice. – Hovercraft Full Of Eels Apr 11 '14 at 02:02
  • *"Any advicewould be appreciated"* I advise you to ask an explicit question. – Andrew Thompson Apr 11 '14 at 02:38
  • I am well aware "setBounds" is not the correct way, in this instance it has to be used because that is what was asked for in the requirements. – javaGeek Apr 11 '14 at 02:42

1 Answers1

2

Start by adding you buttons to another JPanel...

JPanel buttons = new JPanel();
buttons.setLayout(...);
// add buttons...

Set the Border of this panel...

buttons.setBorder(BorderFactory.createLineBorder(Color.black));

Add this to you main container...

welcomePanel.add(buttons);

Pixel perfect layouts are an illusion in modern UI design, you don't control factors like font choices, rendering pipelines, DPI and other factors which will change the requirements needed for each component to be positioned, that is the role of layout managers.

Swing has been designed to work with layout managers, attempting to do without will cause no end of issues and frustration as you try and find more hacks to get around the issues.

You can use things like EmptyBorders to introduce empty space between components or Insets with a GridBagLayout

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you for your answer, I will take this into account in future programs where I am able to use it. – javaGeek Apr 11 '14 at 02:43