954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sizing Elements in a Layout Manager

I've been searching the internet for a way to specify the size of a component within a JPanel, but I haven't been able to find anything that has worked. The best answer I came across was to override the getPreferredSize(), getMaximumSize(), and getMinimumSize() methods of the component, but the component still took up the entire available space within the JPanel.

For the sake of argument, let's say I'm trying to define a custom size for a JTextArea inside a JPanel using a BoxLayout. What is the best way to stop the Layout Manager from resizing my components to take up the entire available space?

Thanks in advance for the help!

abyss776
Newbie Poster
12 posts since Oct 2008
Reputation Points: 11
Solved Threads: 1
 

What layout manager are you using?

Can you post a small program that compiles and executes and shows the problem.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Thanks for the reply! Sure, here's a short code that demonstrates the issue.

import javax.swing.*;
import java.awt.*;

public class TestSwingClass extends JFrame {
  
  public TestSwingClass() {
    initUI();
  }
  
  public void initUI() {
    setSize(500,500);
    setTitle("TestSwingClass");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    JTextArea txt = new JTextArea(5,5);
    
    //none of this seems to have any effect
    txt.setMaximumSize(new Dimension(100,100));
    txt.setMinimumSize(new Dimension(100,100));
    txt.setPreferredSize(new Dimension(100,100));
    
    JPanel pane = new JPanel(new BorderLayout());
    pane.add(txt, BorderLayout.CENTER);
    add(pane); 
  }
  
  public static void main(String args[]) {
    TestSwingClass sc = new TestSwingClass();
    sc.setVisible(true);
  }
}


As you can see, I'm using a BorderLayout. The BorderLayout seems to resize the component to take up the entirety of the center area. However, I would like to be able to set my own custom size for the JTextArea.

abyss776
Newbie Poster
12 posts since Oct 2008
Reputation Points: 11
Solved Threads: 1
 

What I do is wrap the component in a new JPanel (Flow Layout),
and place the new JPanel inside of your formatting JPanel (Border Layout).
Unless you're going for a full screen effect of your Border JPanel,
you could also wrap that in another JPanel (Flow Layout).

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

Thanks, hfx642. I actually tried that with a BoxLayout shortly before you posted that solution, and it worked well. Still, I'm wondering if there is perhaps a more direct method that doesn't involve using a different layout within the BorderLayout.

abyss776
Newbie Poster
12 posts since Oct 2008
Reputation Points: 11
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: