4

I've created JPanel and have already added components into it and I'm going to pass that JPanel to PopUpFactory... So can I get size of JPanel before passing it?

I put Jlabel into it and text after that and I don't know the size of that text...

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
  • The factory should size the panel according to need. If it does not, look to the answers by kleopatra and camickr on [Why does the JTable header not appear in the image?](http://stackoverflow.com/questions/7369814/why-does-the-jtable-header-not-appear-in-the-image) There the point is to create an image, but the size is necessary for the image constructor. – Andrew Thompson Oct 10 '11 at 07:46
  • hmm dont quite understand your setup - you first show the popup and afterwards set the label text from null to something else? Why/when/what controls the text setting? BTW, best to show a small runnable example to allow a quick look :-) – kleopatra Oct 10 '11 at 07:47
  • Oh, and to paraphrase the Pharaoh, 'for better help sooner, post an [SSCCE](http://pscode.org/sscce.html)'. ;) – Andrew Thompson Oct 10 '11 at 07:49

3 Answers3

3

You can set the preferred size using setPreferredSize(Dimension); e.g.

JPanel pnl = new JPanel(new BorderLayout());
pnl.setPreferredSize(new Dimension(640, 480));

This value will subsequently be obtainable by calling getPreferredSize() and will be used when laying out the component, although note that it is not guaranteed that it will actually be rendered at this size.

Why do you actually require the size prior to rendering it? Typically with Swing programming you don't need to deal with explicit dimensions / sizes as the chosen layout will take care of these specifics for you.

EDIT

To address the OP's query regarding JTextField, one option here it to call the int based constructor that accepts the anticipate number of columns. This causes the text field to be rendered wide enough to support that number of characters.

My second point addresses the comment that the setXXXSize methods should never be called directly and that the developer should rely solely on the LayoutManager. This is not always appropriate - Typically it is necessary to set the preferred size of your main application frame. For example, suppose I were writing a a simple browser application in Swing. The majority of the main frame is a JEditorPane for rendering HTML. If I do not set a preferred size for this component (or the containing frame) and I call pack() the frame is likely to be rendered as small as possible, rather than with sensible dimensions.

Adamski
  • 54,009
  • 15
  • 113
  • 152
  • I put Jlabel into it and text after that and I don't know the size of that text... So i want to get the size(not to set). – Chuck Norris Oct 10 '11 at 07:26
  • 1
    you can try getSize() or getPrefferedSize() – tiger Oct 10 '11 at 07:27
  • 2
    -1 nonono! never-ever use any of the setXXSize methods in application code, instead use a decent LayoutManager. +1 for asking the why question :-) – kleopatra Oct 10 '11 at 07:33
  • @tiger - no guessing games, please. Either you know which method to query or not (the latter with the obvious consequence ;-) – kleopatra Oct 10 '11 at 07:34
  • @kleopatra: It is entirely valid to use the setPreferredSize method; Can you find me a reference citing that it should not be used in application code? Even when using a decent LayoutManager this method is often necessary; e.g. to set the preferred size of my main application frame - Simply "packing" the frame / dialog will often make it appear very small. – Adamski Oct 10 '11 at 07:37
  • 1
    on the level of the components, it's far from perfectly valid - on the contrary it's near-to perfectly wrong for many reasons (see f.i. http://stackoverflow.com/questions/7229226/avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swing) - the top-level frame, on the other hand, can (and often is) sized as appropriate – kleopatra Oct 10 '11 at 07:42
  • @kleopatra: In my recent edit why wouldn't it would be valid to call setPreferredSize on the JEditorPane that formed the bulk of the screen real estate in my browser example? I think your argument only applies to specific components (e.g. setting the preferred height of a JTextField and then switching to a different look-and-feel would clearly be problematic). I think it's a valid thing to do for text areas, JTables, charts; i.e. components that are flexible in terms of height and width. – Adamski Oct 10 '11 at 07:45
  • yeah, I'm aware that editorPane is likes freaking out - the clean way use a decent LayoutManager and tell the _manager_ which size you want, not the component. I see othing special to JTables or charts or trees or whatever that would exclude them from the general rule. – kleopatra Oct 10 '11 at 08:00
  • How do you tell the LayoutManager which size you want? Also, what do you mean by "freaking out"? JEditorPane is not buggy as far as I'm aware. It seems like this "general rule" isn't actually that general. Also, you are yet to cite me a reference to anywhere (other than your own answer) saying that this rule even exists! – Adamski Oct 10 '11 at 08:20
2

JComponents doesn't returns getSize, getLocation, getBounds or getXxxSize if a JComponents hasn't been previously visible on the screen or after call pack()

but why care about that, because usage of (proper and correct) LayoutManager can do that automatically, that reason why LayoutManager exist there, really why care about that

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

Just call getPreferredSize method for JLabel.No matter if container of it is not realized, preferred size changes if you are setting text of jlabel even before you set it visible.

shift66
  • 11,760
  • 13
  • 50
  • 83