Results 1 to 9 of 9

Thread: New to Applets

  1. #1
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default New to Applets

    I'm trying to get a simple applet to work. When I run some of the tutorials that create buttons, the buttons don't appear until I mouse-over them.

    Also, I tried adding a sub-panel and I can't seem to get it to work properly. The sub-panel works fine in the fact that it contains the buttons (which don't show up until mouse-over) and seems to be the correct position and size that I set via setBounds(), but I can't get the background to show.

    So I have 2 questions...

    Why don't the buttons show until mouse-over?

    Why doesn't the background show from my sub-panel?

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class HelloWorld extends JApplet {
    
        public void init() {
    	resize(200,200);
            
            setLayout(null);
    
            JPanel panel = new JPanel();
            add(panel);
            panel.setOpaque(true);
            panel.setBackground(Color.red);
            panel.setVisible(true);
            panel.setBounds(20, 20, 120, 120);
    
            JButton button1 = new JButton("1");
            button1.setVisible(true);
            panel.add(button1);
    
            JButton button2 = new JButton("2");
            panel.add(button2);
        }
        public void paint(Graphics g) {
    	g.drawString("Hello world!", 50, 150);
        }
    
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    When I run some of the tutorials that create buttons, the buttons don't appear until I mouse-over them.
    No idea what tutorial you are looking at. I suggest you look at the Swing tutorial which contains an applet example.

    I also see you are still using an null layout even though you have been advised against it.

  3. #3
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    No idea what tutorial you are looking at. I suggest you look at the Swing tutorial which contains an applet example.
    This is the swing tutorial. It's Hello World with buttons and a panel added. I ran another tutorial with buttons that had the same issue of the buttons not appearing until mouse-over, so I decided to try it in Hello World since it is a simple applet that I already had working.

    I also see you are still using an null layout even though you have been advised against it.
    Yes, I was warned that it wasn't the preferred layout. I tried several layouts including Box, Border, and Flow. None of these seemed appropriate for what I'm doing. GridBag seems to be the layout that I should be using from what I've read and understand. I was also warned that it was complicated, so I haven't really tried it... yet.

    Null layout actually works well for me right now. It lets me put components exactly where I want them without moving them around. At this point in time, this is all I need. I will eventually switch to GridBag or something else, but 1 step at a time.

    The example code I posted was originally in FlowLayout and it didn't work, so I tried a couple different layouts. Null layout was the last one I tried and happened to post.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Does the applet tutorial really instruct you to draw directly in the applet itself via its paint method? I find this very hard to believe. What is the link to this tutorial?

    Myself, I'd draw in a JPanel that I placed in the applet. I second the recommendation not to use null layout, that it's a very bad habit to get into.

    e.g.,
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class HelloWorld2 extends JApplet {
      public void init() {
        try {
          javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
              createGUI();
            }
          });
        } catch (Exception e) {
          System.err.println("createGUI didn't successfully complete");
        }
      }
    
      private void createGUI() {
        HelloWorldPanel helloWorldPanel = new HelloWorldPanel();
        helloWorldPanel.setBackground(Color.red);
        JButton button1 = new JButton("1");
        JButton button2 = new JButton("2");
        helloWorldPanel.add(button1);
        helloWorldPanel.add(button2);
        
        getContentPane().add(helloWorldPanel);
      }
    }
    
    @SuppressWarnings("serial")
    class HelloWorldPanel extends JPanel {
      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString("Hello world!", 50, 150);
      }
    }
    Last edited by Fubarable; 02-27-2010 at 11:00 PM.

  5. #5
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    Just to clarify, I absolutely 100% WILL NOT be using null layout. I just haven't got around to learning a lot of the other layouts. I felt like there are still some more basics that I need to learn before worrying about layout managers.

    I'm very comfortable with Flow, Border, and Box layouts as well.

  6. #6
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    Here is one of the tutorials I was using. I did so many of them I'll have to search for the buttons one that was an issue.

    Essentials, Part 1, Lesson 3: Building Applets

    Java Code:
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Color;
    
    public class SimpleApplet extends Applet{
    
      String text = "I'm a simple applet";
    
      public void init() {
            text = "I'm a simple applet";
            setBackground(Color.cyan);
      }
      public void start() {
            System.out.println("starting...");
      }
      public void stop() {
            System.out.println("stopping...");
      }
      public void destroy() {
            System.out.println("preparing to unload...");
      }
      public void paint(Graphics g){
            System.out.println("Paint");
            g.setColor(Color.blue);
            g.drawRect(0, 0,
                       getSize().width -1,
                       getSize().height -1);
            g.setColor(Color.red);
            g.drawString(text, 15, 25);
      }
    }

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Here is one of the tutorials I was using
    That is NOT part of the Swing tutorial. You've been given the link before in one of your other postings. Or you can read the API for any Swing component which also contains a link to the tutorial.

    That tutorial if for an AWT Applet, which in not the same as a Swing JApplet. There is a big difference. You can't just randomly change the class you extend and expect it to work.

  8. #8
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    Here is one of the applets from Getting Started With Applets (The Java™ Tutorials > Deployment > Applets).

    Java Code:
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    import javax.swing.JLabel;
    
    public class HelloWorld extends JApplet {
        //Called when this applet is loaded into the browser.
        public void init() {
            //Execute a job on the event-dispatching thread; creating this applet's GUI.
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        JLabel lbl = new JLabel("Hello World");
                        add(lbl);
                    }
                });
            } catch (Exception e) {
                System.err.println("createGUI didn't complete successfully");
            }
        }
    }
    Three pages later in the tutorial, they use:

    Java Code:
    import java.applet.Applet;
    import java.awt.Graphics;
    
    //No need to extend JApplet, since we don't add any components;
    //we just paint.
    public class Simple extends Applet {
    
        StringBuffer buffer;
    
        public void init() {
            buffer = new StringBuffer();
            addItem("initializing... ");
        }
    
        public void start() {
            addItem("starting... ");
        }
    
        public void stop() {
            addItem("stopping... ");
        }
    
        public void destroy() {
            addItem("preparing for unloading...");
        }
    
        private void addItem(String newWord) {
            System.out.println(newWord);
            buffer.append(newWord);
            repaint();
        }
    
        public void paint(Graphics g) {
    	//Draw a Rectangle around the applet's display area.
            g.drawRect(0, 0, 
    		   getWidth() - 1,
    		   getHeight() - 1);
    
    	//Draw the current string inside the rectangle.
            g.drawString(buffer.toString(), 5, 15);
        }
    }
    I've spent almost 3 days now reading tutorials, trying codes, etc. Sorry if I'm frustrating you, but I'm guess I'm just stupid for not picking it up faster.

    I don't post on here unless I spend a lot of time researching and am still stuck or can't find the answers. All I'm looking for is to be pointed in the right direction.

    I was attempting to modify one of the tutorial Applets. Some are Swing and some aren't.

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I was attempting to modify one of the tutorial Applets. Some are Swing and some aren't.
    So stick with the Swing examples so you don't confuse yourself. I would think that would be obvious advice.

    The Swing example shows how to add a label. So I fail to see why you would not use that example to add a label and a button. Basically it should be a two line code change to the existing program. First you create the button, then you add the button to the content pane of the applet. That would be the obvious approach to me but maybe I think differently then most people.

    That fact that you keep jumping around from using layout managers to using a null layout is also causing problems. Therefore every example code you write is testing 2 completely different concepts and you don't know which concept you are trying to learn is causing the problem. So again master the simple concept of adding components to a panel. Then the code for adding the panel to a JFrame or JApplication is identical. So if your applet doesn't work you know the problem is not with the panel but with something else.
    Last edited by camickr; 02-28-2010 at 05:04 AM.

Similar Threads

  1. Where do applets print to?
    By IamKira in forum Java Applets
    Replies: 0
    Last Post: 01-30-2010, 09:47 AM
  2. I need help with Applets
    By ProgrammingPup in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2009, 09:07 PM
  3. Can't see any Applets
    By pyr0chem in forum Java Applets
    Replies: 9
    Last Post: 12-17-2008, 05:24 AM
  4. applets on mac what´s going on?
    By willemjav in forum Java Applets
    Replies: 6
    Last Post: 04-19-2008, 08:03 PM
  5. Applets and dll
    By peiceonly in forum Java Applets
    Replies: 1
    Last Post: 04-01-2007, 11:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •