Dynamically changing JPanel size - java

How to dynamically change the size of a JPanel depending on its contents which is a dynamically changing Popup with various size possibilities.

Call revalidate on the JPanel after the size has changed. This will force the layout manager to run again.

In the simplest scenario you'd set a new preferred size, invalidate, validate again and finally request a repaint (or simply call SwingUtilities.updateComponentTreeUI on the panel) but the final say about the panel's size (and position, too) rests with the layout manager of the parent container; the look and feel may play a role, too. An alternative way to set the size is to [re]set the sizeVariant property of the component (a kind of size hint) and then as above, with the same caveats. Thus the answer to your question is: it depends.
If you are a tester and you did not write that code I realize that doesn't help you much. In that case you need to talk to its developer.

Related

Null Layout in NetBeans-JAVA

I tried to add image to my login frame in Netbeans, so I try to change the layout from 'free
Design' to 'Null Layout' (like I see in this video: http://www.youtube.com/watch?v=uZFgiqM0udA), and it's work.
But when I run the program the window open in the left side, and not in the regular size -
as you can see in the picture:
What is the problem?
Welcome to the wonderful world of why you should not use null layouts (and why learning to code a UI with a form designer is also a bad idea).
The problem you are facing is based on the fact that Swing (and AWT) were designed to work with layout managers, this is at the core of how the framework works.
When you call pack on a Window, it asks all it's children what size they would like to be and calculates the best size for the Window.
In your case, because you're not using a layout manager, the window is assuming it's default side of 0x0 (plus the frame border).
To fix the issue I suggest two things. Firstly, stop using the form designer until you understand how the UI is constructed and secondly, make use of one or more layout managers.
Take a look at A Visual Guide to Layout Managers and Using layout managers
I've not had the chance to use it, but it might also be worth while to take a look at MigLayout, it comes highly recommended by many of the users on SO
As far as I know, dont use null layout, use LayoutManagers instead, anyway, you're free, I think you're calling pack() method to the frame and you probably didnt set the aize of frame
or you can move on and start by tamming a little the layout manager.
select your frame in the navigator and then, look for this propierties
preferredSize - The size that frame will take as their default
maximumSize - The maximum size of the frame
minimumSize - Minimum size of the frame
Using Absolute Layout fixed mine! For some reason, default size 0*0 was getting executed on run ( but the frame remains in custom resolution), so changing null layout to absolute is calling the custom frame size.

setLocation() does not always work with JLabel objects inside a panel with no layout manager, is this a bug?

So I've search these forums for an answer to this question and can't seem to find one.
First let me state that I completely understand why going with a null layout manager is a bad idea and in this case I basically have no choice. I'm working with a legacy code base and it would be very time consuming to try and write a custom layout manager to perform the correct layout. Some relevant info:
The size of this component never changes
This size of the child components never change once they have been added to the parent
Only the number and location of child components change when a user action dictates they should
This code is not localized
I can't post code examples as the code is production software and would be too lengthy for this forum
The code flow is as follows:
When the parent component is created the layout is set to null in the constructor.
When a user action dictates that they want to display information the child components are created and added to the parent.
Then the correct location for each component is determined and the location is changed via setLocation().
One of the types of child components are 1 or more JLabel objects. This is the object I'm having problems with. In certain cases the call to the JLabel's setLocation() function will have values of x= 52 and y = 0 yet the JLabel gets put at the location x = 0, y = 0. I've already confirmed that the location isn't being reset to 0,0 by some other area of the code so now I'm left with the thought that maybe there is some obscure bug in the Java implementation that is causing the problem. Otherwise, why would it not respect the call to setLocation()?
I am going to confirm the setBounds() isn't being called on the JLabel by any other area of the code. Are there any other calls that could affect the position of a component when a layout manager is NOT being used?
With absolute positioning you have to consider three steps, you've already done the first step by setting the layout to null.
The next step is whenever you add a component to the container, you've to use setBounds(x,y,width,height) method on that component.
You only used setLocation() method without setting the size of the button, you have to use setSize(width,height) method also if you want the button to be displayable.
After all, don't forget to call the Component class's repaint() method.
I have done some experiments with setLocation() in java
https://dl.dropboxusercontent.com/u/27670533/ani_test.rar
this are the src file take a good look at the them maybe it helps

