1

I'm new to Java GUI, and am having issues displaying an image. My intention is to display a large image and allow the user to click on regions of the image to indicate where certain features are located. Anyway, I'm getting a rough start because I can't even get the image to appear, despite reading Oracle's explanation and other solutions.

I've created a JFrame and used its setContentPane() method to add a JPanel and JLabel. I use the setIcon() method of the JLabel to add an image to it, or at least that's my intention...

Any advice is appreciated, especially if there's a better way of doing this. I'll be using OpenCV to process images, and plan to convert them to Java image (or BufferedImage) before displaying them.

Here is the code. I left out the libraries to save space.

public class Pathology {
    public static void main(String[] args) {
        PrimaryFrame primaryFrame = new PrimaryFrame(); 
        primaryFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
        primaryFrame.setSize(1500, 900);
        primaryFrame.setVisible( true );        
        primaryFrame.setContentPane(primaryFrame.getGui()); 

        try {
            primaryFrame.setImage(ImageIO.read(new File("C:\\Users\\Benjamin\\Pictures\\Pathology\\C\\001.png")));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

GUI Class:

public class PrimaryFrame extends JFrame{
    //private JTextField textField1;
    JPanel gui; 
    JLabel imageCanvas; 

    public PrimaryFrame() {
        super( "Pathology-1" );
        //setLayout(new FlowLayout());

        //textField1 = new JTextField("Chup!", 50); 
        //add(textField1);

    }

    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;
    }
}

2 Answers 2

4

Would you laugh at me if I'd tell you that you just have to put the primaryFrame.setVisible( true ); to the end of the main method? :)

For furture understanding, you don't have to call frame.setVisible(true) every time you want to add/update something in the frame (in an ActionListener, for example). Instead you can call frame.revalidate() and frame.repaint(). (Where frame can be replaced with the particular panel)

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

1 Comment

Thanks, LuxxMiner. I'm still working to grasp these basic fundamentals.
2

You need to setVisible(true) after the call to setImage():

primaryFrame.setImage(ImageIO.read(new
    File("C:\\Users\\Benjamin\\Pictures\\Pathology\\C\\001.png")));

because any update to the GUI after setVisible() will not be shown.

That's it and the code should be like this:

public class Pathology {
    public static void main(String[] args) {
        PrimaryFrame primaryFrame = new PrimaryFrame(); 
        primaryFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
        primaryFrame.setSize(1500, 900);
        primaryFrame.setContentPane(primaryFrame.getGui()); 

        try {
            primaryFrame.setImage(ImageIO.read(new File(
                "C:\\Users\\Benjamin\\Pictures\\Pathology\\C\\001.png")));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        primaryFrame.setVisible( true );
    }
}

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.