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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:40:12+00:00 2026-06-17T08:40:12+00:00

It’s my first Post here, so forgive me please if i’m doing something wrong.

  • 0

It’s my first Post here, so forgive me please if i’m doing something wrong.

My Problem is:

I am trying to add Components to a JPanel with defined values for Size etc.
But when i add them to the Panel, they do absolutely not have the Size and Location they should have.
For example:

public class Console extends JFrame {
    private JPanel mainPanel = new JPanel();
    private JTextArea textField = new JTextArea();
    private JTextArea textField2 = new JTextArea();

    public Console() {
        this.setSize(500,300);

        this.mainPanel.setSize(this.getWidth(),this.getHeight());

        this.textField.setEditable(false);
        this.textField.setSize(this.mainPanel.getWidth(), 100);
        this.textField.setPreferredSize(new Dimension(this.mainPanel.getWidth(),this.mainPanel.getHeight()));
        this.textField.setLocation(0, 0);
        this.textField.setText("some text");
        this.textField.setVisible(true);

        this.textField2.setSize(this.mainPanel.getWidth(),200);
        this.textField2.setPreferredSize(new Dimension(this.getWidth(),this.getHeight()));
        this.textField2.setLocation(0,this.mainPanel.getHeight()-this.textField.getHeight());
        this.textField2.setText("blabla");
        this.textField2.setVisible(true);

        this.mainPanel.add(textField);
        this.mainPanel.add(textField2);
        this.mainPanel.setVisible(true);

        this.add(this.mainPanel);
        // I know you should not call setVisible() in the Constructor, just for making Code more simple here.
        this.setVisible(true);
    }
}

When i start the Application, both JTextArea’s are really small and somewhere in the middle (not as set above) while the mainPanel is correct.I tried to call setSize() and setPreferredSize() in different Places in the Code, but it didn’t work. I know it is better to use a LayoutManager for doing this as far as i heard but to be honest, i do not get how to use it correctly. I checked it on Oracle Doc’s but i would appreciate it if someone could post a clean Solution for this, Thanks in Advance.

  • 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-17T08:40:14+00:00Added an answer on June 17, 2026 at 8:40 am

    You need to set a proper Layout for your Container. You setLayout for a Container like JFrame, JPanel and so on. You don’t add other components to layout, but to a container. Then it would layout them accordingly. It is how it works.

    With proper layout you would not need to call setLocation(). Also setVisible(true) is excessive, because true is default values for those components in your code.

    Better not to extend JFrame, extend JPanel instead and add it to JFrame.

    Please, learn about EDT and SwingUtilities.invoketLater() you need to use it.

    Also you can save some bytes, not typing this. all the time.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a

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.