how to automatically resize jPanel - java

I´ve created through NEtBeans design of jframe, when its components is created automatically.
Now I put on this jFrame component jLayeredPane called agendaLayer, cause I need more panes here and switch.
I´ve set horizontal and vertical resize to layout that component belong so it is automatically resize to some value when windows (jFrame) is resized..
Then I´ve created also through designer new class stock which extends jPanel,
now I put this jPanel to JLayredPane and need to get its properties about resizable..
stock st = new stock();
st.setBounds(0,0,agendaLayer.getWidth(),agendaLayer.getHeight());
agendaLayer.add(st);
But It did not work, jLayredPane is automatically resized when window is changed, but jPanel not it remains the same..

, jLayredPane is automaticaly resized when window is changed, but jPanel not it remains the same..
A JLayeredPane uses a null layout by default so components are never resized.
cause I need more panes here and switch.
If you need to switch panels then use a CardLayout. See the Swing tutorial on Using a Card Layout for more information and examples.

You should look into layout managers:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Related

change the size of panels inside a desktopPane when it's size changes

I´m using this code for starting the desktopPane maximized
this.setExtendedState(MAXIMIZED_BOTH);
When the jDesktopPane displays it's maximized, but the panels inside are the same size and I don't know how to resize them. I want the panels to resize too
For the JPanel inside your JDesktopPane, you need to implement layout manager. Then those panel resize based on the layout manager. For example, Here is tutorial how to use FlowLayoutManager
I implemented Layout Manager, but used Free Design Instead of Flow Layout, it was easier to me and I got the results I wanted

Java GUI. how to let Jpanel repaint without changing existing layout

I am working on a JAVA Gui development.
I have a Jpanel, and I can add a Jlabel to it and drag it to somewhere else inside the panel.
Then I can create a new Jlabel, and so on.
The problem I can't solve is that when a new JLabel is created and added to the panel, and the panel call repaint(). The existing labels and the new JLabel are lined up.
I want the existing labels to stay in their locations when a new label is added.
Any advice?
Thank you!
To position components at a random location you need to either:
use a null layout (not recommended)
use a custom layout manager
Check out the Drag Layout. It is a custom layout manager designed to provide layout management support while letting you manage the location of each component.

Java gridbaglayout using scrollpane with draggable jpanel

Im using a gridbaglayout on a jpanel with a scrollpane, all that works fine. Later in my code i add another jpanel using the constraints(x,y) to the same panel on top of everything else thats already there using the index, this also works fine. This added jpanel uses a listener to let me drag the panel around which also works but my problem is when i scroll or resize the jframe it puts the jpanel back where it was before i dragged it. Why?
This added jpanel uses a listener to let me drag the panel around which also works but my problem is when i scroll or resize the jframe it puts the jpanel back where it was before i dragged it. Why?
Because you are using a layout manager. When the frame is resized the layout manager is invoked and components are assigned a size/location based on the rules of the layout manager.
Check out the Drag Layout. It will allow you to drag components around a panel without resetting the location of the components.

Why does only the last JPanel I add to a JFrame resize?

So I am working on a GUI application using the swing framework. In short, I have 3 JPanels that act as different views of my application. Now the problem is that no matter the order I add the JPanels to my JFrame, only the final JPanel I add resizes when I switch to that view.
Some relevant bits of code:
When creating the window, I first create each individual JPanel, and add it to the JFrame:
JPanel newPanel = new SomeClassExtendingJPanel();
this.jframe.add(newPanel);
Next, whenever I switch between views of the application, I hide the panel that is currently active:
jframe.validate();
jframe.repaint();
oldPanel.setVisible(false);
And then activate the to be shown panel:
jframe.validate();
jframe.repaint();
newPanel.setVisible(true);
Does anyone know what could be wrong?
To resize the JFrame with each swap, you could call pack() on it, but this is kludgy having a GUI resize all the time. A better solution is to use the mechanism that Swing has for swapping views -- a CardLayout. This will size the container to the largest dimension necessary to adequately display all of the "card" components.
Check out the CardLayout tutorial and the CardLayout API for more on this.
JFrame's ContentPane has implemented BorderLayout by Default, and there is possible to put only one JComponents to the one Area,
you have to change used LayoutManager or put another JPanels to the other Areas

Is it possible to have 2 JPanels in a Border layout at the same location?

I'm writing a game which uses a border layout with a JPanel using BorderLayout.CENTER. What I'd like to be able to do is sometimes hide this panel and replace it with another panel with different information. I added both to the container and set visibility of one of them to false.
Then later I try:
panel1.setVisible(false);
panel2.setVisible(true);
but this doesn't display the new panel. I just see gray. Any ideas?
TIA
Use a nested JPanel with a CardLayout for that.

Categories