#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    23
    Rep Power
    0

    Java image display


    Simple question here I basically want to display an image within the frame from the click of a button. My problem is getting that image to display. Here is the code Thanks...
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MyImage extends JFrame
    {
    	private JButton open;
    	private JPanel mainPanel;
    
    	public MyImage()
    	{
    		setTitle("Open Image");
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setVisible(true);
    		setSize(700,700);
    
    		open = new JButton("open");
    		open.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				ImageIcon zz = new ImageIcon("barkley.jpg");
    				//mainPanel.add(zz);
    				setVisible(true);
    				pack();
    			}
    
    		});
    		mainPanel = new JPanel();
    		getContentPane().add(mainPanel);
    		mainPanel.setLayout(null);
    		mainPanel.add(open);
    		mainPanel.add(zz);
    		open.setBounds(0,0,100,100);
    	}
    		public static void main(String[] args)
    		{
    			MyImage mi = new MyImage();
    		}
    
    	}
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0
    In actionPerformed, where you commented out mainPanel.add(zz),
    Try changing it to

    Code:
    mainPanel.add(new JLabel(zz));
    It's now a component, and you can resize it and etc like how you would with any other components.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    23
    Rep Power
    0
    Thanks found a solution to my code,
    PS, I know my current solution is sloppy but at least i got it to work for me
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MyImage extends JFrame
    {
    	private JButton open, remove;
    	private JPanel mainPanel;
    	private JLabel imgLabel;
    
    	public MyImage()
    	{
    		setTitle("Open Image");
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setVisible(true);
    		setSize(700,700);
    
    		open = new JButton("open");
    		open.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				ImageIcon zz = new ImageIcon("barkley.jpg");
    				//imgLabel = new JLabel();
    				imgLabel.setIcon(zz);
    
    				mainPanel.add(imgLabel);
    			//	mainPanel.add(new JLabel(zz));
    				//imgLabel.setBounds(50,50,200,200);
    				//mainPanel.validate();
    				//mainPanel.repaint();
    				//imgLabel.setVisible(true);
    				//mainPanel.add(imgLabel);
    				//setVisible(true);
    				//pack();
    			}
    
    
    		});
    		remove = new JButton("remove");
    		remove.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				imgLabel.setVisible(false);
    			}
    
    		});
    		imgLabel = new JLabel();
    		mainPanel = new JPanel();
    		getContentPane().add(mainPanel);
    		mainPanel.setLayout(null);
    		mainPanel.add(open);
    		//mainPanel.add(imgLabel);
    		mainPanel.add(remove);
    		imgLabel.setBounds(50,50,400,400);
    		open.setBounds(0,0,100,100);
    		remove.setBounds(100,50,100,100);
    	}
    	/*public void paintComponent(Graphics g)
    	{
    		if(this.imgLabel!=null)
    		{
    			g.drawImage(imgLabel, 0,0,null);
    		}
    	} */
    		public static void main(String[] args)
    		{
    			MyImage mi = new MyImage();
    		}
    
    	}

IMN logo majestic logo threadwatch logo seochat tools logo