6 Replies - 480 Views - Last Post: 01 May 2013 - 08:15 AM Rate Topic: -----

#1 JAVA Person  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 30-April 13

Adding a JApplet to a JFrame?

Posted 30 April 2013 - 09:13 PM

Hey everyone,
I'm pretty new to JAVA, and for one of my programs, I wanted to implement a JApplet in a JFrame, but for some reason it never works. I've been looking online for examples, but either I'm too new to understand it, or the example code doesn't work for me. So can someone write me a short program that works, or just tell me how to do it? Also, I'm fine with the Applet being a pop-up window if it has to.
Is This A Good Question/Topic? 1
  • +

Replies To: Adding a JApplet to a JFrame?

#2 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Adding a JApplet to a JFrame?

Posted 30 April 2013 - 09:28 PM

Quote

So can someone write me a short program that works, or just tell me how to do it?

Hmmm, no. That wouldn't be fair to your learning process.

Why don't you show us what you've already tried, and how the output is different than expected, and we'll help you from there.

This post has been edited by Gungnir: 30 April 2013 - 09:31 PM

Was This Post Helpful? 0
  • +
  • -

#3 JAVA Person  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 30-April 13

Re: Adding a JApplet to a JFrame?

Posted 30 April 2013 - 10:05 PM

This is what I have tried:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DoubleFrame extends JPanel
{
	private JFrame frame;
	
	public static void main (String [] args)
	{
		DoubleFrame frame = new DoubleFrame();
		frame.run();
	}
	public void run()
	{
		frame = new JFrame("A Frame Test");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 500);
		frame.setBackground(Color.blue);
		frame.setVisible(true);
		Test test = new Test();
		frame.getContentPane().add(test, BorderLayout.NORTH);
	}
	class Test extends JApplet implements ActionListener //My Applet class
	{
		private JButton test; //A JButton for the Applet
		public void init()
		{
			setLayout(new FlowLayout()); //Sets the Layout for the Applet
			test = new JButton("Test");
			test.addActionListener(this); //Adds ActionListener to the Button
			this.add(test); //Adds the JButton to the Applet
		}
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == test)
			{
				System.out.print("Clicked!"); //Prints out if the button is clicked
			}
		}
	}
}

There's probably a lot wrong here, but basically, nothing appears, just the JFrame part. I would think that you just have to add the applet to the frame, but apparently not.
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 858
  • View blog
  • Posts: 2,612
  • Joined: 29-July 11

Re: Adding a JApplet to a JFrame?

Posted 30 April 2013 - 10:32 PM

No no no. What you do is create your application on a JPanel, then you either add that JPanel to a JApplet, or you add it to a JFframe, depending on which you want. This makes your application modular so it can be plugged into either interface.
Was This Post Helpful? 1
  • +
  • -

#5 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Adding a JApplet to a JFrame?

Posted 30 April 2013 - 10:47 PM

I think you want a JPanel, not a JApplet.

Further Reading:
- Stack Overflow > Java - Adding a JApplet into JFrame
Was This Post Helpful? 2
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,883
  • Joined: 06-March 08

Re: Adding a JApplet to a JFrame?

Posted 01 May 2013 - 03:57 AM

View Postfarrell2k, on 01 May 2013 - 01:32 AM, said:

No no no. What you do is create your application on a JPanel, then you either add that JPanel to a JApplet, or you add it to a JFframe, depending on which you want. This makes your application modular so it can be plugged into either interface.

Don't rely on people used to use GUI builder.

Your code is perfect, the only thing missing is:

		Test test = new Test();
		test.init();    <------



When an Applet is launched by a browser, the browser calls it's init method
When you put it in an applicattion, you neeed to call the init() method yourself
Was This Post Helpful? 1
  • +
  • -

#7 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 858
  • View blog
  • Posts: 2,612
  • Joined: 29-July 11

Re: Adding a JApplet to a JFrame?

Posted 01 May 2013 - 08:15 AM

View Postpbl, on 01 May 2013 - 10:57 AM, said:

View Postfarrell2k, on 01 May 2013 - 01:32 AM, said:

No no no. What you do is create your application on a JPanel, then you either add that JPanel to a JApplet, or you add it to a JFframe, depending on which you want. This makes your application modular so it can be plugged into either interface.

Don't rely on people used to use GUI builder.

Your code is perfect, the only thing missing is:


His code is far from perfect. He leaks "this" in a constructor, is not following Swing thread safety rules, and that println() will only work when the JApplet is in a JFrame, then you have to start the JApplet life cycle yourself. It's best to follow my advice to create you application in a JPanel and add it to the frame or the applet as needed. Gungnir provided a great link to an example. Bad way to go about it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1