Find "real" height/width of Swing/AWT object
Because Canvas3D doesn't have the ability to resize dynamically with the parent frame, I would like to be able to track when a user resizes a window and then resize it manually myself. (If this ends up crashing Canvas3D, as some docs suggest, I will simply destroy and recreate it when the user resizes their window). Part of this procedure involves being able to accurately tell how big the container panel is to begin with.
The two methods I've tried:
panel.getHeight(); panel.getPreferredSize().height;
Don't seem to accurately report things: getHeight()
is invariably zero, and getPreferredSize()
returns numbers that don't actually have anything to do with the actual size of the panel.
Any ideas?
Edit: So, I took a debugger to the panel object and manually inspected the non-object properties and I didn't see anything that resembled width/height. Granted, there are sub-objects that I didn't look at. Also, maybe the window has to be visible (it isn't, at the point I'm interfacing the object) when I query for height/object?
Edit 2: So, Swing classes are subclasses of AWT classes, so I imagine if you're able to find the height/width of those, the approach would generalize. I've amended the title accordingly.
Asked by: Grace400 | Posted: 21-01-2022
Answer 1
To determine the size of a component you have to either:
- have set it manually at some point
- run the layout manager responsible for layouting the component
Generally, you get the exact size of a component via the getSize() method, which returns a Dimension object containing width and height, but getWidth/Height() should work too. But this can only work, if one of the two preconditions are met. If a window has never been made visible, has no layout manager or the component (you want to know the size of) has been added after the window/container has been made visible, the size usually is zero.
So to get the correct size, you have to make the container/frame visible (after you have added the component) or call validate() or doLayout() on the container to recalculate the layout, if you added the component after the last layout was done. Another thing to keep in mind is setting and probably configuring a layout manager on the container. If no layout manager ist set (null), even making a container visible oder calling validate() does not set a size on its children.
The minimumSize/preferredSize/maximumSize properties are hints to the layout manager, how the component should be sized, but it does not have to obey them (most layout managers don't).
Edit 2: After I read your other question about the same subject, I think you should read Using Layout Managers from The Java Tutorials
Edit: I don't know if you already figured that out, but to react to the resizing of the window, you can do something like this:
public class WindowResizeTest extends JFrame {
public static void main(String[] args) {
new WindowResizeTest();
}
public WindowResizeTest() {
this.setSize(640, 480);
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
this.add(panel);
this.addComponentListener(new ComponentListener() {
public void componentResized(ComponentEvent e) {
System.out.println(e.getComponent().getSize());
}
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
});
this.setVisible(true);
}
}
Answered by: Samantha945 | Posted: 22-02-2022
Answer 2
I found out that if you extend by JFrame, this code can be used also to save time, effort and space.
int windowWidth = getWidth();
int windowHeight = getHeight();
I know you already got an answer but if you ever need an alternative, here it is.
Answered by: Abigail718 | Posted: 22-02-2022Similar questions
java - POI 3.2 Image Height/Width controlling
Using POI version 3.2
Issue: Not able to resize an image to its original height and width.
I am able to add an image to the excel file.
After adding image I call picture.resize(); method.
Later I resize the columns of the excel file by calling
sheet.setColumnWidth(columnindex, columnwidth)
the image losses its original height/width.
Please help.
java - Windows taskbar height/width
I can't figure out how to get the windows taskbar height dynamicaly to set my application fullscreen.
As you know, taskbar can be in four positions: bottom, top, left or right, so I'm wondering if it's possible to also know the current position to set the window bounds.
EDIT:
Using Lukas link I tryied this:
GraphicsDevice myDevice;
Window myWindow;
try {
myDevice.setFullScreenWindow(myW...
java - Image Disappears When I Change Height/Width
I have the following Java code:
public void actionPerformed(ActionEvent e)
{
ImageIcon i1 = new ImageIcon("/Users/vhaislsalisc/Documents/workspace/Role_Switcher/greenCheck.png", "Success!");
JLabel checkMark = new JLabel(i1);
if (requesterRole.isSelected())
{
try
{
PreparedStatement ps1, ps2;
...
java - Android: Get height/width from byte array or file
I have an image that is stored as a byte array (can also be accessed from Android directory as a .jpg) and I'm trying to get the width and height of the image. I've tried to use javax.imageio however Dalvik doesn't support this lib, so I've opted to use Bitmap. Can anyone help me out here? I keep getting Null Pointer Exceptions.
public int getWidth(byte[] image) throws IOException{
Bitmap bitmapImage = ...
java - how can i edit the form's height/width (I use Netbeans)
I try to create a simple program where, from click a button i could size by height?
Ex. :i create a text field, a button. If In a text field i write 255, form's height of the form expands by 255.
Thanks
java - How to specify the Height/Width in android crop intent without change it by user?
I change outputX/outputY to 100 x and y and then to 1000/1000 x and y but nothing different only resolution changed
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.setType("image/png");
doesImageChosen = true;
getImage.putExtra("crop", true);
getImage.putExtra("aspectX", 1000);
getImage.putExtra("aspectY", 1000);
getImage.putExtra("scaleUpIfNeeded", t...
How to extract SVG height/width in Java without libraries?
There are multiple ways to extract dimensions of a SVG in javascript or php like getBBox() I am looking similar ways to extract SVG height and width in Java. I am trying to avoid using libraries like batik.
java - If I Reduce the Bitmap quality first and then Rescaling its height/width size, is there a difference if I do it the other way around?
Im following the Android Documentary DocumentaryLink on how to resize Bitmap height/width(Using BitmapFactory.decodeStream). And I already know how to compress bitmap quality and byte size in a basic way (Using ByteArrayOutputStream())
But I just wanna know if there is a difference i...
java - Is it possible to bind the height/width of a pane to the center node of a BorderPane?
I have an assignment that requires me to extend the pane class, then add a rectangle on it. I then fill the rectangle and use some buttons to change the colors of the rectangle. I added my extended Pane class to the center node of the border pane, but I envisioned the rectangle taking up the whole pane and binging to the Pane's height and width, then in turn the Pane taking up the whole center Pane of the BorderPane and ...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)