Results 1 to 4 of 4
  1. #1
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default component centering

    Hi, I'm new in Java programming... I'd like to set a Component exactly in center of panel... it should be both horizontally and vertically... I've looked for it and I've found nothing... can you give me any advice please? Thnx

  2. #2
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    Java Code:
    MyPanel.add( component, BorderLayout.CENTER );

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Or if you want the component centered, but you don't want it to expand out to fill the container (as BorderLayout will try to do), you can use a GridBagLayout and just add the component (if it's the only thing being added to that container).

    e.g.,
    Java Code:
        JLabel centerLabel = new JLabel("Centered");
        centerLabel.setBorder(new LineBorder(Color.black));
        
        JPanel panel = new JPanel(new GridBagLayout());
        panel.add(centerLabel);

    Or if you want to see this in a small program:
    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridBagLayout;
    
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    
    public class CenterComponent {
    
      private static void createAndShowUI() {
        JLabel centerLabel = new JLabel("Centered");
        centerLabel.setBorder(new LineBorder(Color.black));
        
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setPreferredSize(new Dimension(500, 500));
        panel.add(centerLabel);    
        
        JFrame frame = new JFrame("CenterComponent");
        frame.getContentPane().add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }
    Last edited by Fubarable; 01-24-2010 at 10:29 PM.

  4. #4
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default

    Thank you... this one I've never tried... I've tried only basic layout managers (BorderLayout, BoxLayout, FlowLayout and GridLayout)... I'll learn GridBagLayout too, it could be useful ;-)... thnx

Similar Threads

  1. centering a label inside a rectangle
    By Brain_Child in forum New To Java
    Replies: 3
    Last Post: 11-19-2009, 05:22 PM
  2. Centering inside a JFrame
    By kahaj in forum AWT / Swing
    Replies: 9
    Last Post: 09-23-2009, 07:23 PM
  3. add component to component
    By Omarero in forum New To Java
    Replies: 7
    Last Post: 05-17-2009, 02:39 PM
  4. Find Component by name
    By amjad905 in forum Advanced Java
    Replies: 9
    Last Post: 04-25-2009, 05:55 PM
  5. Centering text of output
    By dch414 in forum New To Java
    Replies: 2
    Last Post: 10-02-2008, 10:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •