Swing Custom GUI Component - java

I need to create a custom GUI Component about same like shown in the following image.
it has some buttons and labels on this.
How can i create like this

You could use a JWindow, with an Image for the background picture. For the buttons, use a JButton with an ImageIcon.

You can have a JFrame with nullLayout then use a JLabel with the image.
Now for buttons use setContentAreaFilled(false) and setBorderPainted(false) to remove the default button style and it will look exactly like the image that you have passed while creating the button.
To position the Frame at the center use setLocationRelativeTo(null) .
I think that should solve your problem.

Related

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.

Using an image instead of JDialog/frame to hold swing components?

I currently have a JDialog (class that implements JDialog and is constructed like a jframe), and has 3 swing buttons placed on it. Currently I have it set, undecorated = true, to hide the outer frame. Is there any way to use my image to replace the default square frame?
This is what I aim for :
The blue square with shadow is the pre made image.
Regards
The blue square with shadow is the pre made image.
Well, the best way would be to set the background of the panel and then add a ShadowBorder to the panel. This will provide you with far more flexibility in the future as you can create many panels with different colors and reuse the same ShadowBorder instead of having to create an Image every time. I don't have an example of a ShadowBorder, but you might find one if you search the web.
Is there any way to use my image to replace the default square frame?
But if you really want to use your premade Image, then you can just:
create a JLabel and add your Image to the label as an Icon
add the label to the dialog
set the layout manager of the label
add your components to the label.

How to add image to JFrame as background and still add other components to it?

i want to add an Image as a background to a JFrame but not like
this.setContentPane(new JLabel(new IamgeIcon("path of image file"));
When i use the above code line i can not see my other components added to the same JFrame.
I want to set an image as a background as we see in the html pages where body has a background image and we see other components like buttons and textboxes over it. Can anyone please show me how to do that? And I want to attach 2 more panels to same JFrame.Please attach the image of your code output.
You can use JLayeredPane
JLayeredPane adds depth to a JFC/Swing container, allowing components to overlap each other when needed.
link how to use Layered Panes
and check this example

Draw round Button in LookAndFeel-Style

I'm experimenting with JRadioButton's to put them on a JToolbar and select the last one clicked. If i'd use JButtons they wouldn't keep the Selection.
Since JRadioButton always have that Dot, I need to draw them myself by overriding the paint-methods.
The Button's will be circles with an Icon in it. That works if I draw Images, but looks aweful. The problem I have is that I would like to draw the circle so that these Buttons always look like the JButtons with the current LookAndFeel.
How can i do that? I searched for a while now, but I didn't find methods to read some default-colors of the LookAndFeel which I could use.
So how can i read Background-Colors etc. of the current LookAndFeel to use it for some custom Button-Drawing?
So how can i read Background-Colors etc. of the current LookAndFeel to use it for some custom Button-Drawing?
See UIManager Defaults.
I need to draw them myself by overriding the paint-methods
Don't do custom painting in the component. If you don't like the default Icons, then create your own Icon and do the custom painting there or create an Image and use an ImageIcon. The you can use the setXXXIcon() methods.

How do I set Z-order for image in Swing Java

I have 3 image so I want, if I drag any image that should appear above all the other images. Which technique can use? please any idea???? By the way I am using "ImageIcon".
Thanks in advance.
If what you want to do is click on an image, lift it above the others and then drag it, consider placing them in ImageIcons and these in JLabels, and displaying them in a JLayeredPane, or a JPanel that is held in a JLayeredPane. You can then lift the clicked label onto the JLayeredPane.DRAG_LAYER while dragging, and then drop it down into the DEFAULT_LAYER (or on a JPanel that's on the DEFAULT_LAYER) when done. For an example of this, please see my code in a related question: dragging a jlabel around the screen
If I'm totally off base, sorry, but please correct my incorrect assumptions.
By the way I am using "ImageIcon".
I assume this means you are adding in image to a JLabel and are then adding the JLabel to a JPanel.
so I want, if I drag any image that should appear above all the other images
You need to add a MouseListener to the JLabel. When you click on the label then you can reset its Z-Order to 0. This will cause the label to be painted last and so it will be on top of all other labels on the panel.
So the basic code in the MouseListener would be:
panel.setComponentZOrder(label, 0);

Categories