Problem Adding components to JApplet ...

I have a JApplet that is currently displaying a game which is basically a 2D image array with images that are shuffled around and need to be sorted...

I'm having problems adding Swing components to said JApplet!!!

JApplet:

void addComponents ()

{

JLabel bottomLabel =new JLabel ("this is quite clearly the bottom!!!");

JPanel pan =new JPanel ();

bottomLabel.setHorizontalAlignment (JLabel.CENTER);

getContentPane().add (bottomLabel, BorderLayout.SOUTH);

}

/////////

publicvoid init ()

{

addComponents();

}

which is being instantiated like this:

publicvoid gameInit (int x_value,int y_value, String img_path)

{

SliderIMG2 Slider =new SliderIMG2 ();

setContentPane (Slider);

Slider.runGame (x_value, y_value, img_path);//just calls init() and sets variables

////////////

setVisible (true);

}

At the moment I'm just getting my JApplet with the images, but no JLabel

Any help appreciated!!

[1690 byte] By [Hagena] at [2007-11-27 10:58:27]
# 1

that's not enough source code to see what you are doing wrong.

bsampieria at 2007-7-29 12:15:36 > top of Java-index,Desktop,Core GUI APIs...
# 2

hmm okay, anything specific you'd like to see? My code for the JApplet class is about 600 lines, and there are about 5 other classes i'm using aswell..!

btw, I changed the code in the instantiation code to:

getContentPane().add (Slider);

instead of

setContentPane (Slider);-- but no luck...

null

Message was edited by:

Hagen

Hagena at 2007-7-29 12:15:36 > top of Java-index,Desktop,Core GUI APIs...
# 3

> which is being instantiated like this:

No idea what you are doing. An applet is invoked from a browser. You don't instantiate an applet. The browser loads the class and invoked the init() method.

Here is a complete Applet:

// <applet code="AppletTest.class" width="300" height="100"></applet>

import java.awt.*;

import javax.swing.text.*;

import javax.swing.*;

public class AppletTest extends JApplet

{

public void init()

{

JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

buttonPanel.add( new JButton("Button1") );

buttonPanel.add( new JButton("Button2") );

getContentPane().add(buttonPanel, BorderLayout.NORTH);

getContentPane().add( new JTextArea() );

getContentPane().add(new JButton(new DefaultEditorKit.CopyAction()), BorderLayout.SOUTH);

}

}

Which can be tested by using:

appletviewer AppletTest.java

You don't need 500 lines of code to demonstrate a problem....

camickra at 2007-7-29 12:15:36 > top of Java-index,Desktop,Core GUI APIs...
# 4

Let me try again with the code...

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.image.*;

import javax.swing.*;

import java.applet.*;

public class SliderIMG2 extends JApplet implements KeyListener

{

//DECLERATION OF CLASS VARIABLES

public void init ()

{

//ASSIGNING VALUES TO SOME VARIABLES ETC ETC...

addComponents ();

repaint ();

}

public void runGame (int x_value, int y_value, String path)

{

x = x_value;

y = y_value;

img_path = path;

init ();

}

paint (g){

}

update(g){

}

}

That's basically the structure of my JApplet; if I knew where I was going wrong I'd obviously include only the code which I knew to be faulty, but seeing as I don't, I'm trying my best to isolate where the problem could be without adding 500 lines of code here... =/

I'm not invoking anything from a browser; I have a standalone swing GUI interface, where a button-click starts the JApplet...

Here is a gameLauncher() class that I use to ....(I don't know what the correct terminology is) ...run the **** thing!! :)

import javax.swing.*;

import java.awt.*;

public class GameLauncher extends JFrame

{

int x_value;

int y_value;

String img_path;

Tools imt;

public void gameInit (int x_value, int y_value, String img_path)

{

ImageIcon dimensions = new ImageIcon (img_path);

setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

setBounds (200, 200, (dimensions.getIconWidth () + 35), dimensions.getIconHeight () + 50));

SliderIMG2 Slider = new SliderIMG2 ();

getContentPane().add (Slider);

Slider.runGame (x_value, y_value, img_path);

setVisible (true);

}

}

sorry for sounding cocky, I have deadline stress.

Hagena at 2007-7-29 12:15:36 > top of Java-index,Desktop,Core GUI APIs...
# 5

Ah wow! It's amazing where a bit of logic can get one...

I figured out the problem: I was adding (trying to add) components into the JApplet, but my JApplet is actually... uhm, in the gameLauncher JFrame...

All fixed now, I'm just building whatever GUI I want into the gameLauncher JFrame and it's all working out for me so far :)

thanks for the help! BTW if I'm still doing something I shouldn't be doing some advice would be much appreciated!

Hagena at 2007-7-29 12:15:36 > top of Java-index,Desktop,Core GUI APIs...
# 6

You are creating an application not an applet. You should not be extending JApplet. You simply add your components to the content pane of a JFrame.

camickra at 2007-7-29 12:15:36 > top of Java-index,Desktop,Core GUI APIs...