Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8994959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:32:28+00:00 2026-06-15T23:32:28+00:00

I want to align a check box(red highlighted area) According to other respective fields.

  • 0

I want to align a check box(red highlighted area) According to other respective fields.

enter image description here

This is main method I am using to generate this

public class DialogTesting extends JFrame{

public static void main(String args[])
{

    JFrame frame = new JFrame();
    frame.setSize(320,250);
    frame.setLocation(400,400);

    JTextField txtUserName,txtHostName,txtPortNo,txtSID;
    JPasswordField txtPassword;
    JPanel mainPanel;
    JCheckBox chkBoxSaveConnection;

    mainPanel = new JPanel();
    mainPanel.setBorder(BorderFactory.createEmptyBorder());
    mainPanel.setPreferredSize(new Dimension(300, 210));
    mainPanel.setLayout(new FlowLayout());


    JLabel l_label = null;
    txtUserName = new JTextField("K_USERNAME", 15);
    txtUserName.putClientProperty("maxlength", 200);
    l_label = new JLabel("User Name");
    l_label.setPreferredSize(new Dimension(100, 30));
    mainPanel.add(l_label);
    mainPanel.add(txtUserName);

    txtPassword = new JPasswordField("K_PASSWORD", 15);
    txtUserName.putClientProperty("maxlength", 200);
    l_label = new JLabel("Password");
    l_label.setPreferredSize(new Dimension(100, 30));
    mainPanel.add(l_label);
    mainPanel.add(txtPassword);

    txtHostName = new JTextField("K_HOSTNAME", 15);
    txtHostName.putClientProperty("maxlength", 200);
    l_label = new JLabel("Host Name");
    l_label.setPreferredSize(new Dimension(100, 30));
    mainPanel.add(l_label);
    mainPanel.add(txtHostName);

    txtPortNo = new JTextField("K_PORTNO", 15);

    l_label = new JLabel("Port Number");
    l_label.setPreferredSize(new Dimension(100, 30));
    txtPortNo.putClientProperty("maxlength", 200);
    mainPanel.add(l_label);
    mainPanel.add(txtPortNo);


    txtSID = new JTextField("K_SID", 15);
    l_label = new JLabel("SID number");
    l_label.setPreferredSize(new Dimension(100, 30));
    txtPortNo.putClientProperty("maxlength", 200);
    mainPanel.add(l_label);
    mainPanel.add(txtSID);

    chkBoxSaveConnection = new JCheckBox();
    l_label = new JLabel("chkBoxSaveConnection");
    l_label.setPreferredSize(new Dimension(150, 30));
    mainPanel.add(l_label);
    mainPanel.add(chkBoxSaveConnection);

    mainPanel.setVisible(true);

    frame.add(mainPanel);
    frame.setVisible(true);

}
}

Here I want to make check box(Red Highlighted area) align according to other fields

I tried this solution to make it align properly

mainPanel.setLayout(new GridLayout()); 
GridBagConstraints l_bag_constraints = new GridBagConstraints();
l_bag_constraints.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(jlabel,FieldMapperHelper.getGridBagCompPosition(l_bag_constraints,0,0,10,1,0,10)
            );
    mainPanel.add(txtUserName
            ,FieldMapperHelper.getGridBagCompPosition(l_bag_constraints,0,1,10,1,0,10)
            );

But in this case it is showing me very small text box.

Please let me know if you want any thing else apart from it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:32:29+00:00Added an answer on June 15, 2026 at 11:32 pm

    Ok, so your problem is that you don’t use an appropriate LayoutManager and you are also forced to set preferred sizes (you should simply never do that).

    Here is an update version of your code which uses GridBagLayout and should works nicely:

    Result image

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class DialogTesting {
    
        protected void initUI() {
    
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JTextField txtUserName, txtHostName, txtPortNo, txtSID;
            JPasswordField txtPassword;
            JPanel mainPanel;
            JCheckBox chkBoxSaveConnection;
    
            GridBagConstraints firstCol = new GridBagConstraints();
            firstCol.weightx = 1.0;
            firstCol.anchor = GridBagConstraints.WEST;
            firstCol.insets = new Insets(5, 20, 5, 5);
    
            GridBagConstraints lastCol = new GridBagConstraints();
            lastCol.gridwidth = GridBagConstraints.REMAINDER;
            lastCol.weightx = 1.0;
            lastCol.fill = GridBagConstraints.HORIZONTAL;
            lastCol.insets = new Insets(5, 5, 5, 20);
    
            mainPanel = new JPanel(new GridBagLayout());
    
            JLabel l_label = null;
            txtUserName = new JTextField("K_USERNAME", 15);
            txtUserName.putClientProperty("maxlength", 200);
            l_label = new JLabel("User Name");
            mainPanel.add(l_label, firstCol);
            mainPanel.add(txtUserName, lastCol);
    
            txtPassword = new JPasswordField("K_PASSWORD", 15);
            txtUserName.putClientProperty("maxlength", 200);
            l_label = new JLabel("Password");
            mainPanel.add(l_label, firstCol);
            mainPanel.add(txtPassword, lastCol);
    
            txtHostName = new JTextField("K_HOSTNAME", 15);
            txtHostName.putClientProperty("maxlength", 200);
            l_label = new JLabel("Host Name");
            mainPanel.add(l_label, firstCol);
            mainPanel.add(txtHostName, lastCol);
    
            txtPortNo = new JTextField("K_PORTNO", 15);
    
            l_label = new JLabel("Port Number");
            txtPortNo.putClientProperty("maxlength", 200);
            mainPanel.add(l_label, firstCol);
            mainPanel.add(txtPortNo, lastCol);
    
            txtSID = new JTextField("K_SID", 15);
            l_label = new JLabel("SID number");
            txtPortNo.putClientProperty("maxlength", 200);
            mainPanel.add(l_label, firstCol);
            mainPanel.add(txtSID, lastCol);
    
            chkBoxSaveConnection = new JCheckBox();
            l_label = new JLabel("chkBoxSaveConnection");
            mainPanel.add(l_label, firstCol);
            mainPanel.add(chkBoxSaveConnection, lastCol);
    
            frame.add(mainPanel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String args[]) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new DialogTesting().initUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want align the div to center. like this I have tried margin-top, vertical-align
I want to align the buttons as shown bellow, for this, I used relative
I'm struggling a bit with this CSS-issue: http://jsfiddle.net/timkl/JbEX8/ I want two divs to align
i am constructing html table dynamically using java, and i want to check the
I have this html code, i want to align vertically the checbox in it's
I want to store the values based on the check-box selected sequentially.but i have
I am using Struts1.3, and in this Jsp page dynamically generate check boxes depends
This is what it outputs : I want to align 'Imprimante : MonImprimante' on
I want to align my logo at the top but failed to do so
i want to align two textboxes with their display names ,side by side in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.