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

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.

Related

Can I make resizable the icon inside a jlabel?

Is there any property or any code that I can implement to make an icon inside a JLabel resizable. Or is there any other item which can store an image, that I could use to have a resizable image inside a JFrame?
use this code :
photo = new ImageIcon(UrlofPhoto);
Image im = photo.getImage();
Image Newim = im.getScaledInstance(yourlabel.getWidth(), yourlabel.getHeight(),Image.SCALE_SMOOTH);
ImageIcon Newphoto=new ImageIcon(Newim);
yourlabel.setIcon(Newphoto);
don't forget to set an initialised size to yourlabel
Generally, I would't suggest trying this with a JLabel as JLabel has a lot of other features you really don't want to messing with (alignment and text).
Generally, a better solution is to use a dedicated "image" panel, which you can provide additional control over to fine tune how you want the scaling to work.
For example
If you're really stuck on using JLabel, I would recommend attaching a ComponentListener to it and resizing the underlying image when ever it changes size. The problem with this is componentResized may be called repeatedly in quick succession, meaning you will need to devise some kind of coalescing algorithm that only reacts to the last event within a given period of time...
Check out Darryl's Stretch Icon which will dynamically scale the icon to fill the label.

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.

Maintain JComboBox Size

I want to make an account screen for a project, but I'm still new to GUI's. This is my first time working with a JComboBox and I'm having a bit of trouble. I want to basically place the JComboBox inside a box, which will be part of my background image. I tried using BorderLayout, but that just made a giant combobox that took up my entire screen. I have my code here and a drawing which illustrates my goal below:
See this answer for 2 layouts that can easily center the panel containing the combo box.
Use borders and layout padding within that panel for the white space required.

Swing Custom GUI Component

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.

Java - control Z order of JPanels

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.

Categories