Applet in a frame

Hi,
I have a webpage with two frames; one contains an applet with the AppletContext.showDocument() method. I want this method to open the webpage in the second frame on the same page. My applet updates the webpage at an interval, so I don't want to destroy it. How can I make this happen?
Thanks.

Nevermind. I figured it out. You just have to specify the name of the second frame in the target parameter of showDocument method.

Similar Messages

  • How to communicate with another applet in different frame?

    Hello,evryone
    How to communicate with another applet in different frame?
    Can you give some advices?
    thank you!
    zhongboqing

    i faced this problem one year ago.It would be something like that:
    first you have to get the applet context 'getAppletContext()' (which is the current frame).
    Then get parent of that context 'getParent()' (which is the browser context).
    Then u can access the desired frame by its name. Finally you can access the desired applet located within this frame by

  • Displaying applet in separate frame

    hi!
    i am using following code to display applet in separate frame.
    <applet
    ARCHIVE="applet.jar"
    CODE="applet.class"
    WIDTH=600 HEIGHT=600>
    <param name="config" value="applet.conf">
    <PARAM NAME="sepframe" value="true">
    </applet>
    so the applet gets displayed in separate frame. but at the same time.
    i also see a 'gray area ' on my jsp page. how do i remove that?
    thanks
    bandya

    maybe if you try something like this
    <applet ... WIDTH=0 HEIGHT=0>
    </applet>
    the frame should open according to preferedSize of the applet
    so in the applet code just set the prefferedSize to 600x600

  • Converting an applet into a frame

    hello.
    this is james mcfadden. I am developing a 2-player BlackJack card game in java. how do i convert the following 2 pieces of code from an applet into a frame?
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    public class Blackjack extends Applet implements ActionListener,Runnable{
         private Deck deck;     //The deck...
         private Hand player_hand;//player hand
         private Hand dealer_hand;//dealer hand.
         private Button bHit=new Button("Hit");
         private Button bStay=new Button("Stay");
         private Button bRestart=new Button("New Game");
         //lets do some double buffering.
         Image offIm=null;
         Graphics offGraphics=null;
         Dimension offDimension=null;
         Image[] card_images = new Image[52];
         private boolean cards_loaded = false;
         private int current_card_loading = 0;
         String message;                         //print a message...
         int width;          //The width of the applet
         int height;          //The height of the applet
         int card_width = -1;
         int card_padding = -1;     //How much should we pad the cards by..?
         private int games=-1;     //How many games have been played?
         public void init() {
              Thread card_loader = new Thread(this);
              card_loader.start();
         public void newGame(){
              bHit.setEnabled(true);
              bStay.setEnabled(false);
              player_hand = new Hand();     //Create new hands...
              dealer_hand = new Hand();
              //shuffle the deck.
              deck.shuffleCards();
              games++;
         public void hit(){
              bStay.setEnabled(true);
              player_hand.addCard(deck.dealCard());
              if(player_hand.getValue() > 21){
                   message = "You loose! (score:"+player_hand.getValue()+")";
                   //disable the hit and stay buttons...
                   bHit.setEnabled(false);
                   bStay.setEnabled(false);
                   return;
              message = "Score: "+player_hand.getValue();     
         public void stay(){
              while(dealer_hand.getValue() < 17){
                   dealer_hand.addCard(deck.dealCard());
              if(dealer_hand.getValue() <= 21 && player_hand.getValue() < dealer_hand.getValue())
                   message = "You loose! (" + player_hand.getValue()+
                             " - "+dealer_hand.getValue()+")";
              }else if (player_hand.getValue() == dealer_hand.getValue())
                   message = "You draw! (" + player_hand.getValue()+")";
              }else {
                   message = "You win! (" + player_hand.getValue()+
                             " - "+dealer_hand.getValue()+")";
              bHit.setEnabled(false);
              bStay.setEnabled(false);
         public void actionPerformed(ActionEvent e) {
              if(e.getSource() == bRestart)
                   newGame();
              }else if (e.getSource() == bHit)
                   hit();
              }else if (e.getSource() == bStay)
                   stay();
              repaint();
         public void paint(Graphics g) {
              update(g);
         public void update(Graphics g) {
              //lets sord out double buffering
              if(offGraphics==null){
                   offIm=createImage(getSize().width,getSize().height);
                   offGraphics=offIm.getGraphics();
              if(!cards_loaded){
                   //display a message saying we're loading the cards...
                   offGraphics.setFont(new Font("Arial",Font.PLAIN,14));
                   offGraphics.setColor(new Color(171,205,239));
                   offGraphics.fillRect(0,0,getSize().width,getSize().height);
                   offGraphics.setColor(Color.black);
                   offGraphics.drawString("Loading cards... ",5,20);
                   offGraphics.drawRect(15,40, 102 ,10);
                   offGraphics.drawRect(13,38, 106 ,14);
                   offGraphics.setColor(new Color(171,205,239));
                   offGraphics.fillRect(0,0,150,35);
                   offGraphics.setColor(Color.black);
                   offGraphics.fillRect(15,40, (current_card_loading)*2 ,10);
                   offGraphics.drawString("Loading card: "+current_card_loading+1,15,20);
              }else{
                   Image currentCard;
                   while(card_width == -1)
                        card_width = deck.getCard(0).getImage().getWidth(this);
                   if(card_padding == -1)
                        card_padding = (width - (card_width * 2) - 4) / 4;
                   //clear the background...
                   offGraphics.setColor(Color.blue);
                   offGraphics.fillRect(0,0,width,height);
                   offGraphics.setColor(Color.black);
                   offGraphics.fillRect(1,1,width-2,height-2);
                   offGraphics.setColor(Color.white);
                   offGraphics.drawString("PLAYER:",card_padding,40);
                   offGraphics.drawString("DEALER:",(width/2) + card_padding,40);
                   offGraphics.drawString(message,5,height - 20);
                   if(games > 0)
                        offGraphics.drawString(games + " game(s) played...",5,height - 10);
                   //Draw the players hand...
                   for(int i=0;i<player_hand.getCardCount();i++){
                        currentCard = player_hand.getCards().getImage();
                        offGraphics.drawImage(currentCard, card_padding, 70+(20*(i-1)), this);
                   //now draw the dealers hand...
                   for(int i=0;i<dealer_hand.getCardCount();i++){
                        currentCard = dealer_hand.getCards()[i].getImage();
                        offGraphics.drawImage(currentCard, (width/2 ) + card_padding, 70+(20*(i-1)), this);
              //draw buffered image.
              g.drawImage(offIm,0,0,this);
    public void run(){
              MediaTracker t=new MediaTracker(this);
              System.out.println("Applet getting cards...");
              for(current_card_loading=0; current_card_loading < 52; current_card_loading++){
              //try{
                   card_images[current_card_loading] = getImage(getCodeBase(),
                                                                "cards/" + (current_card_loading+1) + ".gif");
                   if(card_images[current_card_loading] == null){
                        System.out.println("Null card... ["+current_card_loading+"]");
                   }else{
                        t.addImage(card_images[current_card_loading],0);
                   try{
                        t.waitForID(0);
                   }catch(InterruptedException e){
                        System.err.println("Interrupted waiting for images..>");
                   //lets show our new status.
                   repaint();
              //create the deck object now
              deck = new Deck(this,card_images);     //Create a new deck object.
              card_width = deck.getCard(0).getImage().getWidth(this);
              bHit.addActionListener(this);
              bStay.addActionListener(this);
              bRestart.addActionListener(this);
              bHit.setEnabled(false);
              bStay.setEnabled(false);
              width = getSize().width;
              height = getSize().height;
              this.add(bHit);
              this.add(bStay);
              this.add(bRestart);
              player_hand = new Hand();     //Create the hands...
              dealer_hand = new Hand();
              //make sure that the buttons appear properly.
              this.validate();
              message = "";
              cards_loaded = true;
              System.out.println("End of thread...");
              repaint();
    import java.awt.*;
    import java.applet.*;
    import java.net.*;
    import java.io.*;
    class Deck {
         Card[] cards = new Card[52];
         int currCard;
         public Deck(Blackjack myApplet,Image[] card_images) {
              // These are the (initial) values of all the cards
              int[] values = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                   11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                   11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                   11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};
              // Make all the cards, getting the images individually
              for (int i=0; i<52; i++) {
    //               Image image = myApplet.getImage(myApplet.getCodeBase(), "cards/" + (i+1) + ".gif");
                   cards[i] = new Card(card_images, values[i]);
              // Shuffle the cards.
              shuffleCards();
         public void shuffleCards(){
              Card temp;
              //loop through each card in the deck and swap it with some random card.
              for (int i=0; i<52; i++) {
                   int rand = (int)(Math.random()*52);
                   temp = cards[51-i];//take the current card...
                   cards[51-i] = cards[rand];//and swap it with some random card...
                   cards[rand] = temp;
              //now, reset the currentCard to deal...
              currCard = 0;
         public Card dealCard() {
              Card card = cards[currCard];
              currCard++;
              return card;
         public Card getCard(int i) {
              return cards[i];

    thanks for the advice.
    i've done what you suggested. but when i went try to compile the 2 programs i get error messages (shown below along with the modified code). do you know what are causing these error messages?
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    public class Blackjack extends JFrame implements ActionListener,MouseListener{
         private Deck deck;     //The deck...
         private Hand player_hand;//player hand
         private Hand dealer_hand;//dealer hand.
         //private Button bHit=new Button("Hit");
         JButton bHit=new JButton("Hit");
         //private Button bStay=new Button("Stay");
         JButton bStay=new JButton("Stay");
         //private Button bRestart=new Button("New Game");
         JButton bRestart=new JButton("New Game");
         //lets do some double buffering.
         Image offIm=null;
         Graphics offGraphics=null;
         Dimension offDimension=null;
         Image[] card_images = new Image[52];
         private boolean cards_loaded = false;
         private int current_card_loading = 0;
         String message;                         //print a message...
         int width;          //The width of the applet
         int height;          //The height of the applet
         int card_width = -1;
         int card_padding = -1;     //How much should we pad the cards by..?
         private int games=-1;     //How many games have been played?
         public static void main(String[] args){
               Blackjack bj =new Blackjack(); 
              bj.pack();
              bj.setVisible(true);
         public Blackjack(){
            Thread card_loader = new Thread(this);
              card_loader.start();
              bHit.addMouseListener(this);
          bHit.addActionListener(this);
          //bHit.addMouseMotionListener(this);
              add(bHit);
              bStay.addMouseListener(this);
          bStay.addActionListener(this);
          //bStay.addMouseMotionListener(this);
              add(bStay);
              bRestart.addMouseListener(this);
          bRestart.addActionListener(this);
          //bRestart.addMouseMotionListener(this);
              add(bRestart);
              JFrame frame=new JFrame("BlackJack");
              frame.setSize(400,200);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.add(this);
            frame.pack();
            frame.setVisible(true);
         public void newGame(){
              bHit.setEnabled(true);
              bStay.setEnabled(false);
              player_hand = new Hand();     //Create new hands...
              dealer_hand = new Hand();
              //shuffle the deck.
              deck.shuffleCards();
              games++;
         public void hit(){
              bStay.setEnabled(true);
              player_hand.addCard(deck.dealCard());
              if(player_hand.getValue() > 21){
                   message = "You loose! (score:"+player_hand.getValue()+")";
                   //disable the hit and stay buttons...
                   bHit.setEnabled(false);
                   bStay.setEnabled(false);
                   return;
              message = "Score: "+player_hand.getValue();     
         public void stay(){
              while(dealer_hand.getValue() < 17){
                   dealer_hand.addCard(deck.dealCard());
              if(dealer_hand.getValue() <= 21 && player_hand.getValue() < dealer_hand.getValue()){
                   message = "You loose! (" + player_hand.getValue()+" - "+dealer_hand.getValue()+")";
              else if (player_hand.getValue() == dealer_hand.getValue()){
                   message = "You draw! (" + player_hand.getValue()+")";
              else{
                   message = "You win! (" + player_hand.getValue()+" - "+dealer_hand.getValue()+")";
              bHit.setEnabled(false);
              bStay.setEnabled(false);
         public void actionPerformed(ActionEvent e){
              if(e.getSource() == bRestart){
                   newGame();
              else if (e.getSource() == bHit){
                   hit();
              else if (e.getSource() == bStay){
                   stay();
              repaint();
         public void paint(Graphics g){
              update(g);
         public void update(Graphics g){
              //lets sord out double buffering
              if(offGraphics==null){
                   offIm=createImage(getSize().width,getSize().height);
                   offGraphics=offIm.getGraphics();
              if(!cards_loaded){
                   //display a message saying we're loading the cards...
                   offGraphics.setFont(new Font("Arial",Font.PLAIN,14));
                   offGraphics.setColor(new Color(171,205,239));
                   offGraphics.fillRect(0,0,getSize().width,getSize().height);
                   offGraphics.setColor(Color.black);
                   offGraphics.drawString("Loading cards... ",5,20);
                   offGraphics.drawRect(15,40, 102 ,10);
                   offGraphics.drawRect(13,38, 106 ,14);
                   offGraphics.setColor(new Color(171,205,239));
                   offGraphics.fillRect(0,0,150,35);
                   offGraphics.setColor(Color.black);
                   offGraphics.fillRect(15,40, (current_card_loading)*2 ,10);
                   offGraphics.drawString("Loading card: "+current_card_loading+1,15,20);
              else{
                   Image currentCard;
                   while(card_width == -1){
                        card_width = deck.getCard(0).getImage().getWidth(this);
                   if(card_padding == -1){
                        card_padding = (width - (card_width * 2) - 4) / 4;
                   //clear the background...
                   offGraphics.setColor(Color.blue);
                   offGraphics.fillRect(0,0,width,height);
                   offGraphics.setColor(Color.black);
                   offGraphics.fillRect(1,1,width-2,height-2);
                   offGraphics.setColor(Color.white);
                   offGraphics.drawString("PLAYER:",card_padding,40);
                   offGraphics.drawString("DEALER:",(width/2) + card_padding,40);
                   offGraphics.drawString(message,5,height - 20);
                   if(games > 0){
                        offGraphics.drawString(games + " game(s) played...",5,height - 10);
                   //Draw the players hand...
                   for(int i=0;i<player_hand.getCardCount();i++){
                        currentCard = player_hand.getCards().getImage();
                        offGraphics.drawImage(currentCard, card_padding, 70+(20*(i-1)), this);
                   //now draw the dealers hand...
                   for(int i=0;i<dealer_hand.getCardCount();i++){
                        currentCard = dealer_hand.getCards()[i].getImage();
                        offGraphics.drawImage(currentCard, (width/2 ) + card_padding, 70+(20*(i-1)), this);
              //draw buffered image.
              g.drawImage(offIm,0,0,this);
    public void run(){
              MediaTracker t=new MediaTracker(this);
              System.out.println("Frame getting cards...");
              for(current_card_loading=0; current_card_loading < 52; current_card_loading++){
              //try{
                   card_images[current_card_loading] = getImage(getCodeBase(),"cards/" + (current_card_loading+1) + ".gif");
                   if(card_images[current_card_loading] == null){
                        System.out.println("Null card... ["+current_card_loading+"]");
                   else{
                        t.addImage(card_images[current_card_loading],0);
                   try{
                        t.waitForID(0);
                   catch(InterruptedException e){
                        System.err.println("Interrupted waiting for images..>");
                   //lets show our new status.
                   repaint();
              //create the deck object now
              deck = new Deck(this,card_images);     //Create a new deck object.
              card_width = deck.getCard(0).getImage().getWidth(this);
              bHit.addActionListener(this);
              bStay.addActionListener(this);
              bRestart.addActionListener(this);
              bHit.setEnabled(false);
              bStay.setEnabled(false);
              width = getSize().width;
              height = getSize().height;
              this.add(bHit);
              this.add(bStay);
              this.add(bRestart);
              player_hand = new Hand();     //Create the hands...
              dealer_hand = new Hand();
              //make sure that the buttons appear properly.
              this.validate();
              message = "";
              cards_loaded = true;
              System.out.println("End of thread...");
              repaint();
    ----jGRASP exec: javac -g X:\NETPROG\Assignment2\Blackjack.java
    Blackjack.java:7: Blackjack is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener
    public class Blackjack extends JFrame /*Applet*/ implements ActionListener,MouseListener/*,Runnable*/{
    ^
    Blackjack.java:53: cannot find symbol
    symbol : constructor Thread(Blackjack)
    location: class java.lang.Thread
         Thread card_loader = new Thread(this);
    ^
    Blackjack.java:209: cannot find symbol
    symbol : method getCodeBase()
    location: class Blackjack
                   card_images[current_card_loading] = getImage(getCodeBase(),"cards/" + (current_card_loading+1) + ".gif");
    ^
    3 errors
    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.
    import java.awt.*;
    import javax.swing.JFrame;
    import java.net.*;
    import java.io.*;
    class Deck {
         Card[] cards = new Card[52];
         int currCard;
         public Deck(/*Blackjack myApplet*/Blackjack myFrame,Image[] card_images) {
              // These are the (initial) values of all the cards
              int[] values = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                   11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                   11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                   11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};
              // Make all the cards, getting the images individually
              for (int i=0; i<52; i++) {
                   cards[i] = new Card(card_images, values[i]);
              // Shuffle the cards.
              shuffleCards();
         public void shuffleCards(){
              Card temp;
              //loop through each card in the deck and swap it with some random card.
              for (int i=0; i<52; i++) {
                   int rand = (int)(Math.random()*52);
                   temp = cards[51-i];//take the current card...
                   cards[51-i] = cards[rand];//and swap it with some random card...
                   cards[rand] = temp;
              //now, reset the currentCard to deal...
              currCard = 0;
         public Card dealCard() {
              Card card = cards[currCard];
              currCard++;
              return card;
         public Card getCard(int i) {
              return cards[i];
    ----jGRASP exec: javac -g X:\NETPROG\Assignment2\Deck.java
    Blackjack.java:6: Blackjack is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener
    public class Blackjack extends JFrame implements ActionListener,MouseListener{
    ^
    Blackjack.java:45: cannot find symbol
    symbol : constructor Thread(Blackjack)
    location: class java.lang.Thread
         Thread card_loader = new Thread(this);
    ^
    Blackjack.java:201: cannot find symbol
    symbol : method getCodeBase()
    location: class Blackjack
                   card_images[current_card_loading] = getImage(getCodeBase(),"cards/" + (current_card_loading+1) + ".gif");
    ^
    3 errors
    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

  • Inter Applet Communication across frames - Help Needed

    I am trying inter applet communication across frames. For this to happen I am using an intermidiate
    class which registers two applets and whenever any applet needs reference of other applet it gets it
    through this class.
    The page is an important part of a navigation link. So it is loaded many times while traversing through
    the site.
    Every time I load this page the applet does not paint itself (shows grey or background) and the browser
    stops responding. The machine needs to be restarted. This also happens when we keep that page idle for
    a long time (say 2 hours - session does not time out but applet hangs). I have used another thread object
    which is for utility and accesses the applet in the other frame every 10 seconds or so.
    When the applet hangs it does ot throw any exception or JVM error. This happens on certain machines
    evrytime and never on some machines. The applet hangs only in Microsoft IE 5 & 5.5 and never in Netscape
    4.x.
    What could be the problem?
    Can anyone help me with this problem? Its a deadline project and I can't get through.
    Thanks & Regards,
    Rahul

    Try making the register and getter methods of the intermediate class static synchronized. Then register the applets in their start() methods and unregister them in their stop() methods. Call the getter method of the intermediate class wherever you need access to another applet and never cache the instance you get. You may have to also synchronize all your start() and stop() methods to the intermediate class, as well as all methods that perform interapplet communication.
    Tell me what happenned ...

  • Opening a jsp from an applet in a frame?

    this is the code i'm using to open a new jsp and send some stuff in the header. it works fine but it does not support the frameset i am using... any ideas how i can open this page in the frame?
    thanks
    URL url = getCodeBase();
    try{
    getAppletContext().showDocument (new URL (url+"AcceptHousePos.jsphouse="+myHouse.getString()));
    catch(MalformedURLException e) {
    showStatus("URL not found");

    Do you want to open a jsp application in a frame when you click on the applet?If so, there is a programme in http://www.globalleafs.com -Java-Java Applets 's download section. Check it out.

  • Please help! Image only shows when i use Applets instead of Frame/JFrame

    The following classes don't work because the Canvas shows just background colour. However, when I let my class "MyContainer" extend Applet instead(and running the Applet instead of the StartUp class that creates a JFrame) of Frame/JFrame i get my Image on the Canvas showing, WHY?
    code:
    public class StartUp {
    public static void main(String[] arg) {
    MyContainer my = new MyContainer();
         my.setSize(500, 400);
         my.setVisible(true);
    public class MyContainer extends JFrame {
    MyCanvas m;
    public void MyContainer() {
         m=new MyCanvas();
         this.setSize(320, 320);
    this.setLayout(new GridLayout(1, 1, 0, 0));
    this.add(m);
    this.setVisible(true);
    public class MyCanvas extends Canvas {
    Image im;
    public MyCanvas() {
    im=Toolkit.getDefaultToolkit().createImage("c:\\miniferrari.jpg");
    try {
    jbInit();
    catch(Exception e) {
    e.printStackTrace();
    public void paint(Graphics g) {
    g.drawImage(im, 0, 0, this);
    private void jbInit() throws Exception {
    this.setBackground(Color.darkGray);

    as you are using a JFrame, you should refer to it's contentpane:
    this.getContentPane().setLayout(new GridLayout(1, 1, 0, 0));
    this.getContentPane().add(m);

  • How To open applet inside AWT Frame

    Hi there !
    I want to open my applet directly inside a AWT Frame window from a hyper link...
    That is when a user clicks a hyper link i want to open my applet inside a AWT Frame...I do not want my applet to be opened inside a browser window is there any possibility to do this ...pls help me
    -Priya

    hi,
    i see one way, but i really do not know it works or not.
    Try the following:
    An applet is nothing more then a panel. So you can intanciate the applet:Class class1=new Class.forName(nameOfApplet);
    Applet a=(Applet)class1.newInstance(//maybe a parameter);
    frame.add(a);
    a.init(); //calling the init-method of the appletJust try, but I really can't commit you the workability.
    regards

  • [NEWBIE] How to open a frame from an Applet?

    Hi, I'm trying to open a JFrame from an Applet. Basically, I would like that, once the user selects an option from a JMenu, a Frame would be shown with a text box where user could type some content.
    I built both the Applet and the Frame, and singularly they work. When I execute the main method from the JFrame, the window appears, but when I execute something like the following from the applet:
    MyFrame mf = new MyFrame();
    mf.setVisible(true);nothing is shown. I'm sure that I don't know something, but I just started with applets.
    Thanks for any help,
    Marco

    If MyFrame is a subclass of JFrame, then it should not
    make any difference.
    What do you need the JFrame for? Can this be done
    using another "screen" by using a CardLayout?From a menu, when the user selects 'Start game' I need a text box coming out so that the user can insert a word. This word should then evaluate an instance member of the Applet.
    Marco

  • How to create a frame in a applet?

    Dear All,
    How to create a frame in a applet?
    Thanks in advance
    Kityy

    You can't add a Frame/JFrame to an Applet/JApplet.
    An Applet/JApplet has its origins in the class Panel/JPanel.
    So, if you make your Applet to an application you
    can easily do that by putting your Applet "above" a Frame.
    Maybe you can archieve your goals with some Panels?!

  • Maximizing a frame or applet window

    When you set the size of an applet using the setSize(int, int) method in a frame window or specify the width and height in an applet's tag or comment in the source code, the applet or application window is always in the "Restored" state (meaning it can be resized). However, how can I maximize the window to make it automatically fill the screen? Similarly, what if I want to minimize the window? And furthermore, if the user maximizes or minimizes the window and in the source code I try to resize it, will I get a runtime error or will it change the window to the restored state?

    // <applet code=mark width=400 height=300></applet>
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class mark extends JApplet implements ActionListener {
      JButton smallerButton, largerButton;
      static JFrame frame;
      static boolean isApplet = true;
      public void init() {
        smallerButton = new JButton("Size Smaller");
        smallerButton.addActionListener(this);
        largerButton = new JButton("Resize Larger");
        largerButton.addActionListener(this);
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(smallerButton);
        buttonPanel.add(largerButton);
        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.red);
        Container cp = getContentPane();
        cp.setLayout(new BorderLayout());
        cp.add(buttonPanel, "North");
        cp.add(centerPanel, "Center");
      public void actionPerformed(ActionEvent e) {
        JButton button = (JButton)e.getSource();
        if(isApplet) {
          Frame awtFrame = null;
          Component c = getParent();
          while(c != null) {
            c = c.getParent();
            if(c instanceof Frame) {
              awtFrame = (Frame)c;
              break;
          if(button == smallerButton)
            awtFrame.setExtendedState(Frame.NORMAL);
          if(button == largerButton)
            awtFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
        else {
          if(button == smallerButton)
            frame.setExtendedState(Frame.NORMAL);
          if(button == largerButton)
            frame.setExtendedState(Frame.MAXIMIZED_BOTH);
      public static void main(String[] args) {
        isApplet = false;
        frame = new JFrame("Main Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JApplet applet = new mark();
        frame.getContentPane().add(applet);
        frame.setSize(400,300);
        frame.setLocation(300,200);
        applet.init();
        applet.start();
        frame.setVisible(true);
    }

  • For standalone java applications Applet is better or Frame is better?

    Hi Every body,
    Iam new to java standalone application developement.As per my knowledge both Applet aswell as Frame concept can be used to develop java
    standalone applications, if Iam right. My application need menus, drop down menus and it also need to connect to backend database.So which is better?
    So please some body guide me.
    Thanks in Advance
    Basavaraj M

    A JFrame not a frame will be most apropriate because that just what it supposed to be used for but u can consider a JApplet if u want to incorporate it into a web application.
    Sq2000

  • How can I run an applet from an application (frame)?

    I have an application (frame) that render images and save it, and then I want to create an animation with those images using Animator applet from the application itself, but I do not how to call the applet from the application, is this possible?
    Thanks in advance, Jorge

    You can simply run a JApplet in a JFrame. Just create a frame object, an object of your applet, add the applet to the frame and call the init() function of your applet. It could look like this:
    JFrame anyFrame = new JFrame("MyTitle");
    YourApplet yourApplet = new YourApplet();
    anyFrame.getContentPane().add(yourApplet, BorderLayout.CENTER);
    yourApplet.init();
    anyFrame.setSize(new Dimension(800,600));
    anyFrame.setVisible(true);
    You will not necessarily have to write the applets default constructor.

  • Are the Frame or JFrame objects not supported when used by applets??????

    Hi,
    Is there anyone to help me ?
    I've an applet which uses Frame objects. With the "appletViewer" executable the results are
    perfect.
    But Netsacape Communicator seems not to support Frames..?!? Is this possible?????
    Thanks
    deniz

    Oh..Thanks for the replies.
    But the reason was that I was still using Netscape Communicator 4.5.
    With Netscape Communicator 6.1 installed yesterday; everything is
    normal. i.e. I can observe the frames that applet creates... :-))

  • Using getter/setter for returing a string variable to display on an Applet

    have two classes called, class A and class testA.
    class A contains an instance variable called title and one getter & setter method. class A as follow.
    public class A extends Applet implements Runnable, KeyListener
         //Use setter and getter of the instance variable
         private String title;
         public void init()
              ASpriteFactory spriteFactory = ASpriteFactory.getSingleton();
              // Find the size of the screen .
              Dimension theDimension = getSize();
              width = theDimension.width;
              height = theDimension.height;
              //Create new ship
              ship = spriteFactory.createNewShip();
              fwdThruster = spriteFactory.createForwardThruster();
              revThruster = spriteFactory.createReverseThruster();
              ufo = spriteFactory.createUfo();
              missile = spriteFactory.createMissile();
              generateStars();
              generatePhotons();
              generateAsteroids();
              generateExplosions();
              initializeFonts();
              initializeGameData();
              //Example from Instructor
              //setMyControlPanel( new MyControlPanel(this) );
              // new for JDK 1.2.2
              addKeyListener(this);
              requestFocus();
         public void update(Graphics theGraphics)
              // Create the offscreen graphics context, if no good one exists.
              if (offGraphics == null || width != offDimension.width || height != offDimension.height)
                   // This better be the same as when the game was started
                   offDimension = getSize();
                   offImage = createImage(offDimension.width, offDimension.height);
                   offGraphics = offImage.getGraphics();
                   offGraphics.setFont(font);
              displayStars();
              displayPhotons();
              displayMissile();
              displayAsteroids();
              displayUfo();
              //displayShip();
              //Load the game with different color of the space ship          
              displayNewShip();
              displayExplosions();
              displayStatus();
              displayInfoScreen();
              // Copy the off screen buffer to the screen.
              theGraphics.drawImage(offImage, 0, 0, this);
         private void displayInfoScreen()
              String message;
              if (!playing)
                   offGraphics.setColor(Color.white);
                   offGraphics.drawString("\'A\' to Change Font Attribute", 25, 35);
                   offGraphics.drawString(getTitle(), (width - fontMetrics.stringWidth(message)) / 2, height / 2
                             - fontHeight);
                   message = "The Training Mission";
                   offGraphics.drawString(message, (width - fontMetrics.stringWidth(message)) / 2, height / 2);
                   message = "Name of Author";
                   offGraphics.drawString(message, (width - fontMetrics.stringWidth(message)) / 2, height / 2
                             + fontHeight);
                   message = "Original Copyright 1998-1999 by Mike Hall";
                   offGraphics.drawString(message, (width - fontMetrics.stringWidth(message)) / 2, height / 2
                             + (fontHeight * 2));
                   if (!loaded)
                        message = "Loading sounds...";
                        int barWidth = 4 * fontWidth + fontMetrics.stringWidth(message);
                        int barHeight = fontHeight;
                        int startX = (width - barWidth) / 2;
                        int startY = 3 * height / 4 - fontMetrics.getMaxAscent();
                        offGraphics.setColor(Color.black);
                        offGraphics.fillRect(startX, startY, barWidth, barHeight);
                        offGraphics.setColor(Color.gray);
                        if (clipTotal > 0)
                             offGraphics.fillRect(startX, startY, (barWidth * clipsLoaded / clipTotal), barHeight);
                        offGraphics.setColor(Color.white);
                        offGraphics.drawRect(startX, startY, barWidth, barHeight);
                        offGraphics
                                  .drawString(message, startX + 2 * fontWidth, startY + fontMetrics.getMaxAscent());
                   else
                        message = "Game Over";
                        offGraphics.drawString(message, (width - fontMetrics.stringWidth(message)) / 2, height / 4);
                        message = "'S' to Start";
                        offGraphics.drawString(message, (width - fontMetrics.stringWidth(message)) / 2, height / 4
                                  + fontHeight);
              else if (paused)
                   offGraphics.setColor(Color.white);
                   message = "Game Paused";
                   offGraphics.drawString(message, (width - fontMetrics.stringWidth(message)) / 2, height / 4);
         public String getTitle() {
              System.out.print(title);
              return title;
         public void setTitle(String title) {
              this.title = title;
    }displayInfoScreen method in class A calls out for getTitle( ) to be displayed on an applet as an initial display string for the application.
    The instance variable title is set by setTitle method which is called out in class testA as follow,
    public class testA extends TestCase
          * testASprite constructor comment.
          * @param name
          *          java.lang.String
         public testA(String name)
              super(name);
          * Insert the method's description here.
          * @param args
          *          java.lang.String[]
         public static void main(String[] args)
              junit.textui.TestRunner.run(suite());
              // need to figure out how to get rid of the frame in this test
              System.exit(0);
         public static Test suite()
              return new TestSuite(testA.class);
          * Basic create and simple checks
         public void testCreate()
              A theGame = new A();
              assertNotNull("game was null!", theGame);
          * Basic create and simple checks
         public void testInit()
              A theGame = new A();
              Frame gameFrame = new Frame("THE GAME");
              gameFrame.add(theGame);
              int width = 640;
              int height = 480;
              gameFrame.setSize(width, height);
              // must pack to get graphics peer
              gameFrame.pack();
              theGame.resize(width, height);
              theGame.setTitle("TEST THE GAME");
              theGame.init();
              assertEquals("ASprite width not set", A.width, width);
              gameFrame.dispose();
              gameFrame.remove(theGame);
    }Basically, class testA invokes the init( ) method in class A and start the applet application. However, it displays a white blank display. If I change the getTitle( ) in the displayInfoScreen method to a fixed string, it works fine. Did I forget anything as far as using getter & setter method? Do I have to specify some type of handle to sync between setter and getter between two classes? Any feedback will be greatly appreciated.
    Thanks.

    Your class A extends runnable which leads me to believe that this is a multi-threaded application. In that case, title may or may not be a shared variable. Who knows? It's impossible to tell from what you posted.
    Anyway, what is happening is that your applet is being painted by the JFrame before setTitle is called. After that, who knows what's happening. It's a complicated application. I suspect that if you called setTitle before you added the applet to the frame, it would work.

Maybe you are looking for

  • Quote Report - Performance

    I have created a quote report with narratives around the opportunity, opportunity-product, and account information. The report contains a pivot table. I'm getting very hit or miss performance results on the report. Sometimes the report runs faster fo

  • Can't get iTunes to close when using my iPod

    I have the original Touch, works just fine so why replace it.  However, lately when I download from iTunes, sync the new songs and then click the button to disconnect, I can't get itunes to close.  It keeps opening and finally in dispair, I resort to

  • Can i revert to snow leopard version of ical?

    I much preferred the way all my calendars had different coloured text to easily distinguish between them. Also you could see up to three lines of text per notice. Not imppressed with Lion at all

  • Single PR during MRP

    Hello Gurus, My client is having a unique requirement. He has got 1000 raw materials in a BOM. Whenever he runs MRP he gets 1000 PR's for 1000 materials. His requirement is to have a single PR with 1000 line items. The reason behind this is the relea

  • How can I turn off the safari

    Hi, I have no problems to turn down the Safari, but after I have been on a travelmarket site, I can turn of my computer. Any one who had same kind of a problem?