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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:45:59+00:00 2026-06-01T16:45:59+00:00

I am having two problems with the layout of my gui. I am using

  • 0

I am having two problems with the layout of my gui.
I am using a JTabbedPane to hold two JPanels, each panel has a selection of buttons and text areas, and each is laid out using GridBagLayout.
In one of my Panels I have a JScrollPane which uses a JTextArea. When I append anything to this text area and then click off the gui so it no longer has focus, or if I change the tab, the sizes of all the text fields and the text are changed to be as small as they can be.

To further illustrate my problem, here are before and after pictures of when i click off the gui after appending to the text area:
Before I click off the gui

After I click off the gui

Here is the code that I use to add the JTextArea to the Panel:

table = new JTextArea();
    table.setEditable(false);
    JScrollPane sp = new JScrollPane(table);
    sp.setSize(40, 10);
    c.insets = new Insets(10,10,10,10);
    c.gridx = 1;
    c.gridwidth = 4;
    c.gridy = 7;
    c.gridheight = 7;
    this.add(sp, c);

And here is the code I use to add the text Areas to the Panel:

title = new JTextField(10);
    author = new JTextField(10);
    dueDate = new JTextField(10);
    setDate = new JTextField(10);
    setWeighting = new JTextField(10);

    c.gridx = 2;
    c.gridy = 1;
    this.add(title, c);//add title field
    c.gridx = 2;
    c.gridy = 2;
    this.add(author, c);//add author field
    c.gridx = 2;
    c.gridy = 3;
    this.add(dueDate, c);//add dueDate field
    c.gridx = 2;
    c.gridy = 4;
    this.add(setDate, c);//add setDate field
    c.gridx = 2;
    c.gridy = 5;
    this.add(setWeighting, c);//add set Weighting field
  • 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-01T16:46:01+00:00Added an answer on June 1, 2026 at 4:46 pm

    I was able to partially reproduce your problem like so:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Foo002 {
    
       private static final int ROWS = 5;
    
       private static void createAndShowGui() {
          JPanel assignmentsPanel = new JPanel(new GridBagLayout());
          final JTextArea textarea = new JTextArea(ROWS, 20);
    
          GridBagConstraints c = new GridBagConstraints();
          int insetGap = 2;
          c.insets = new Insets(insetGap, insetGap, insetGap, insetGap);
          c.fill = GridBagConstraints.HORIZONTAL;
          c.gridwidth = 1;
          c.gridheight = 1;
          c.weightx = 1.0;
          c.weighty = 1.0;
          String[] labels = { "title", "author", "date due", "date set",
                "set weighting" };
          int row = 0;
          for (int i = 0; i < labels.length; i++) {
             JLabel label = new JLabel(labels[i], SwingConstants.CENTER);
             c.gridx = 0;
             c.gridy = i;
             assignmentsPanel.add(label, c);
             c.gridx = 1;
             JTextField textfield = new JTextField(10);
             assignmentsPanel.add(textfield, c);
    
             label.setPreferredSize(textfield.getPreferredSize());
             row++;
          }
          c.gridx = 0;
          c.gridy = row;
          c.fill = GridBagConstraints.HORIZONTAL;
          Action myAction = new AbstractAction("Fill Area") {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < 10; i++) {
                   sb.append("foo bar bif baz spam\n");
                }
                textarea.setText(sb.toString());
             }
          };
          assignmentsPanel.add(new JButton(myAction), c);
          c.gridx = 1;
          assignmentsPanel.add(new JButton("Button 2"), c);
          row++;
    
          c.gridx = 0;
          c.gridy = row;
          c.gridwidth = 2;
          c.gridheight = ROWS;
    
          JScrollPane scrollpane = new JScrollPane(textarea);
    
          assignmentsPanel.add(scrollpane, c);
    
          JTabbedPane tabbedPanel = new JTabbedPane();
          tabbedPanel.add("Assignments", assignmentsPanel);
          tabbedPanel.add("Modules", new JPanel());
    
          JOptionPane.showMessageDialog(null, tabbedPanel, "Foo",
                JOptionPane.PLAIN_MESSAGE);
    
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    Before and after adding text to JTextArea, it looks like:
    enter image description here enter image description here

    And after clicking on tabs:
    enter image description here

    But it can be fixed by giving the JScrollPane a vertical scrollbar:

      // JScrollPane scrollpane = new JScrollPane(textarea);
      JScrollPane scrollpane = new JScrollPane(textarea,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    

    Which when run looks like:
    enter image description here

    Looks good on Mac OS, too:

    enter image description here

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

Sidebar

Related Questions

I am using MVC-View Model, EF Model first I am having problems with two
IE6 is giving me issues of course. I'm having problems with my layout using
I'm having problems with JTables. I'm adding two tables to a panel, the tables
Having problems with the combining of two statements in a controller. Both statements work
I was having problems while intersecting two geometries, getting a TopologyException probably due to
We are having some problems deploying our WCF services into IIS7. We have two
I'm having some problems trying to perform a query. I have two tables, one
I'm having the following problem: When I got two labels into each other: <Label
Having two forms, each in it's own jQuery UI tab, how can I post
I have been having problems creating a relative layout in XML for a list

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.