6 Replies - 5539 Views - Last Post: 28 October 2011 - 05:51 AM Rate Topic: -----

#1 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

How to add image to JFrame Code

Posted 24 October 2011 - 06:58 AM

Hello everyone, I would like to add an image into my menu, but am unable to figure out how to accomplish this. any help would be great. Thanks.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class StudentMenu extends JFrame{
	public StudentMenu()


	{

		super("Student Registration System");

		JMenu file = new JMenu("File");
		JMenuItem RegItem = new JMenuItem("Register for a Course");
		file.add(RegItem);
		JMenuItem DropItem = new JMenuItem("Drop/Remove a Course");
		file.add(DropItem);
		JMenuItem SearchItem = new JMenuItem("Search for a Course");
		file.add(SearchItem);
		JMenuItem PrintItem = new JMenuItem("Print Class Schedule");
		file.add(PrintItem);
		JMenuItem AccountItem = new JMenuItem("Account Summary");
		file.add(AccountItem);
		JMenuItem exitItem = new JMenuItem("Logout");
		file.add(exitItem);

		JMenuBar bar = new JMenuBar();
		setJMenuBar(bar);
		bar.add(file);

		getContentPane();
		setSize(400, 200);
		setVisible(true);
	}
}


This post has been edited by markjnj: 24 October 2011 - 07:01 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to add image to JFrame Code

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: How to add image to JFrame Code

Posted 24 October 2011 - 07:04 AM

You can create an ImageIcon object, passing the pass to the image and pass this object to the contructor of the JMenuItem -

ImageIcon icon = new ImageIcon("someImage.png");

JMenuItem iconMenuItem = new JMenuItem(icon);

// add to JMenu ...

Was This Post Helpful? 0
  • +
  • -

#3 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

Re: How to add image to JFrame Code

Posted 24 October 2011 - 07:24 AM

Thanks Ryano, I put the code in but how do I actually get it to show up? I thought it was as east as setting the size and visible to true but that resizes my whole menu.

View PostRyano121, on 24 October 2011 - 07:04 AM, said:

You can create an ImageIcon object, passing the pass to the image and pass this object to the contructor of the JMenuItem -

ImageIcon icon = new ImageIcon("someImage.png");

JMenuItem iconMenuItem = new JMenuItem(icon);

// add to JMenu ...

Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: How to add image to JFrame Code

Posted 24 October 2011 - 07:28 AM

What do you mean by getting it to show?

Do you mean the JFrame itself or the menu?
Was This Post Helpful? 0
  • +
  • -

#5 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

Re: How to add image to JFrame Code

Posted 24 October 2011 - 07:40 AM

I wasn't sure if this was going to be possible or not using JFrame, it prob would've better been suitable for JPanel, but I have a drop down Menu and wanted an image or background image to appear in the unused area of the window.
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: How to add image to JFrame Code

Posted 24 October 2011 - 07:51 AM

Aah I see. In that case you want to add the image to the actual JFrame instead of the menu.

You may want to check this link in that case (just take notice of the paintComponent method). You will have to create a JPanel that covers the rest of the window and paint the image into the panel.

Also I see a lack of this very important line in your code - -

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


It may be in your main method, but it just makes sure that the program closes when you hit the big red X.
Was This Post Helpful? 1
  • +
  • -

#7 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

Re: How to add image to JFrame Code

Posted 28 October 2011 - 05:51 AM

Thanks ryano!

Sent from my HTC Glacier using Tapatalk
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1