How to get real JPanel Size? - java

How should I get real JPanel size in JFrame?

The size will be 0,0 until it is displayed because the components and layout are not calculated beforehand.

thePanel.getSize();
This returns the Dimension.
I sometimes add a ComponentListener to show the dimension when the panel is resized.
EDIT: If you want the size of the content pane of the JFrame then
theFrame.getContentPane().getSize();

Related

JFrame & JPanel sizing issue

I am attempting to code a JFrame containing a JPanel. Within the JPanel is an array of JTextField's. So, my GUI looks like:-
I am not using a layout manager, and have set this to null for the JFrame and the JPanel. I am sizing these components by hand.
You can see that the right hand portion of the JPanel is chopped off, even though I have used the same sizing as the containing JFrame.
The code appears as below:-
I have calculated the required width of the JPanel by multiplying the number of columns in the JTextField array by the width of the JTextField. Aside from that would need to be added the width of each gap between the JTextFields (there would be (columnNumber - 1) of them), as well as the two border gaps.
I have done this, yet the right hand side border gap is chopped off, as you can see from the diagram.
If I add some random amount to the panelWidth, then you can see the right hand gap there, but my question is what am I missing here? This ought to work surely, if the JFrame side and the JPanel size are identical, which they are as I have also printed them both out, and the print outs give the same number.
Jeremy
I want for any combination of row/column values to allow a constant vertical & horizontal distance between each JTextField, and for each of those text fields to maintain default sizing.
The GridLayout allows you to specify a horizontal/vertical gap between each component and allows you to control the size of the grid.
Then you can wrap the panel using a GridLayout in a panel that respects the size of the grid.
For example you could do:
JPanel grid = new JPanel( new GridLayout(...) );
JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add(grid, new GridBagConstraints());
frame.add(wrapper, BorderLayout.CENTER);
If you pack the frame, the grid panel will be displayed at is preferred size.
If you resize the frame the grid panel will remain centered in the wrapper panel.
The top part of that GUI is well suited to a grid layout, the bottom part with 'Go / Cancel' buttons - a flow layout. Put the grid layout in the CENTER of a border layout, the flow layout in the PAGE_END, pack the top level container (for non-cropped, 'right size') & the job is done.
It might end up looking something like this:

How to scale JPanel to fit JFrame

Currently, I have a full screen JFrame. Within that JFrame, is a panel called MainPanel which is 1280 by 640, a portion of the full screen game. The MainPanel is where my game is rendered to. Is there any way to scale this MainPanel to fit JFrame without having to adjust all the component sizes of sprites and such? I'm thinking of rendering MainPanel to an image and drawing it over JFrame, but I do not know how to go about this.
Thanks.
By default, the layout manager of a JFrame is BorderLayout, and adding a JPanel to it without any arguments will place it in CENTER, which will automatically scale to the size of the JFrame. It will ignore any size you have given the JPanel.

How to make JPanel transparent in Java

I have multiply smaller JPanels which are then added to a bigger JPanel which is the same size as JFrame. Now I want to make those smaller JPanels transparent and the image I have added to the bigger JPanel showing in the background. I tried using setOpaque(true) on the smaller JPanels but it does not show the image that I have added to the bigger JPanel. I hope you understood my problem.

How to make JPanel resize to fits its components?

I have a JScrollPane whose viewport is a JPanel. The JPanel contains smaller JPanels that take up the entire viewport's width, and the big JPanel is set to a FlowLayout. The user should be able to add as many JPanels as they want (well, up to 200 for my purposes), and they should be able to scroll down the JScrollPane to see everything they have added. Basically I'm just trying to make the JScrollPane grow. I'm calculating what I thought the JPanel's height should be like this:
Dimension dimension = new Dimension(width, smallPanel.height * totalPanels
+ ((FlowLayout) getLayout()).getVGap * totalPanels);
setPreferredSize(dimension);
And it mostly works, but as you add more, it starts cutting the smaller JPanels off, and the bottom panel eventually isn't shown. Is there a way that I can determine the size of the JPanel for the viewport so that I wouldn't have to calculate its dimensions with variables? Like pack() does for JFrame? Or do I need to keep guessing and checking?
Thanks

JFrame not properly sized

If I set the size of JPanel first, then set the panel to be the content pane of an unsized frame, the frame will be squeezed into a very small rectangle in the top-left corner of the screen.
But if I set the size of frame first and then set the panel to be its content pane the frame will be properly drawn.
Why does this happen and how do I solve this if I really want to specify the size of JPanel rather than JFrame?
You should tell the frame to pack itself when it is first shown:
frame.pack();
frame.setVisible(true);
This will cause the frame to adopt a size that suits its contents.

Categories