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.
Related
I want to make my jTable expandable while floating/overlay when dragging it using ComponentResizer class. I'm using ComponentResizer class from Rob Camick.
Currently as below after dragging the table:
I tried change my layout to null but the result still same.
In my code, I just added to call the ComponentResizer class:
ComponentResizer cr = new ComponentResizer();
cr.setSnapSize(new Dimension(10, 10));
cr.registerComponent(jScrollPane2);
I expect the dragging table will float and overlay the components below it.
Swing is designed to paint components in 2D space, not 3D space.
So when components are added to the same panel, Swing will paint components in the reverse order the component is added to the panel. In your case it looks like you add the components to the panel before adding the scrollpane to the panel.
So you could:
reverse the order in which you add the components to the panel
use the setComponentZOrder(...) method on the scrollpane to set its value to 0, so it is painted last.
However, this will still cause a problem because if you hover over the button the button will appear because its border is changed. This is because Swing assumes 2D layouts not 3D. If you want to make sure the table is always painted over the button you need to override the isOptimizedDrawingEnable() method of the panel. See Overlap Layout for more information on ZOrder painting.
I solved the problem by put all components in one layered pane and using Border Layout.
I am adding a JTextArea to my JPanel and setting it to a specific place but for some reason it does not move at all.
I have used both setBounds() and setLocation(), but to no avail. Here is what I stopped on:
JTextArea name_field=new JTextArea(1,10);
name_field.setBackground(color);
name_field.setBounds(100,100,600,420);
name_field.setLineWrap(true);
add(name_field);
It keeps creating the text field at the same spot: at the top of the screen in the middle. The only thing I managed to do is change it's width by adding name_field.setLineWrap(true) which only confused me even further. If this for some reason isn't supposed to work, is there another way of moving and possible resizing my JTextArea?
default layout of a jpanel is flowlayout .in order to work setBounds() it should be null layout.but it's highly discourage to use null layout[no layout].you should use layouts there are lot of layouts flow,grid,box,..etc.you first decide appropriate layout for your panel and then use it.
if you set layout to null then your code should work .[but dooont!]
setLayout(null); //change jpanel layout to null
JTextArea name_field=new JTextArea(1,10);
name_field.setBackground(color);
name_field.setBounds(100,100,600,420);
name_field.setLineWrap(true);
add(name_field);
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
In short, my need is to have a background Image in my java app, and upon some event, create some other graphics on top of that image.
I was thinking I would use a JPanel to draw the background image in, add it at to my JFrame the start of program, and then add other JPanels on top of that upon certain events. The problem is Swing gives the JPanels added first the highest Z index, so what should be my background is showing up on top of everything.
Is there any way to control the Z index/order of the JPanels, or am I going about this completely wrong?
You can use the setComponentZOrder() to handle Z-Order in your application.
Resources :
JavaDoc - Container.setComponentZOrder
oracle.com - Mixing heavy and light components
Sounds strange to add mutiple JPanels and use z-order. I would suggest you either simple add ONE JPanel with the paintComponent(Graphics g) method overwritten, where you draw the correct image according to your events.
Or use the CardLayout, and add JLabels (with different images), then when your event triggers, use
CardLayout cl = (CardLayout)getLayout();
cl.show(this, "card3");
to show the correct JLabel.
The JLayeredPane is designed for just this purpose. It allows you to control the Z-order of components.
I was thinking I would use a JPanel to
draw the background image in, add it
at to my JFrame the start of program,
Sounds reasonable.
and then add other JPanels on top of
that upon certain events. The problem
is Swing gives the JPanels added first
the highest Z index, so what should be
my background is showing up on top of
everything.
Adding a panel to a panel is just like adding another component to the panel. The child component will be painted on top of the parent panel. There is no need to play around with Z-Order.
The key is to set a layout manager on the panel with the image. Then each panel you add to the image panel must be made non-opaque so that the background image will be painted.
Maybe the Background Panel can help you out. It automatically makes any component added directly to the panel non-opaque.
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.