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

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.

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

How can I create responsive JList in Java Swing

I want to create a shop by having a main JPanel that each component inside it is a JPanel with an image, label and button.
I did tried using a JList but the problem with the JList its only holds the rendering of the component and because of that the button isn't working and its only an image. I can walk around and use MouseEvent but it feels wrong for me and I am sure that there is a better solution for it.
I want that the components will change their positions depend on the frame size, like in the JList.
For example, if I change from the width of the screen the positions of the components will change from this:
to this:
I do have an idea by using GridLayout or GridBagLayout in the paintComponent (because it calls every rendering. If you know another method that calls every rendering int the JPanel I would like to know) and changing the positions of the components by changing the layout variables inside the paintComponent.
I did surfed the internet to find a solution but I only found that people used JTable but I don't see it working here.
each component inside it is a JPanel with an image, label and button.
Makes sense.
changing the positions of the components by changing the layout variables inside the paintComponent.
The paintComponent() method has nothing to do with changing the layout of the panels. You should not be playing with the paintComponent() method.
I do have an idea by using GridLayout or GridBagLayout
You are correct to use a layout manager, but unfortunately, none of the default layout managers will wrap automatically at a random number of components.
The layout managers are invoked automatically as the frame is resized.
So you can use the Wrap Layout which is an extension to the FlowLayout that will allow random wrapping.

how to automatically resize jPanel

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

Java Swing - set panel

I'm quite new to Swing, and I'm programming an application with NetBeans' UI designer.
Now I have an JPanel called "editorPanel", and it must be able to display multiple things. (so, sometimes it has to display an image, and sometimes it has to display a text editor)
I have made separate panels for this, so say I'd have a JPanel called ImagePanel and one called TextPanel. It has to switch easily between them, so I tried this:
editorPanel = new ImagePanel();
But that didn't work.
So, what I want to do, is set an empty panel to a defined panel.
How can I make this work?
The proper way to achieve your goal is to using a card layout and switching panels accordingly.
You ca get some idea on how card layout stuff is working in here

Swing changing JTabbedPane to a JPanel

I'm having this strange issue with Swing. I have a main JPanel to which I am adding a JTabbedPane. Inside of this JTabbedPane I am adding another panel:
myTabbedPane.add(innerPanel, "Title", 0);
outerPanel.add(myTabbedPane);
Now, I no longer want myTabbedPane to be JTabbedPane, I want it to be a JPanel. When I change its type (and remove the extra params from its add() method), nothing within the outerPanel is visible anymore. (I am using setBounds() and I set the layouts to null).
Why does it work when using a tabbed pane but suddenly stop when switching to a JPanel? I know that this can be done differently (such as adding the innerPanel directly to the outerPanel), but please don't just tell me to do it differently. I'd just like to know why it suddenly doesn't work when using a JPanel instead. Is there an issue with adding a JPanel to a JPanel? Thanks!
Stop using null layout. Use BorderLayout and then use add inner panel to the center.
Tabbed pane used it own layered layout - that is why it worked before.

Categories