0

I've been working at this problem for hours now, with no results. I cannot seem to get the applet to display properly when viewing the HTML page OR when running the applet through Eclipse. It is always blank. I've been searching for a long time now and decided just to ask. Here's the code:

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

public class Topics extends Applet{
    String topicTotal = "";
    JPanel topicPanel;
    JLabel title, username, topic, paragraph, topicsTitle;
    JTextField nameField, topicField;
    JButton submitButton;
    JTextArea paragraphArea;

    public String getTopicTotal() {
        return topicTotal;
    }


    public JPanel createContentPane(){
        final JPanel topicGUI = new JPanel();
        topicGUI.setLayout(null);

        //posting panel
        final JPanel postPanel = new JPanel();
        postPanel.setLayout(null);
        postPanel.setLocation(0, 0);
        postPanel.setSize(500, 270);
        topicGUI.add(postPanel);

        setVisible(true);

        // JLabels

        JLabel title = new JLabel("Make A Post");
        title.setLocation(170, 3);
        title.setSize(150,25);
        title.setFont(new Font("Serif", Font.PLAIN, 25));
        title.setHorizontalAlignment(0);
        postPanel.add(title);

        JLabel username = new JLabel("Username: ");
        username.setLocation(20, 30);
        username.setSize(70,15);
        username.setHorizontalAlignment(0);
        postPanel.add(username);

        JLabel topic = new JLabel("Topic: ");
        topic.setLocation(20, 50);
        topic.setSize(40,15);
        topic.setHorizontalAlignment(0);
        postPanel.add(topic);

        JLabel paragraph = new JLabel("Paragraph: ");
        paragraph.setLocation(20, 70);
        paragraph.setSize(70,15);
        paragraph.setHorizontalAlignment(0);
        postPanel.add(paragraph);

        // JTextFields

        nameField = new JTextField(8);
        nameField.setLocation(90, 30);
        nameField.setSize(150, 18);
        postPanel.add(nameField);

        topicField = new JTextField(8);
        topicField.setLocation(60, 50);
        topicField.setSize(180, 18);
        postPanel.add(topicField);

        // JTextAreas

        paragraphArea = new JTextArea(8, 5);
        paragraphArea.setLocation(20, 85);
        paragraphArea.setSize(450, 100);
        paragraphArea.setLineWrap(true);
        paragraphArea.setEditable(true);
        JScrollPane scrollParagraph = new JScrollPane (paragraphArea);
        scrollParagraph.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        postPanel.add(paragraphArea);

        // JButton

        JButton submitButton = new JButton("SUBMIT");
        submitButton.setLocation(250, 30);
        submitButton.setSize(100, 30);
        submitButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {

                topicTotal = topicTotal + "/n" + nameField + "/n" + topicTotal + "/n" + paragraphArea + "/n";
        }
        });
        postPanel.add(submitButton);

        setVisible(true);

            return topicGUI;
        }

        private static void createAndShowGUI() {


            JFrame frame = new JFrame("");

            Topics display = new Topics();
            frame.setContentPane(display.createContentPane());
            frame.setSize(500, 270);
            frame.setVisible(true);

        }

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
       }

}
royhowie
  • 11,075
  • 14
  • 50
  • 67
Dimfish
  • 3
  • 5
  • It works perfectly fine for me running on netbeans – Paul Samsotha Nov 24 '13 at 16:25
  • was that running it as an application or an applet? Because mine runs fine as an application, but is blank when ran as an applet. – Dimfish Nov 24 '13 at 16:29
  • How are you attaching the applet to you HTML and do you have Java Enabled on the browser? – Paul Samsotha Nov 24 '13 at 16:32
  • Java is fully enabled, and here is how I have it attached: – Dimfish Nov 24 '13 at 16:36
  • In order to run as an applet, you need to override the `init()` method. I posted an answer below. I didn't get a chace to format it correctly, but it runs. You don't need the `main` running as an applet. Just format it the way you want. But you can run the program as an applet. – Paul Samsotha Nov 24 '13 at 16:53
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Nov 24 '13 at 16:58

1 Answers1

0

In order to run as an applet, you need to override the init method. You don't need a main method. Just add all you components inside the init method

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

public class Topics extends Applet {
String topicTotal = "";
JPanel topicPanel;
JLabel title, username, topic, paragraph, topicsTitle;
JTextField nameField, topicField;
JButton submitButton;
JTextArea paragraphArea;

public String getTopicTotal() {
    return topicTotal;
}

public void init() {
    final JPanel topicGUI = new JPanel();
    topicGUI.setLayout(null);

    // posting panel
    final JPanel postPanel = new JPanel();
    postPanel.setLayout(null);
    postPanel.setLocation(0, 0);
    postPanel.setSize(500, 270);
    add(postPanel);

    setVisible(true);

    // JLabels

    JLabel title = new JLabel("Make A Post");
    title.setLocation(170, 3);
    title.setSize(150, 25);
    title.setFont(new Font("Serif", Font.PLAIN, 25));
    title.setHorizontalAlignment(0);
    add(title);

    JLabel username = new JLabel("Username: ");
    username.setLocation(20, 30);
    username.setSize(70, 15);
    username.setHorizontalAlignment(0);
    add(username);

    JLabel topic = new JLabel("Topic: ");
    topic.setLocation(20, 50);
    topic.setSize(40, 15);
    topic.setHorizontalAlignment(0);
    add(topic);

    JLabel paragraph = new JLabel("Paragraph: ");
    paragraph.setLocation(20, 70);
    paragraph.setSize(70, 15);
    paragraph.setHorizontalAlignment(0);
    add(paragraph);

    // JTextFields

    nameField = new JTextField(8);
    nameField.setLocation(90, 30);
    nameField.setSize(150, 18);
    add(nameField);

    topicField = new JTextField(8);
    topicField.setLocation(60, 50);
    topicField.setSize(180, 18);
    add(topicField);

    // JTextAreas

    paragraphArea = new JTextArea(8, 5);
    paragraphArea.setLocation(20, 85);
    paragraphArea.setSize(450, 100);
    paragraphArea.setLineWrap(true);
    paragraphArea.setEditable(true);
    JScrollPane scrollParagraph = new JScrollPane(paragraphArea);
    scrollParagraph
            .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    add(paragraphArea);

    // JButton

    JButton submitButton = new JButton("SUBMIT");
    submitButton.setLocation(250, 30);
    submitButton.setSize(100, 30);
    submitButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            topicTotal = topicTotal + "/n" + nameField + "/n" + topicTotal
                    + "/n" + paragraphArea + "/n";
        }
    });
    add(submitButton);
}

}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720