Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Tools / October 2006

Tip: Looking for answers? Try searching our database.

Help needed with NetBeans IDE so sample code will work please?

Thread view: 
M - 19 Oct 2006 08:20 GMT
Hi Folks,

Below is some sample code for a Java Applet. If I manually compile it using
the command line interpreter & fire it off using an HTML document it works
fine. However if I create a project in NetBeans Right Click on the
ShowDocument.java file and click Run File, I get a message in the output
window "createGUI didn't successfully complete".

Can anyone please tell me exactly what I need to do to get this (and other)
sample code to actually work inside the NetBeans IDE.

Regards,

M

/*
* ShowDocument.java
*
* Created on 19 October 2006, 07:42
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package org.me.ShowDocument;

import java.applet.AppletContext;
import javax.swing.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;

public class ShowDocument extends JApplet
                         implements ActionListener {
   URLWindow urlWindow;

   public void init() {
       //Execute a job on the event-dispatching thread:
       //creating this applet's GUI.
       try {
           SwingUtilities.invokeAndWait(new Runnable() {
               public void run() {
                   createGUI();
               }
           });
       } catch (Exception e) {
           System.err.println("createGUI didn't successfully complete");
       }
   }

   private void createGUI() {
       JButton button = new JButton("Bring up URL window");
       button.addActionListener(this);
       add(button);

       JFrame.setDefaultLookAndFeelDecorated(true);
       urlWindow = new URLWindow(getAppletContext());
       urlWindow.pack();
   }

   public void destroy() {
       //Execute a job on the event-dispatching thread:
       //creating this applet's GUI.
       try {
           SwingUtilities.invokeAndWait(new Runnable() {
               public void run() {
                   destroyGUI();
               }
           });
       } catch (Exception e) { }
   }
   
   private void destroyGUI() {
       urlWindow.setVisible(false);
       urlWindow = null;
   }

   public void actionPerformed(ActionEvent event) {
       urlWindow.setVisible(true);
   }
}

class URLWindow extends JFrame
                       implements ActionListener {
   JTextField urlField;
   JComboBox choice;
   AppletContext appletContext;

   public URLWindow(AppletContext appletContext) {
       super("Show a Document!");
       this.appletContext = appletContext;

       JPanel contentPane = new JPanel(new GridBagLayout());
       setContentPane(contentPane);
       contentPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
       GridBagConstraints c = new GridBagConstraints();
       c.fill = GridBagConstraints.HORIZONTAL;

       JLabel label1 = new JLabel("URL of document to show: ",
                                  JLabel.TRAILING);
       add(label1, c);

       urlField = new JTextField("http://java.sun.com/", 20);
       label1.setLabelFor(urlField);
       urlField.addActionListener(this);
       c.gridwidth = GridBagConstraints.REMAINDER;
       c.weightx = 1.0;
       add(urlField, c);

       JLabel label2 = new JLabel("Window/frame to show it in: ",
                                  JLabel.TRAILING);
       c.gridwidth = 1;
       c.weightx = 0.0;
       add(label2, c);

       String[] strings = {
           "(browser's choice)", //don't specify
           "My Personal Window", //a window named "My Personal Window"
           "_blank",             //a new, unnamed window
           "_self",
           "_parent",
           "_top"                //the Frame that contained this applet
       };
       choice = new JComboBox(strings);
       label2.setLabelFor(choice);
       c.fill = GridBagConstraints.NONE;
       c.gridwidth = GridBagConstraints.REMAINDER;
       c.insets = new Insets(5,0,0,0);
       c.anchor = GridBagConstraints.LINE_START;
       add(choice, c);

       JButton button = new JButton("Show document");
       button.addActionListener(this);
       c.weighty = 1.0;
       c.ipadx = 10;
       c.ipady = 10;
       c.insets = new Insets(10,0,0,0);
       c.anchor = GridBagConstraints.PAGE_END;
       add(button, c);
   }
   
   public void actionPerformed(ActionEvent event) {
       String urlString = urlField.getText();
       URL url = null;
       try {
           url = new URL(urlString);
       } catch (MalformedURLException e) {
           System.err.println("Malformed URL: " + urlString);
       }

       if (url != null) {
           if (choice.getSelectedIndex() == 0) {
               appletContext.showDocument(url);
           } else {
               appletContext.showDocument(url,
                                 (String)choice.getSelectedItem());
           }
       }
   }
}
Kroll, Michael - 19 Oct 2006 13:15 GMT
Hi,

do you a html-file in NetBeans too ??

Regards
Michael

Signature

http://www.netbeans-forum.de
German NetBeans, GlassFish and Java Forum

M - 19 Oct 2006 17:21 GMT
> Hi,
>
> do you a html-file in NetBeans too ??
>
> Regards
> Michael

Yep

Signature

Regards,

M
http://za1012001.googlepages.com/home

Lion-O - 26 Oct 2006 13:23 GMT
> However if I create a project in NetBeans Right Click on the
> ShowDocument.java file and click Run File, I get a message in the
> output window "createGUI didn't successfully complete".

Did you put the entire piece of code in a single file or... ?  Because
if you didn't split it up then I can imagine that it doesn't run.

Which NB version do you use?

Signature

Groetjes, Peter

.\\ PGP/GPG key: http://www.catslair.org/pubkey.asc

Lion-O - 26 Oct 2006 13:41 GMT
> Did you put the entire piece of code in a single file or... ?  Because
> if you didn't split it up then I can imagine that it doesn't run.

...Still, I can't help wonder how you created the project. Did you
manually create a new package and 1 (or 2) new class files after which
you copied/pasted this code into it ?

Because thats exactly what I did (I always keep a dummy project around
in which I keep lots of test cases) and it simply worked. First I
created the package and 2 class files manually, with a second test I
simply created a class file in another package and only changed the
package name in your source and both tests work.

So I wonder if its either your NetBeans version or the way in which you
set this all up.

Signature

Groetjes, Peter

.\\ PGP/GPG key: http://www.catslair.org/pubkey.asc



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.