Results 1 to 3 of 3
  1. #1
    RKhadder is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default Centering withen a JPanel

    This is my code
    Java Code:
    public class FaciltyLogIn extends JPanel implements ActionListener
    {
        public static JPasswordField codeInput;
        public static JDialog dialog;
        public static boolean loggedIn = false;
        public static String user = "";
        
        public FaciltyLogIn()
        {
            if (!loggedIn)
            {
                JLabel label = new JLabel("Password");
                codeInput = new JPasswordField(20);
                JButton button = new JButton("OK");
                button.setActionCommand("OK");
                button.addActionListener(this);
            
                add(label);
                add(codeInput);
                add(button);
            }
            else if (loggedIn)
            {
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                JLabel timeLabel = new JLabel("You timed in at " + dateFormat.format(calendar.getTime()));
                JLabel nameLabel = new JLabel("Welcome back " + user);
                JButton button = new JButton("CLOSE");
                button.setActionCommand("CLOSE");
                button.addActionListener(this);
                
                add(timeLabel);
                add(nameLabel);
                add(button);
            }
        }
    
       ...
    
       private static void GUI()
        {
            dialog = new JDialog();
            
            FaciltyLogIn contentPane = new FaciltyLogIn();
            contentPane.setOpaque(true);
            dialog.setContentPane(contentPane);
            
            Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
            size.setSize(size.getWidth(), size.getHeight());
            
            dialog.toFront();
            dialog.setSize(size);
            dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
            dialog.setResizable(false);
            dialog.setUndecorated(true);
            dialog.setAlwaysOnTop(true);
            dialog.setVisible(true);
        }
    Now skip to the second if-statment. As you see it adds the two labels and the button. However when they are added they are right next to each other. How do I make it so they are right after each other and in the center of dialog? I tried adding the .setLayout(new GridLayout(0,1)) but it uses the whole screen so that doesnt work.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Try GridBagLayout

  3. #3
    RKhadder is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    That work nicely thank you very very much.
    Java Code:
    GridBagLayout gridBag = new GridBagLayout();
                GridBagConstraints constraints = new GridBagConstraints();
                setLayout(gridBag);
                constraints.fill = GridBagConstraints.BOTH;
                constraints.weightx = 1.0;
                constraints.gridwidth = GridBagConstraints.REMAINDER;
                gridBag.setConstraints(timeLabel, constraints);
                constraints.gridwidth = GridBagConstraints.REMAINDER;
                gridBag.setConstraints(nameLabel, constraints);
                constraints.gridwidth = GridBagConstraints.REMAINDER;
                gridBag.setConstraints(button, constraints);
    However, the button spans the whole width, from oneside of the screen to the other.

Similar Threads

  1. Centering things a content pane
    By RKhadder in forum New To Java
    Replies: 1
    Last Post: 09-28-2010, 01:43 AM
  2. component centering
    By BigBear in forum AWT / Swing
    Replies: 3
    Last Post: 01-24-2010, 11:48 PM
  3. centering a label inside a rectangle
    By Brain_Child in forum New To Java
    Replies: 3
    Last Post: 11-19-2009, 05:22 PM
  4. Centering inside a JFrame
    By kahaj in forum AWT / Swing
    Replies: 9
    Last Post: 09-23-2009, 07:23 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
  •