Java Swing Components does not have correct Size when added to JPanel
Asked Answered
F

4

5

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.

Flavoprotein answered 14/1, 2013 at 14:53 Comment(6)
you need to use correct LayoutMonostome
Sorry if this sounds dumb: But do you mean setLayout() ? If so, i think i have to, for example, create a GridLayout. But do i have to add the Components to this Layout or how does it work?Flavoprotein
See my guidelines as an answer.Monostome
You are really not providing any Rows and Columns, which also can determine the size for the same. Use the appropriate Constructor while initializing or set Rows and Columns as suggested in the answers. Using FlowLayout (default for the JPanel) so no change needed on the Layout concerned as noted in some answers (IMHO), you will be able to see your JTextArea once you specify Row and Column for the same.Lemaster
As much as possible, restrain yourself, from specifying arbitrary values for setXxXSize() methods, let the Layout used by you, worry about that part.Lemaster
Thanks to everyone Guys! There were a lot of useful answers, and i liked them all :) (wich were useful). I chose Nikolay's Answer because i used invokeLater() in the end. Though i want to thank Gagandeep and Joey equally.Flavoprotein
M
6

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.

Monostome answered 14/1, 2013 at 15:12 Comment(5)
Searched for SwingUtilities.invoketLater() and learned that JPanel is using FlowLayout by default :). Thank youFlavoprotein
"Also you can save some bytes, not typing this. all the time." It is perhaps ironic that when using an auto-complete IDE, doing so can actually save keystrokes, in that the IDE will pop a list of method names. ;) +1 for your answer,Livery
@AndrewThompson, yes, right it is a little ironic. I think in Eclipse one can press Ctlr+Space to see list of methods and fields without typing this.Monostome
(Quickly tests both Eclipse & Netbeans - both do that) ..Nice! Thanks for the tip. :)Livery
@AndrewThompson, the same is supposed to be used in Eclipse for auto-complete.Monostome
N
2

it's all about the layout Swing layout.

Noncooperation answered 14/1, 2013 at 15:1 Comment(1)
Thanks to you and Nikolay, i will check it out again. Was reading somewhere wrong in Oracle Docs and just realized it ^^. Sorry.Flavoprotein
C
2

For your JTextArea problem, use:

       JTextArea j=new JTextArea();
       j.setColumns(20);
       j.setRows(5);

Change the values of setColumns() and setRows() to vary the size; + the suggestion given about Layout Managers.

Hope this works ;)

Crow answered 14/1, 2013 at 15:11 Comment(1)
+1 to the info, or initialize the JTextArea with a proper constructor. Layout has nothing to do with this, since JPanel has by default FlowLayout, just your answer is too right related to the issue at hand.Lemaster
P
-1

try using the absolute layout of the jpanel.

Peripeteia answered 14/1, 2013 at 16:42 Comment(2)
What is absolute layout ? Never heard of it before. Though if you referring to Absolute Positioning, then first, you should try to read the first paragraph of Absolute Positioning, regarding what the Java Docs have to say regarding this thingy, as much as possible restraint yourself from using it.Lemaster
-1 That's the worst advice one can give. You should almost never use absolute-layout.Melodramatize

© 2022 - 2024 — McMap. All rights reserved.