CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2013
    Posts
    22

    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
    Join Date
    Jun 2013
    Location
    New York, USA
    Posts
    21

    Re: java image display

    Why is //mainPanel.add(zz); commented out?

  3. #3
    Join Date
    Feb 2013
    Posts
    22

    Re: java image display

    i commented out mainPanel.add(zz) because i thought since i had it elsewhere in my code i didn't need it to be there. But i found the solution to my problem. 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();
    		}
    
    	}

  4. #4

    Re: java image display

    Quote Originally Posted by jumpman8947 View Post
    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.
    Google is a good friend. Check following links:

    http://www.coderanch.com/t/427715/GU...-image-Trouble
    http://stackoverflow.com/questions/1...ing-java-swing

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: java image display

    If you feel your problem has been solved, please mark your thread Resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured