Tanvi
Tanvi

Reputation: 95

Why does the JFrame resizes itself when image is added to it

the size of the frame is set to 500x500, but when i add an image to it the frame resizes to the size of the image. I tried using this

jf.setPreferredSize(new Dimension(500, 500));

but then the image would not appear.

import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.event.*;
public class Test implements ActionListener {
ImageIcon imgIcon;
  JFrame jf ;
  JPanel jp_image,jp_option;
  BufferedImage img;
  Test(){
        jf = new JFrame("photo editor");
        jf.setPreferredSize(new Dimension(500, 500));
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jp_image = new JPanel();
        jp_option = new JPanel();

        jp_image.setPreferredSize(new Dimension(200, 500));
        jp_option.setPreferredSize(new Dimension(300, 500));

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true, jp_option,  jp_image);
        jf.getContentPane().add(splitPane);
        jf.setSize(1900, 1000);
        JMenuBar mbar = new JMenuBar();
        jf.setJMenuBar(mbar);
        JMenu File = new JMenu("File");
        JMenuItem New;
        File.add(New = new JMenuItem("New..."));
        mbar.add(File);
        jf.setVisible(true);
        New.addActionListener(this); 
  }


  public void actionPerformed(ActionEvent ae){
      String arg = ae.getActionCommand();
        if(arg.equals("New...")){
                File file = null;
                JFileChooser chooser = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(" jpg,gif,jpeg,png", "jpg", "gif" , "jpeg" , "png");
                chooser.setFileFilter(filter);
                int returnVal = chooser.showOpenDialog(null);
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    System.out.println("File Choosed: " + chooser.getCurrentDirectory() + "\\" +chooser.getSelectedFile().getName());
                    file = chooser.getSelectedFile();
                }           
                try{
                    img = ImageIO.read(file);
                    jp_image.add(new JLabel(new ImageIcon(img)));
                    jp_image.setSize(300, 500); 
                    jf.setPreferredSize(new Dimension(500, 500));// no image appears
                    //jf.pack();    the image appears but the frame resizes     
                }
                catch(Exception e){
                    System.out.println("No Image Selected");
                }
        }


    }

  public static void main(String args[]){
      Test t = new Test();
  }
}

how can i set the image in the panel without resizing the frame.

Upvotes: 2

Views: 52

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285460

You're adding a component to the GUI but not telling the component's container's layout manager that you're doing this. Be sure to call repaint() and revalidate() on the container after adding the JLabel. For example:

img = ImageIO.read(file);
jp_image.add(new JLabel(new ImageIcon(img)));
jp_image.setSize(300, 500);
jf.setPreferredSize(new Dimension(500, 500));// no image appears
jp_image.revalidate();
jp_image.repaint();

Even better- add the JLabel just once on GUI creation and don't re-add it when you get an image, but instead simply set its Icon. For example:

public class Test implements ActionListener {
    ImageIcon imgIcon;
    JFrame jf;
    JPanel jp_image, jp_option;
    BufferedImage img;
    private JLabel imageLabel = new JLabel();

    Test() {
        // ...

        jp_image.add(imageLabel); //!!

        // ...
    }

    public void actionPerformed(ActionEvent ae) {
        String arg = ae.getActionCommand();
        if (arg.equals("New...")) {
            // ...

            try {
                img = ImageIO.read(file);
                imageLabel.setIcon(new ImageIcon(img)); //!!
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

Upvotes: 3

Related Questions