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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:55:07+00:00 2026-05-28T06:55:07+00:00

I originally posted a question here I’ve discovered that the JTextField only resizes if

  • 0

I originally posted a question here

I’ve discovered that the JTextField only resizes if the JScrollPane exists. In other words, I can minimize and maximize it all I want until the scrollbar appears (because there is too much text to fit into the window). After that if I minimize the window, the JTextField triples in vertical size. Hopefully this new information will spark a possible solution. Please post if you have any ideas, thank you.

  • 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-05-28T06:55:08+00:00Added an answer on May 28, 2026 at 6:55 am

    I am not much of a guy who will use GridBagLayout, seems like the whole issue is with your Layout settings in that.

    Seems like you missed to read the GridBagLayout Tutorials, it clearly states that ” it is possible to reuse the same GridBagConstraints instance for multiple components, even if the components have different constraints. However, it is recommended that you do not reuse GridBagConstraints, as this can very easily lead to you introducing subtle bugs if you forget to reset the fields for each new instance.”

    So after reading that I made a small improvement in your code of my own, hope you wont mind that 🙂

    What i did, is that, I made two different objects of GridBagConstraints, one each of your scrollpane and textfield, so that they can have different values. Since you were using the same object for both the components i.e.

    c.weightx = 1.0;
    c.weighty = 1.0;
    

    Now till scroll bar doesn’t appears all goes well, but once it does and you minimize the window, and then restores the window, then while repainting the window on the screen, since the values for weights is the same for both the components, hence they try to occupy equal area of the window in terms of Y-axis.

    So in order to avoid that, I made two objects with different values for weights along Y-Axis, where JTextArea being mighty has been provided with the higher value i.e. weighty = 0.8 and JTextField being not so mighty in terms of its use in the present scenario, hence it’s been given a weight of weighty = 0.2.

    I had rebuild the code again for your extra understanding. Using different GridBagConstraint objects for different components is the way out of the mess.

    Do have a look at the code now :

    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class Console extends JPanel implements ActionListener
    {
        boolean ready = false;
        protected JTextField textField;
        protected JTextArea textArea;
        private final static String newline = "\n";
        //Game m_game;
        Thread t;
    
        public Console(/*Game game*/) 
        {
            super(new GridBagLayout());
    
            //m_game = game;
            textField = new JTextField(20);
            textField.setPreferredSize(null);
            textField.addActionListener(this);
    
            textArea = new JTextArea(20, 60);
            textArea.setEditable(true);
            textArea.setWrapStyleWord(true);
            textArea.setLineWrap(true);
            Font comic = new Font("Serif", Font.PLAIN, 14);
            textArea.setFont(comic);
            JScrollPane scrollPane = new JScrollPane(textArea);
    
            //Add Components to this panel.
                // This first object serves as providing values to the TEXTAREA.
            GridBagConstraints c = new GridBagConstraints();
            c.gridwidth = GridBagConstraints.REMAINDER;
    
            c.fill = GridBagConstraints.HORIZONTAL;
            c.fill = GridBagConstraints.BOTH;
            c.weightx = 1.0; 
            c.weighty = 0.8;// This being mighty has been given weight as 0.8, the highest.
    
            // This second object serves as providing values to the TEXTFIELD.
            GridBagConstraints c1 = new GridBagConstraints();
            c1.gridwidth = GridBagConstraints.REMAINDER;
    
            c1.fill = GridBagConstraints.HORIZONTAL;
            c1.fill = GridBagConstraints.BOTH;
            c1.weightx = 1.0;
            c1.weighty = 0.2;  // Since this has to occupy less space, hence weight is less also.
            add(scrollPane, c);
            add(textField, c1);
        }
    
        public void actionPerformed(ActionEvent evt) 
        {
            String text = textField.getText();
            //m_game.input = text;
            textField.selectAll();
            textArea.setCaretPosition(textArea.getDocument().getLength());
            //m_game.wait = false;
            /*synchronized (m_game)
            {
                ready = true;
                m_game.notifyAll();
            }*/
        }
    
        public static void createAndShowGUI() 
        {
            //Create and set up the window.
            JFrame frame = new JFrame("Game");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Add contents to the window.
            frame.setContentPane(new Console());
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        createAndShowGUI();
                    }
                });
        }
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a question that extends from the originally posted here: Link to loading-xaml
This is a slightly updated question that was originally posted here: XSL - Finding
Question originally posted on Webmasters , was recommended to move it here. I host
Note this question was originally posted in 2009, before C++11 was ratified and before
Originally posted on Server Fault , where it was suggested this question might better
I originally started this question in another thread, but that thread was sorta, kinda
I originally posted a question about why did the elements of my webservice return
EDIT: I figured out the answer to the original YUI3 question I posted here,
I originally posted this question over at the Zend Forums but figured it would
Originally this bug was posted here: https://rails.lighthouseapp.com/projects/8994/tickets/5713-ruby-19-ku-incompatible-with-mem_cache_store And now, as we've run into the

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.