Link to home
Start Free TrialLog in
Avatar of rt_vin
rt_vin

asked on

Adding a window to a container exception... swing/ applet error

I wanted to put a swing application on to a webpage and therefore used a swing program within an applet and then the applet on the webpage... it works fine with a single button or icon but not with panels... please help me solve the error... i have provided the code:

/*SWING PROGRAM*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Test01 extends JFrame
{      
      public Test01()
      {
            super("Example");

        JTextField newItemField;
        JList      itemsList;
        JButton    addButton;
        JButton    removeButton;
        JButton    testButton;

        getContentPane().setLayout(null);

        //The text field
        newItemField = new JTextField();
        newItemField.setLocation(12,12);
        newItemField.setSize(150,30);
        getContentPane().add(newItemField);

        //The Add button
        addButton = new JButton("Add");
        addButton.setMnemonic('A');
        addButton.setLocation(174, 12);
        addButton.setSize(100,30);
        getContentPane().add(addButton);

        //The List
        itemsList = new JList();
        JScrollPane scrollPane = new JScrollPane(itemsList,
        ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setLocation(12,45);
        scrollPane.setSize(150,150);
        getContentPane().add(scrollPane);

        //The Remove button
        removeButton = new JButton("Remove");
        removeButton.setMnemonic('R');
        removeButton.setLocation(174,45);
        removeButton.setSize(100,30);
        getContentPane().add(removeButton);
       
        //The Test button
        testButton = new JButton("Test");
        testButton.setMnemonic('T');
        testButton.setLocation(1,1);
        testButton.setSize(100,30);
        getContentPane().add(testButton);  
     
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(null);
        buttonPane.setLocation(11,198);
        buttonPane.setSize(150,150);
        buttonPane.add(testButton);
            getContentPane().add(buttonPane);
            buttonPane.setBorder(BorderFactory.createLineBorder(Color.black));
           
    }

   
    private static void createAndShowGUI() {
        //Create and set up the window.         
        JFrame frame = new Test01();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
               
        int SIDE_FRAME_OFFSETS = 6;
        int MENU_BAR_OFFSETS = 20+6;
        frame.setSize(600 + SIDE_FRAME_OFFSETS, 500 + MENU_BAR_OFFSETS);
        frame.setResizable(false);
        frame.setVisible(true);
    }      

      public static void main(String[] arg)
      {
            
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });        
      }      
      
}




/*APPLET PROGRAM*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;


public class Test01applet extends Applet
{
      Test01 x;

      //--- constructor
      public Test01applet()
      {
            setBackground(Color.yellow);
            x = new Test01();
            setLayout(new BorderLayout());
            add(x,BorderLayout.CENTER);
      }
      
      public void init(){}
      
}




/*HTML PAGE*/
<HTML>
<BODY>
<br><br><br>
<DIV ALIGN="center">
<APPLET CODE="Test01applet.class" name="Control" WIDTH=700
HEIGHT=600 >
</APPLET>
<br>
</DIV>
</BODY>
</HTML>


Thanks in advance,

Vinny.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to use JApplet - Swing and AWT don't mix well
Avatar of rt_vin
rt_vin

ASKER

Can you tell me how i can go about using that?
That's not the main problem though - you can't add a window to a container. You could either take all the components *on* the window and put them on the applet or create the window and *show* it in the applet:

     public void init() {

                setBackground(Color.yellow);
                x = new Test01();
                x.setSize(200, 200);
                x.setVisible(true);
    }
Avatar of rt_vin

ASKER

Thanks for your continued replies...

Was that init function, 'create the window and *show* it in the applet'?, cos it still doesn't work... which of the two ways that you gave is easier? Can you show me for my example?

Please help me with this, i've been stuck for days :(
>>Was that init function, 'create the window and *show* it in the applet'?,

Yes. Save this as Test01applet.java, compile and run as

appletviewer Test01applet.java

SNIP===============================================



/*
<applet codebase="." code="Test01applet.class" width="300" height="300">
</applet>
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Test01 extends JFrame
{
     public Test01()
    {
         super("Example");

       JTextField newItemField;
       JList      itemsList;
       JButton    addButton;
       JButton    removeButton;
       JButton    testButton;

       getContentPane().setLayout(null);

       //The text field
       newItemField = new JTextField();
       newItemField.setLocation(12,12);
       newItemField.setSize(150,30);
       getContentPane().add(newItemField);

       //The Add button
       addButton = new JButton("Add");
       addButton.setMnemonic('A');
       addButton.setLocation(174, 12);
       addButton.setSize(100,30);
       getContentPane().add(addButton);

       //The List
       itemsList = new JList();
       JScrollPane scrollPane = new JScrollPane(itemsList,
       ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
       ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
       scrollPane.setLocation(12,45);
       scrollPane.setSize(150,150);
       getContentPane().add(scrollPane);

       //The Remove button
       removeButton = new JButton("Remove");
       removeButton.setMnemonic('R');
       removeButton.setLocation(174,45);
       removeButton.setSize(100,30);
       getContentPane().add(removeButton);

       //The Test button
       testButton = new JButton("Test");
       testButton.setMnemonic('T');
       testButton.setLocation(1,1);
       testButton.setSize(100,30);
       getContentPane().add(testButton);

        JPanel buttonPane = new JPanel();
       buttonPane.setLayout(null);
       buttonPane.setLocation(11,198);
       buttonPane.setSize(150,150);
       buttonPane.add(testButton);
         getContentPane().add(buttonPane);
         buttonPane.setBorder(BorderFactory.createLineBorder(Color.black));

    }


   private static void createAndShowGUI() {
       //Create and set up the window.
       JFrame frame = new Test01();
       frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

       int SIDE_FRAME_OFFSETS = 6;
       int MENU_BAR_OFFSETS = 20+6;
       frame.setSize(600 + SIDE_FRAME_OFFSETS, 500 + MENU_BAR_OFFSETS);
       frame.setResizable(false);
       frame.setVisible(true);
   }

     public static void main(String[] arg)
    {

       //Schedule a job for the event-dispatching thread:
       //creating and showing this application's GUI.
       javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               createAndShowGUI();
           }
       });
    }

}





public class Test01applet extends JApplet
{
    Test01 x;

     public void init() {

                setBackground(Color.yellow);
                x = new Test01();
                x.setSize(200, 200);
                x.setVisible(true);
    }

}


Avatar of rt_vin

ASKER

BRILLIANT!!!

Ok, i know i'm being fussy, but why does it open in a new window?... Can't it open within the webpage?

Thanks for all your help :)
>>Can't it open within the webpage?

You could do. Change

>>

                x.setSize(200, 200);
                x.setVisible(true);
>>

to

add(x.getContentPane());
Avatar of rt_vin

ASKER

Ok, that doesn't work... it says 'Applet control notinited' on the webpage... if you haven't already have enough of me then can you please tell me what that means :o)
Works ok for me. You need to look at your browser's java console and look for exceptions
Avatar of rt_vin

ASKER

Oh ok, it just flashs quickly... it says "error: java.lang.Error: Do not use Test01applet.add() use Test01applet.getContentPane().add() instead." (??)
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rt_vin

ASKER

THANK YOU SO SO SO SO MUCH!!!!!!!! :o)
8-)