Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JButtons and Layouts and Margins (Oh My!)

807601Apr 3 2008 — edited Apr 3 2008
Ok... I'm not new to Java... just new to Swing. I'm frustrated with how some of this stuff works, and I was hoping someone could clear this little bit up for me.

I've got a JButton. I want to wipe away the margins and set the size of the margin as I like. However, depending on the layout of the JPanel, I get different results for the same lines (that should in my mind do the same thing)... Here are the lines in question.
   import javax.swing.*;
   import java.awt.*;
	
    public class XXXXXX extends JFrame {
   
       public XXXXXX()	 {
       
         //...
         
         JPanel panel = new JPanel();
         getContentPane().add(panel);
         panel.setLayout(null);
         
         JButton sendMsgBtn = new JButton("Send");
         sendMsgBtn.setBounds(200, 60, 50, 30);
         sendMsgBtn.setMargin(new Insets(0,0,0,0));
      
         panel.add(sendMsgBtn);
      }	
   
   }
When I comment out the panel.setLayout(null) line of code then the setMargins works (but the setSize part of the setBounds doesn't work). However, when I leave the line uncommented, the setSize part of setBounds works (but the setMargin doesn't work). Any ideas?

I guess the pertinent question would be this... Why doesn't the setMargin() call work since I've set the Layout to null?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 1 2008
Added on Apr 3 2008
3 comments
1,649 views