Resize of child components Swing - java

I want to know if I had a component,for example,button how to able it to resize if parent component is resized by user?

This is handled by the layout manager of the container. A button at the center of a BorderLayout will resize in all directions, for example.
Read the Swing tutorial on layout managers.

Related

Make children of JLayeredPane fill available space of container

I have two JPanel instances in a JLayeredPane, on different z-orders. I want both of my child JPanels to always fill the space of the LayeredPane.
The idea is for me to toggle the display of a 2nd panel over top of the first to display a modal-like dialog. Yes, I could just use a JDialog, but I thought it would be fun to try and create some transparancy overtop of the covered JPanel for a nice effect.
I find that using a layout manager on the JLayeredPane, like BorderLayout, and trying to set both children to CENTER conflicts since both panels can't be in the Center.
Is there a trick that I'm not seeing?
The idea is for me to toggle the display of a 2nd panel over top of the first
The easiest way to do this is to use a Glass Pane.
Check out the Disabled Glass Pane for an example of this approach.
There are two ways to create some "Glass Panel like" overlay for JPanels with JLayeredPane:
Add a ComponentListener to the JLayeredPane and update the sizes of all child components whenever the size of the JLayeredPane changes
Create a simple FillLayout, which expands the size of its child Components to the size of the Layout Container (In our case the JLayeredPane). You need to keep a list of children Components. During layoutContainer you copy the dimensions of the Container to these child Components. I wrote this and its really simple, but unfortunately I can't post it, since it's corporate. But if anyone is interested just ask in the comments. The implementation basically consists of one-liners.
For both solutions you need to make sure, that the panels on top are transparent, by setting setOpaque to false. This ensures that underlying panels render their content.

Setting Specific JPanel Child to Ignore Layout Manager

I'm making a simple GUI where there are multiple JPanels under a GridBag layout manager. The problem is I want to add a drag-and-drop feature by moving around child JPanels under those in the GridBag layout, but for whatever reason it seems setLocation() is not working. I suspect that the layout manager is interfering with this. I don't want to completely take out the layout manager because I need it to help organize the elements, so is there any way to set a number of specific elements to ignore the layout manager?
Just add a new jPanel with a transparent background (if it's not transparent by default use setOpaque(false)) to your GridBag layout, and set its layout to null:
myPanel.setLayout(null);
Also, set its size to an appropriate value, because null layouts by default shrink to nothing with no coordinates.
myPanel.setMinSize(int x, int y);
Any components added to your overlaying jPanel wilL not be affected by a layout and will be able to be moved around freely.

JavaFx-- positioning components in SceneBuilder

I used the drag and drop interface to put the components where I want them to be, but when the window is resized they lose their relative position. I have attached a screen shot of my hierarchy and of two windows to show how the components lose their position.
Hierarchy
Fullscreen
If you want to use the AnchorPane to layout your components, you can set the Anchor Pane Constraints, like in the image below:
This way it doesn't matter if you resize the screen, the button will always stay 10px far from the AnchorPane's right border.
When you use the AnchorPane to place components on screen you are not going to have a relative positioning, You should use others containers to layout your application. Read more about how to use Layout Panes here: Using Layout Panes
You need to study how to use Layout Panes. Dragging and dropping components results in absolute positioning, which really bad practice.

Frame image scaling

I'm using code:
main.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("4.jpg")).getScaledInstance(main.getContentPane().getWidth(), main.getContentPane().getHeight(), Image.SCALE_SMOOTH))));
to set the image.problem is,it's not on the full frame
and having 1 more problem when I'm designing GUI in the NetBeans IDE,the picture is overlapping the components.
plz suggest some solution.
You need to use layout manager. Without layout manager it will be difficult to resize components manually. Check these links Resize the components of the frame in full-screen mode & Visual Guide to Layouts. IMHO what you need is either Grid Layout or Border Layout.

JTabbedPane auto-resize

I am developing a Java Swing application with Net-Beans. the application has a JFrame with tabbed panes. Now the problem is that when the frame is maximized, the tabbed pane only covers half the page and does not re-size to fit the new size of the frame.
Is there a way in Net-Beans to make the tabbed panes re-size with the enlargements of the JFrame, or does it have to be done programmatically.
If it has to be done programmatically, how would I go about it?
The sounds like a layout manager issue
Try setting the layout of the frame to BorderLayout before you add the JTabbedPane

Categories

Resources