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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:17:44+00:00 2026-06-10T12:17:44+00:00

I am coding a custom JPanel, call it MyPanel, by hand which contains two

  • 0

I am coding a custom JPanel, call it MyPanel, by hand which contains two JLabels and two JTextBoxes. This JPanel will be used in a JFrame which contains other JPanels using a CardLayout. The JFrame is non-resizeable; its size is fixed based on the JPanel which has the most components.

Since MyPanel is sparse on controls, I am trying to figure out the best layout manager to use. I want the labels and text fields to appear close to the top with all the extra space at the bottom. I believe GridBagLayout provides the functionality to get this panel to look the way I want. MyPanel has the following initComponents() method which is called from the constructor:

public class MyPanel extends JPanel {

    public static void main(String[] args) {
        JFrame frame = new JFrame("MyPanel Test");
        frame.add(new MyPanel(null));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 425);
        frame.setVisible(true);
    }

    public MyPanel() {
        initComponents();
    }

    private void initComponents() {
        this.setLayout(new GridBagLayout());

        JLabel cardYearLabel = new JLabel("Card Year:");
        cardYearLabel.setFont(new Font("Tahoma", 0, 14)); // NOI18N

        GridBagConstraints yearLabelConstraints = new GridBagConstraints();
        yearLabelConstraints.gridx = 0;
        yearLabelConstraints.gridy = 0;
        yearLabelConstraints.weightx = 1;
        yearLabelConstraints.weighty = 1;
        yearLabelConstraints.anchor = GridBagConstraints.WEST;
        yearLabelConstraints.insets = new Insets(20, 50, 10, 10);
        this.add(cardYearLabel, yearLabelConstraints);

        this.yearTextField = new JFormattedTextField();
        this.yearTextField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(new DecimalFormat("#0"))));
        this.yearTextField.setFont(new Font("Tahoma", 0, 14)); // NOI18N
        this.yearTextField.setColumns(10);
        this.yearTextField.addFocusListener(new UpdateInstructionsFocusListener("Enter card year."));

        GridBagConstraints yearTextFieldConstraints = new GridBagConstraints();
        yearTextFieldConstraints.gridx = 1;
        yearTextFieldConstraints.gridy = 0;
        yearTextFieldConstraints.weightx = 2;
        yearTextFieldConstraints.weighty = 1;
        yearTextFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
        yearTextFieldConstraints.insets = new Insets(20, 10, 10, 50);
        this.add(this.yearTextField, yearTextFieldConstraints);

        JLabel cardNumberLabel = new JLabel("Card Number:");
        cardNumberLabel.setFont(new Font("Tahoma", 0, 14)); // NOI18N

        GridBagConstraints numberLabelConstraints = new GridBagConstraints();
        numberLabelConstraints.gridx = 0;
        numberLabelConstraints.gridy = 1;
        numberLabelConstraints.weightx = 1;
        numberLabelConstraints.weighty = 1;
        numberLabelConstraints.anchor = GridBagConstraints.WEST;
        numberLabelConstraints.insets = new Insets(10, 50, 0, 10);
        this.add(cardNumberLabel, numberLabelConstraints);

        this.numberTextField = new JFormattedTextField();
        this.numberTextField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(new DecimalFormat("#0"))));
        this.numberTextField.setFont(new Font("Tahoma", 0, 14)); // NOI18N
        this.numberTextField.setColumns(10);
        this.numberTextField.addFocusListener(new UpdateInstructionsFocusListener("Enter card number."));

        GridBagConstraints numberTextFieldConstraints = new GridBagConstraints();
        numberTextFieldConstraints.gridx = 1;
        numberTextFieldConstraints.gridy = 1;
        numberTextFieldConstraints.weightx = 2;
        numberTextFieldConstraints.weighty = 1;
        numberTextFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
        numberTextFieldConstraints.insets = new Insets(10, 10, 0, 50);
        this.add(this.numberTextField, numberTextFieldConstraints);

        JPanel filler = new JPanel();
        GridBagConstraints fillerConstraints = new GridBagConstraints();
        fillerConstraints.gridx = 0;
        fillerConstraints.gridy = 2;
        fillerConstraints.gridwidth = 2;
        fillerConstraints.weightx = 1;
        fillerConstraints.weighty = 100;
        this.add(filler, fillerConstraints);
    }
}

I cringe at using a blank JPanel to fill up the empty space at the bottom of MyPanel. Is there a better, more elegant way to do this? (I’m not stuck on using GridBagLayout. If another standard LayoutManager can do this, I’m all for 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-10T12:17:45+00:00Added an answer on June 10, 2026 at 12:17 pm

    One way to achieve this is to put the controls in a panel, then add that panel to the PAGE_START of a 2nd panel with a BorderLayout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hai Freinds So far i have used this coding to create a custom Control
My friend done this below coding for custom control <a href=javascript:__doPostBack('id','msg');>click</a> now i want
This is my first attempt at coding a custom wordpress theme and I'm almost
I'm coding a report page. In this page, there are two date fields for
I'm coding a custom panel representing the hand of cards. It's a panel that
I am coding a JList with checkBoxes using a custom CellRenderer, and a context
There exist tools for comparing code against a custom specified set of coding guidelines/standards
Coding Platform ASP.NET 4.0 WebForms on IIS7.5 If I request a page blahblah.aspx which
Coding footer naively, if there's not enough content, then there will be empty space
Upon coding, I realized I had made a mistake. I used git checkout 7e57dbf5a7245...

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.