JLabels dont show

So I have a panel and depending upon users entry they are populated with an x number of jlabels. Now the problem is, when the user entered information the labels successfully populate but they do not display properly in the panel; they don't even show.
Only when I resize the frame they appear?
It's been a while since I did Swing programming and I am trying to remember the method which you are supposed on a container after you add components. I think it's revalidate().
usually you have to call:
JPanel yourPanel = new JPanel();
yourPanel.repaint();
yourPanel.validate();
invalidate marks a component as needing to be relaid out soon because the component or one of its children has been resized or become visible or invisible. invalidate is called on a component automatically when children components are added/removed.
validate checks that a container is valid and if not, calls doLayout or invalidateTree to calculate positions and sizes of children components. validate effectively redoes the layouts if necessary, deciding on new sizes and locations of all components in the container.
After adding/removing components from a container, validate must be called on the parent to let the LayoutManager redo the layout. Calling validate does not schedule a repaint, so you may need to call repaint after the validate.

JDialog and JPanel do not open to their set sizes and do not show their labels?

In my Netbeans code I have JPanels and JDialog which are driving me crazy at times. Some of the controllers on these containers decide not to show up or automatically change size even though I have set up both their size and contents within the code and through using the IDE properties. For instance some of my jButtons on a certain JPanel does not show its text label or the sizes of some of my text field change.
Any solution to this would be grately appreciated!
When you create GUI using the NetBeans IDE wizards the Layout manager attached with JPanel and JFrame is GroupLayout and it works as expected. It keep the size of your JPanel and JFrame as you have specified.
Now if you change the LayoutManager of the JPanel or JFrame then you are on your own. You must know the consequences of changing the LayoutManager and update / add the required code to make the code to run as expected.
I will suggest you to keep the default LayoutManager as GroupLayout if you want to get what you see in the NetBeans component designer.
Unfortunately you did not provide any code snippet that can show your problem. But let me assume that you are confused with layout behavior. Typically we use Layout manager and delegate to it the responsibility of placing and re-sizing the graphical elements. Layout manager does it work when the parent element is being painted, i.e. during execution of method paint() that happens asynchronously and may be caused by various events (e.g. changing focus, re-sizing of window etc).
In this case all your attempts to change size of specific element by calling its setSize() could be overridden by layout manager that decides to change size of the same element differently.
So, if my assumption is correct learn to use layout managers and ask more specific questions if you have any difficulties with them.

stretched button in the gridLayout

everyone, I have some problem with Java I created panel with five rows and one column, also I added button to the one of the rows, finally I added this panel to the frame, and I received button which is stretched to full frame, can I reduce the size of the button, I used
myButton.setSize(10,10);
but it doesn't seem to work, also I use
frame.pack();
may it be the problem, thanks in advance
You should refrain from setting the size yourself. You should select a proper LayoutManager that does the job for you.
(Unless you explicitly do setLayout(null) the sizes you set will be discarded and the current layout manager will assign new sizes to the components.)
You say that you're using a GridLayout. This layout is fairly "unflexible", and if you want the benefits of GridLayout but more flexibility, you could give GridBagLayout a try.
If you don't find a layout manager that suites your needs, don't be afraid of writing your own. (It's five relatively easy methods to implement.)
If you post a screen-shot of your current application, and an explanation on how you want it to look, we'll be able to help you further.
Some useful links
Trail: Using Layout Manager
Create a Custom Swing/AWT Layout Manager in Java
When aloobe seems to have a more general solution for you. I'd say the immediate solution would be to add the button to a JPanel, set the Layoutmanager a layout like GridLayout, or another LayoutManager, if you find another suits you better.
And add this panel to your original panel, in place of the button.
When you are using layout managers (like GridLayout) then calls to setSize() generally don't work, because the layout manager ultimately decides how to size and place each component in a container.
What you can do is call setPreferredSize(), setMaximumSize() and/or setMinimumSize(). Depending on the layout manager in use one or more of these requests (because that's really what they are) will be honoured. When you call frame.pack() it will try to size everything in the container (and subcontainers) to their preferred sizes.
So, in your case, calling myButton.setPreferredSize (10, 10); will probably work. However, it will still be resized if the container changes size. If you don't want that, there are other solutions I can post.

Categories

Resources