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 8562163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:40:19+00:00 2026-06-11T16:40:19+00:00

I have following code which is running fine, but I want to know if

  • 0

I have following code which is running fine, but I want to know if there is any method which can change the font size to fill maximum area in JTextBox. When I am running to code on three different theme it is giving me different results.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import java.awt.GridLayout;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.SwingConstants;
import javax.swing.JPasswordField;
import java.awt.Component;
public class NewLoginBox extends JDialog {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private final JPanel contentPanel = new JPanel();
    private JTextField userID;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            NewLoginBox dialog = new NewLoginBox();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public NewLoginBox() {
        setBounds(100, 100, 350, 220);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new TitledBorder(null, "Sign In", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        {
            JPanel titlePanel = new JPanel();
            contentPanel.add(titlePanel);
            titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
            {
                JLabel lblPleaseEnterYour = new JLabel("Welcome to Application. Please Sign In ");
                lblPleaseEnterYour.setAlignmentX(Component.CENTER_ALIGNMENT);
                lblPleaseEnterYour.setAlignmentY(Component.BOTTOM_ALIGNMENT);
                lblPleaseEnterYour.setHorizontalAlignment(SwingConstants.LEFT);
                titlePanel.add(lblPleaseEnterYour);
                titlePanel.add(Box.createVerticalStrut(15));
            }
        }
        {
            JPanel formPanel = new JPanel();
            contentPanel.add(formPanel);
            formPanel.setLayout(new GridLayout(0, 2, 5, 5));
            {
                JLabel lblUserId = new JLabel("User ID");
                formPanel.add(lblUserId);
            }
            {
                userID = new JTextField("", 15);
                formPanel.add(userID);
                userID.setColumns(10);
            }
            {
                JLabel lblPassword = new JLabel("Password");
                formPanel.add(lblPassword);
            }
            {
                passwordField = new JPasswordField();
                formPanel.add(passwordField);
            }
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
            {
                JButton btnHelp = new JButton("Help");
                buttonPane.add(btnHelp);
            }
        }
    }

}

Nimbus Look and feel – which is have best look and feel.

Window Look and feel

Swing Look and feel

Now what I want that the size of Jtextbox should be dynamically change according to the text inside it. so that we can have consistent look and feel.
Thanks
Ashish Tyagi

  • 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-11T16:40:20+00:00Added an answer on June 11, 2026 at 4:40 pm
    • Don’t use GridLayout for your formPanel. Instead use GridBagLayout.
    • Or consider using a 3rd party layout such as MigLayout.
    • Don’t set the bounds of your GUI’s. Instead let the layout managers do that for you.
    • Don’t forget to call pack() on your top level windows after filling them with components and before displaying them.

    For example,

    NewLoginBox.java:

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JDialog;
    import javax.swing.JPanel;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JLabel;
    import java.awt.GridLayout;
    import javax.swing.JTextField;
    import javax.swing.border.Border;
    import javax.swing.border.CompoundBorder;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.TitledBorder;
    import javax.swing.SwingConstants;
    import javax.swing.JPasswordField;
    import java.awt.Component;
    
    public class NewLoginBox extends JDialog {
       private static final long serialVersionUID = 1L;
       private final JPanel contentPanel = new JPanel();
       private JTextField userID;
       private JPasswordField passwordField;
    
       public static void main(String[] args) {
          try {
             NewLoginBox dialog = new NewLoginBox();
             dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
             dialog.pack();
             dialog.setLocationRelativeTo(null);
             dialog.setVisible(true);
          } catch (Exception e) {
             e.printStackTrace();
          }
       }
    
       public NewLoginBox() {
          //setBounds(100, 100, 350, 220);
          // getContentPane().setLayout(new BorderLayout());
          TitledBorder titledBorder = new TitledBorder(null, "Sign In",
                TitledBorder.LEADING, TitledBorder.TOP, null, null);
          int ebGap = 10;
          EmptyBorder emptyBorder = new EmptyBorder(ebGap, ebGap, ebGap, ebGap);
          Border compoundBorder = new CompoundBorder(titledBorder, emptyBorder);
          contentPanel.setBorder(compoundBorder );
          getContentPane().add(contentPanel, BorderLayout.CENTER);
          contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
          JPanel titlePanel = new JPanel();
          contentPanel.add(titlePanel);
          titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
          JLabel lblPleaseEnterYour = new JLabel(
                "Welcome to Application. Please Sign In ");
          lblPleaseEnterYour.setAlignmentX(Component.CENTER_ALIGNMENT);
          lblPleaseEnterYour.setAlignmentY(Component.BOTTOM_ALIGNMENT);
          lblPleaseEnterYour.setHorizontalAlignment(SwingConstants.LEFT);
          titlePanel.add(lblPleaseEnterYour);
          titlePanel.add(Box.createVerticalStrut(15));
    
          JPanel formPanel = new JPanel();
          contentPanel.add(formPanel);
    
          // formPanel.setLayout(new GridLayout(0, 2, 5, 5));
          ebGap = 7;
          formPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
          formPanel.setLayout(new GridBagLayout());
          JLabel lblUserId = new JLabel("User ID");
          addWithGbc(formPanel, lblUserId, 0, 0);
          userID = new JTextField("", 15);
          addWithGbc(formPanel, userID, 1, 0);
          userID.setColumns(10);
          JLabel lblPassword = new JLabel("Password");
          addWithGbc(formPanel, lblPassword, 0, 1);
          passwordField = new JPasswordField();
          addWithGbc(formPanel, passwordField, 1, 1);
          JPanel buttonPane = new JPanel();
          buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
          getContentPane().add(buttonPane, BorderLayout.SOUTH);
          JButton okButton = new JButton("OK");
          okButton.setActionCommand("OK");
          buttonPane.add(okButton);
          getRootPane().setDefaultButton(okButton);
          JButton cancelButton = new JButton("Cancel");
          cancelButton.setActionCommand("Cancel");
          buttonPane.add(cancelButton);
          JButton btnHelp = new JButton("Help");
          buttonPane.add(btnHelp);
       }
    
       private void addWithGbc(Container container, JComponent component, int x,
             int y) {
          int hGap = 5;
    
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridx = x;
          gbc.gridy = y;
          if (x % 2 == 0) {
             gbc.anchor = GridBagConstraints.WEST;
             gbc.insets = new Insets(hGap, 0, hGap, 20); // magic number!
             gbc.weightx = 1.0;
             gbc.weighty = 1.0;
             gbc.fill = GridBagConstraints.BOTH;
          } else {
             gbc.anchor = GridBagConstraints.EAST;
             gbc.insets = new Insets(hGap, 0, hGap, 0);
             gbc.weightx = 1.0;
             gbc.weighty = 0.0;
             gbc.fill = GridBagConstraints.HORIZONTAL;
          }
          container.add(component, gbc);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know is below code correct ? I have following code which
I have following code which works for radio buttons but need to be changed
I have the following code which is working, I was wondering if this can
I have the following code which is fine if I give invalid parameters (though,
I have the following code which utilises Guava's Files.readLines() method: List<String> strings = Lists.newArrayList();
I have the following code which will allowed a user running iOS to move
I have the following code which works fine on my Windows 2003 server: static
I have the following code which definitely returns a proper data result if I
I have the following code which is used to upload large files (~6MB) to
i have the following code which switches some fullscreen-background-images (fadeOut, fadeIn). setInterval(function() { var

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.