I am new to Java GUI programming. I am working on an application where I need to display images, on a UI along with some other button/controls. I have a working piece of code which uses JFrame.
As a general bit of un-asked for advice, I will recommend that you try to avoid having your classes extend top-level window classes such as JFrame as this will limit greatly what you can do with the class. Better if you need to extend a Swing component that it be a JPanel, as this can be placed in a JFrame, a JDialog, a JOptionPane, another JPanel,... pretty much anywhere in your GUI. Note that most of the time you won't even need to have your class extend a component.
My requirement is where I want to add some more widgets such as button to start/stop displaying image etc. When I am using Jframe to display image, it occupies the entire JFrame, and I can't add other controls.
This is a bit confusing as a JFrame does not by itself have a mechanism to display an image.
I am looking for something that I can display image and add it as a component in to JFrame. Can someone explain how this can be done?
Your requirements are a bit vague, but consider:
- Make ImageIcons for each image that you wish to display, and save your icons.
- Create a single JLabel for displaying images.
- Call
setIcon(someIcon) on your JLabel to swap images.
- If your image needs to change size to fit the component that's displaying it, then have your display component extend JPanel, and display your image in the JPanel's
paintComponent(Graphics g) method, as per Kevin Workman's answer (1+ to it!).
If this information doesn't help you, then please consider fleshing out your question a bit more by providing us with more relevant background information and relevant code.