0

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.

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.

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?

0

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

4

One easy way is to display the image in a JLabel.

ImageViewer

Many (OK 3) images

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;

public class ImageViewer {
    
    JPanel gui;
    /** Displays the image. */
    JLabel imageCanvas;

    /** Set the image as icon of the image canvas (display it). */
    public void setImage(Image image) {
        imageCanvas.setIcon(new ImageIcon(image));
    }
    
    public void initComponents() {
        if (gui==null) { 
            gui = new JPanel(new BorderLayout());
            gui.setBorder(new EmptyBorder(5,5,5,5));
            imageCanvas = new JLabel();
            
            JPanel imageCenter = new JPanel(new GridBagLayout());
            imageCenter.add(imageCanvas);
            JScrollPane imageScroll = new JScrollPane(imageCenter);
            imageScroll.setPreferredSize(new Dimension(300,100));
            gui.add(imageScroll, BorderLayout.CENTER);
        }
    }
    
    public Container getGui() {
        initComponents();
        return gui;
    }
    
    public static Image getRandomImage(Random random) {
        int w = 100 + random.nextInt(400);
        int h = 50 + random.nextInt(200);
        BufferedImage bi = new BufferedImage(
                w,h,BufferedImage.TYPE_INT_RGB);
        
        return bi;
    }

    public static void main(String[] args) throws Exception {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("Image Viewer");
                // TODO Fix kludge to kill the Timer
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
                final ImageViewer viewer = new ImageViewer();
                f.setContentPane(viewer.getGui());
                
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);

                ActionListener animate = new ActionListener() {

                    Random random = new Random();
                    
                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        viewer.setImage(getRandomImage(random));
                    }
                };
                Timer timer = new Timer(1500,animate);
                timer.start();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Comments

3

There are several components that support displaying icon. One of them is JLabel: http://docs.oracle.com/javase/tutorial/uiswing/components/label.html

Another option is to extend JPanel and override the paintComponent() method to display whatever you want, including images: http://docs.oracle.com/javase/tutorial/uiswing/painting/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.