I have a subclass of JPanel, and I want it to, as soon as its parent frame is pack()ed, to set its minimum size to its new preferred size. I've tried using a ComponentAdapter, but JPanels are by default visible; if I setVisible(false) at the beginning of the constructor, the JFrame won't make it visible again. If I use SwingUtilities's method to get the window root, it will return null because it's in a constructor.
Is there a way to do this?
I have a subclass of JPanel, and I want it to, as soon as its parent frame is pack()ed, to set its minimum size to its new preferred size.
If you know the preferred/minimum size before the pack, set it then.
to set its minimum size to its new preferred size
Certainly possible, but whether it is useful ... . You can override the getMininumSize() method to return super.getPreferredSize();
Related
I have a Panel which needs to know its size inside the constructor:
class Panneau extends JPanel {
public Panneau() {
super();
new Map(getSize());
}
}
Unfortunately, the size doesn’t seem to have been initialized yet and getSize() returns a Dimension with 0 for height and width. How to get the future Dimension notwithstanding?
If you want to work with the size of a component, you need to do so when the component is actually in use in a well-formed user interface.
One of the ways to do that, is to add a ComponentListener to the code and implement the componentResized method, which will be called whenever the component's size changes. There you can work with the latest accurate value for the component's size by using getSize().
I want to get the present size of my Jframe whenever i click on the button1. This is the code which i have written in my button1clickevent initially when i click on the button i do get a width and height but when i change the size of my Jframe and then again i click on button1 i get the same width and height as i got earlier.The values are not changing anytime.
AppDisplay ap=new AppDisplay(); //class which has got the JFrame
int f12;
f12=ap.getContentPane().getSize().width;
System.out.println(f12+"\n");
f12=ap.getContentPane().getSize().height;
System.out.println(f12);
I think your referencing the wrong Object. What's AppDisplay & why do you need a new instance?
Also, what's wrong with Frame.getSize(); ?
You are printing the size of the Content Pane which is inside the JFrame. Use the JFrame's getSize() method to get the size of the frame.
final Dimension size = ap.getSize();
System.out.println(size);
Also, MadProgrammer makes a good point that the code you show creates a new instance of AppDisplay. You can create as many instances as you want, even without showing them. You will always want to carefully consider the lifecycle of the objects and the scope of the variable names that reference them so that you can refer to the intended object every time.
one idea i found that.
Place a jButton ("jbutton1") one the jFrame.
and write the code inaction performed of the buton.
System.out.println(this.getSize());
then run the frame.
adjust the frame to required size.
then click the button..
we will get the updated size..
then we can set it as the actual size of the frame from properties of jframe...
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...
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.
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
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.
I am using a JPanel (with several labels inside) to add a dynamic information on a graph. This panel is dynamically created, it is not visible before I use it to draw.
For this, I am using a BufferedImage, and I follow approximately the same steps as described on this other question. It works good, as long as I specify all sizes (the panel, and its components).
Like asked as well in comments of the referred question, how can I determine the optimal size of this panel? The same operation would be done if this panel was displayed in a regular frame/layout setting.
In my case, how can I "pack", in a way, this panel, so that its size, and size of its content are set to the optimal (determined by the size of labels, then)?
Suraj and willcodejavaforfood put me on the good track.
Checking what is actually done in a pack() method, I see that this is mostly setting the current size to the one returned by getPreferredSize().
From this, I managed to make such solution:
// Creating the panel
JPanel lPanel = new JPanel();
//lPanel.setSize(1000, 1000); //default size, not needed anymore
lPanel.setLayout(new BoxLayout(lPanel, BoxLayout.PAGE_AXIS));
//Adding the content
lPanel.add(new JLabel("Blah"));
// etc...
//Adjust the panel to its preferred size
lPanel.setSize(lPanel.getPreferredSize());
//Call the layout method
//(this will adjust the content components to their correct size and position)
lPanel.doLayout();
This method works correctly, and adjusts the panel and its content to the correct size (and answers my question in a simplistic way: "how to find the preferred size? getPreferredSize()").
However, it requires to set the initial size to a large enough size, so that the content fits in, or they won't be put on the layout. This is a bit pity, and not really "clean", but I can't find a way to avoid that, for now.
Edit: Actually, the default size was not necessary, because getPreferredSize() returns the correct value, even before calling doLayout(). As such, the panel can be set to its proper size before calling the layout method.
The direct answer is to call Window#pack(). This method will automatically set the size of all underlying children to thier preferred sizes(ofcourse this depends on layouts of child containers, for e.g. BorderLayout doesent give a damn about preffered sizes).
So as long as you have set preferred sizes(or min/max sizes in case layouts are like BorderLayout) of your child components, pack() method will be all you need.
[UPDATE]One way is to do is add a HierarchyListener to your jpanel and check for HierarchyEvent#DISPLAYABILITY_CHANGED events. This event is called when your panel is realized that is ready to be shown(and a parent is available), at this moment you can do:
SwingUtilities#getWindowAncestor(myPanel).pack();
I'm setting a JFrame size with myFrame.setSize(Xsize,Ysize), but it gives me a problem : this doesn't define the usable space in the frame but the whole frame's size (it include the Windows Manager frame size, which isn't always the same).
Is there any way to define the JFrame size by defining the usable space size ?
Usually the best method is to setPreferredSize() on the contents of the frame and then call pack().
If that is not possible then I think calling getInsets() on the Window will grab the size of the window manager frame as long as the frame is completely visible (you may need to use a listener and wait until a windowOpened() event). I haven't actually tried this on X but it definitely works on Windows. The downside of that approach is that the frame appears and then resizes. You could start it off screen and then move it to a visible location once you have the insets.