Swing applet- please help

Hi,
I am still trying to make sense of the java code...
I thought it should be easy to create an applet, but I don't know what is wrong.
Could anyone help me debugging it?
Here is the code and the error msg:
package BrImage;
/* Example 16-3 An Image Previewer Accessory implements BrImagFile*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.applet.Applet;
public class BrImage extends JApplet {
public void init() {
getContentPane().add(new JLabel("JAppletSwing!"));
public class BrImagFile extends javax.swing.JFrame {
JFileChooser chooser = new JFileChooser();
ImagePreviewer previewer = new ImagePreviewer();
PreviewPanel previewPanel = new PreviewPanel();
class PreviewPanel extends javax.swing.JPanel {
public PreviewPanel() {
JLabel label = new JLabel("Image Previewer",SwingConstants.CENTER);
setPreferredSize(new Dimension(150,0));
setBorder(BorderFactory.createEtchedBorder());
setLayout(new BorderLayout());
label.setBorder(BorderFactory.createEtchedBorder());
add(label, BorderLayout.NORTH);
add(previewer, BorderLayout.CENTER);
public BrImagFile() {
super("Image Previewer");
Container contentPane = getContentPane();
JButton button = new JButton("Select A File");
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
chooser.setAccessory(previewPanel);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int state = chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
String s = "CANCELED";
if(file != null && state == JFileChooser.APPROVE_OPTION) {
s = "File Selected: " + file.getPath();
/* needs to open file on the right html */
JOptionPane.showMessageDialog(null, s);
chooser.setFileFilter(new ImageFilter()); /*test filter */
chooser.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
if(e.getPropertyName().equals(
JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
File f = (File)e.getNewValue();
String s = f.getPath(), suffix = null;
int i = s.lastIndexOf('.');
if(i > 0 && i < s.length() - 1)
suffix = s.substring(i+1).toLowerCase();
if(suffix.equals("gif") ||
suffix.equals("jpg") ||
suffix.equals("bmp"))
previewer.configure(f);
class ImagePreviewer extends javax.swing.JLabel {
public void configure(File f) {
Dimension size = getSize();
Insets insets = getInsets();
ImageIcon icon = new ImageIcon(f.getPath());
setIcon(new ImageIcon(icon.getImage().getScaledInstance(
size.width - insets.left - insets.right,
size.height - insets.top - insets.bottom,
Image.SCALE_SMOOTH)));
class ImageFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File f) {
boolean accept = f.isDirectory();
if( ! accept) {
String suffix = getSuffix(f);
if(suffix != null)
accept = suffix.equals("jpg") ||
suffix.equals("gif") ||
suffix.equals("bmp");
return accept;
public String getDescription() {
return "Image Files(*.gif *.jpg *.bmp)";
private String getSuffix(File f) {
String s = f.getPath(), suffix = null;
int i = s.lastIndexOf('.');
if(i > 0 && i < s.length() - 1)
suffix = s.substring(i+1).toLowerCase();
return suffix;
public static void main(String a[]) {
JApplet applet = new BrImage();
JFrame f = new BrImagFile();
f.setBounds(300, 300, 300, 75);
applet.init();
applet.start();
f.setVisible(true);
/* f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE); */
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
BrImage/BrImage.java [109:1] non-static variable this cannot be referenced from a static context
JFrame f = new BrImagFile();
^
1 error
Errors compiling BrImage.

Hi again,
Please help.
JL

Similar Messages

  • Cannot Import javax.swing.JOptionPane   Please HELP!!!

    import javax.swing.JOptionPane;
    this line of code returns the error:
    C:\Java Files\BankAccount\BankAccount_Test.java:1: Class javax.swing.JOptionPane not found in import.
    import javax.swing.JOptionPane;
    ^
    1 error
    Process completed.
    Please help, I don't know what the problem could be....

    Swing was not part of any JDK's earlier than 1.2. Swing (or anything with a J in front of it, ie. JFrame, JOptionPane, etc...) was probably the most dramatic (if not largest) modification/addition to the Java language, that's why versions later than, and including, 1.2 are known as "Java 2".

  • Problem to debug applet, please help...

    i use windows.
    there are 3 files under "c:\current" directory, they are
    file 1. mem_appletviewer.bat - with content:
    "e:\install\java\jdk1.3.1_05\bin\appletviewer" -J-Xdebug -J-Xrunjdwp:transport=dt_shmem,address=conn001122,server=y,suspend=y test.htm
    file 2. mem_jdb.bat - with content:
    "e:\install\java\jdk1.3.1_05\bin\jdb" -attach conn001122
    file 3. test.htm - with applet tag:
    <applet
    code="Let0.class"
    codebase="G:\Victor\Vc5060\Develop\JCity\files\RecentFile"
    width=400 height=250 ></applet>
    (Let0.class is a simplest class extends Applet)
    please note, codebase is not "c:\current" directory
    problems happend when i debug: (run mem_appletviewer.bat first then mem_jdb.bat)
    a) if move Let0.class to "c:\current" directory, debug is fine but test.htm can not load Let0.class itself because of codebase tag.
    b) if move Let0.class to codebase directory (G:\Victor\Vc5060\Develop\JCity\files\RecentFile), test.htm can load the class but appletviewer can not load the class (can not find Let0.class).
    ===========================================================
    in my application, i need to put Let0.class in codebase directory.
    Question:
    1) how to set -classpath for appletviewer (as debug application class in java.exe )? is it possible?
    2) codebase TAG in test.htm doesn't work for appletviewer, what is problem? how to fix it?
    totally:
    how to debug Let0.class if it is in codebase directory (not in "c:\current")?
    thx

    i fix it.

  • I want to create an applet, Please Help...

    HI all,
    I want to create an applet which should be able to display text and images...
    To display text in an applet I am using ...
    import java.awt.*;
    import java.applet.*;
    public class SimpleApplet extends Applet
        public void paint(Graphics g)
            g.drawString("A Simple Applet", 20, 20);
    }Now If I want to create few operators in a different class, and i want to use it from the SimpleApplet class what should be doing.

    HI all,
    I am creating an Gui: The code is here:
    import javax.swing.*;
    import java.awt.*;
    public class Tab extends JFrame
        JPanel p,p1,p2,p3,p4,p5;
        Frame f1,f2,f3;
        JTabbedPane tpane;
        public Tab()
            p = new JPanel();
            p1 = new JPanel();
            p2 = new JPanel();
            p3 = new JPanel();
            p4 = new JPanel();
            p5 = new JPanel();
            tpane = new JTabbedPane();
            p.setLayout(new GridLayout(1,1));
            tpane.addTab("File",p1);
            tpane.addTab("Edit",p2);
            tpane.addTab("Document",p3);
            tpane.addTab("View",p4);
            tpane.addTab("Help",p5);
            p.add(tpane);
            getContentPane().add(p);
            setVisible(true);
        public static void main(String[] args)
            // TODO Auto-generated method stub
            Tab t = new Tab();
    }I want to diaplay some text in the main area left. What should I do.
    How to add the Text.

  • Error when trying to sign the Applet, please Help

    Hello,
    I have this Error when I am trying to sign my applet, after execut this command :
    jarsigner -verify -verbose -certs test.jar
    jarsigner: java.lang.SecurityException: invalid SHA1 signature file digest for classe/JDBC_SQL.class
    And for your information, that I had add the JDBC_SQL.class to the jar file (test.jar) because I made some change to the file JDBC_SQL.java
    so please if you know what can I do to resolve my problem
    Thank you to reponse !
    rania +

    ahh, here is a problem. u made changes to the JDBC_SQL.java and packed the jar file. u have to sign the jar file again.
    let me know if u need futher assistance

  • Comunication between applets (please help)

    I have an applet that connects to a servlet, which connects to a database. Signing the applet made it possible to access it from client machines.
    However, there is another applet (in the same html page), which receives a message from the first applet (the one that connects to the servlet). This applet only receives a vector of objects from the first applet, and is triggered by a message send.
    The first applet is working fine. But when I click the button that orders the second applet to paint himself, nothing happens (the cursor remains in waiting state, and the second applet is never repainted).
    If you didn't understand, please let me know, so that I'll try to explain better.
    Thanks!

    Sorry, I forgot to mention that the second applet (the one that receives the message from the first one) isn't signed.
    I didn't do it because it doesn't make any connections or unsecure actions (except for receiving a message, but is this unsecure?).
    Can someone tell me if that is the problem?

  • APPLETS PLEASE HELP !!!!!!

    Does anyone know how to close an applet dynamically from within its own code.
    Have tried via javascript but the applet I created using the object tag fails to communicate correctly. Says 'Object does not support this operation'.
    If anyone can help I would be grateful as this is a major issue now.
    Thanks Andrew Mercer

    Thankyou very much for your help.
    I can now use java code from an applet to close the window that surrounds this applet.
    However I still have one problem that I cannot solve by looking through the documentation.
    My applet basically is used to copy files from server to client cache. Once each file is fully copied over then the applet has done its job and can be closed.
    The main Browser window stays open all this time and each applet window is opened and file transfered by hitting a link on this window from a list of links that can run into 100's.
    The first time this operation is requested the JSObject.getWindow(this) call returns a valid reference to the applet within the window.
    Once the first window is closed the next JSOBject.getWindow(this) fails, in fact it goes no further and does not return an exception or suitable message.
    This happens even if I have closed the window manually.
    How can I use the getWindow(this) call for subsequent windows?
    Depending where I put the getWindow(this) I can actually make the applet init operation stall and go no further.
    Thanks again.
    Andrew Mercer

  • Sending emails from inside an applet (Please help!)

    Hi all!
    I�d like to send an email from inside the applet. For this reason I need to get the Systemproperties, don't I ?
    Properties props = System.getProperties();
    props.put("mail.smtp.host", mailhost);
    ... and set the mail.smtp.host Property my smtp-host.
    But as I try to get the systemproperties from the applet, I get the exception:
    com.ms.security.SecurityExceptionEx[Sendmsg.<init>]: Unable to access system properties.
    Can somebody tell me, how I can solve this problem? Which properties are needed to send mails?
    Does it work, if I just create my Properties, and set only the smtp.host?
    As I continued my experiments, I realised that it is not only the mail.smtp.host property, that is needed by the applet, but java.home, and many more...
    So is it possible to send emails in this form at all??
    Pleeaaaase help, I have to hand in my project very soon!
    Thanks a lot,
    Gabor

    I've found a site about signing:
    http://www.suitable.com/Doc_CodeSigning.shtml
    I think you can read german, therefore the german newsgroup de.comp.lang.java is a good point to search. Use http://groups.google.com to search the group.
    Uwe

  • Random white screen when trying to run applet (Please help)

    We have been plagued by this problem for months. We are running a Java applet (that we wrote) requiring JRE 1.3.0_01, where the html file has been created with html converter. RANDOMLY, when loading the applet, the applet will come up (we spawn a new browser window for it) and we will get a blank white screen, AND netscape will freeze, needing to be terminated. Saying program is not responding when you click the X.
    This is not a multithreaded program.
    I have also tried the new JRE 1.4 with the same problems.
    The strange thing is, we have found some workarounds.... if you tell your plugin to show the java console all the time, it takes care of majority of the crashes, but the applet STILL does the same thing every once in awhile. If someone can tell me what the problem is here I will be eternally grateful!!
    Steve

    I had the same problem at one time during the development of my applets. Without more details from you, there is no way to know the exact cause but I can give you some ideas:
    1) painting problems
    2) null pointer problems
    3) improper setup of the HTML file (this was the culprit for us...double check EVERYTHING)

  • Saving data with applets please help

    Hello,
    I want to save some variables, so the user of the applet have his last configuration at the start of the applet.
    What can i use? Do i have to give the permission at the applet of writing and reading files on the user disc (how do i make that for ie, netscape or mozilla)
    Or it is possible to use cookies in my applet???
    Thanks a lot.

    Actually we had some luck using the server to store a cookie in the browser, in your case it could be size and position. Then when the web page with the applet is requested you can create the html page on the fly and include the values as applet parameters.
    Another approach is to have the server store the cookie information via a cgi-bin script or servlet, and have the applet ask for it via a cgi-bin script or servlet when it starts up.

  • Swing gurus, please help me

    Hi all,
    Hi I have created a gui with two panels named serverPanel and clientPanel.
    The serverPanel will be displayed when the serverMode is selected in the menu and viceversa.
    Now my question is that , can we set the initial focus to each of these panels?
    i.e the cursor should be there at some desired components at the selected panel.
    plz help me to sort this problem out.

    To expand on the above, you want to add a ComponentListener to the panels in question and then on the componentShown() event set the focus to the desired elemnt.
    myPanel.addComponentListener(new ComponenetAdapter()
    public void componentShown(ComponentEvent e)
    someItem.requestFocus();
    });

  • Please help...my browser can't display applets

    I know it might sound inept to be asking something about my mozilla browser in this forum but right now I am studying Java and using my browser to view the applet exercises I make. I am particularly in the methods section where you have to make recursions (I am using JAVA 2 how to program Third Edition by Deitel). I made my own version of the fibonacci program in the book . It has a GUI, action listener and the method where all the calculations take place. Unfortunately, when I test it on my brower, Mozilla Fireforx, it just displays the GUI and whole program does not work. The compiler, in this case javac, cannot sense anything wrong and I take it that my code is correct. However nothing still happens when I test it on my browser. As previously mentioned, it just displays the GUI and that's all. I really do not know what is wrong. Is it with my code or with the browser? Or is with the fact that I am using an outdated version of JAVA 2 how to program with a new version of J2SDK that makes my code incompatible with the new technology? please help. TY
    Here's my code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class AppletCode extends JApplet implements ActionListener {
         JLabel inputLabel, outputLabel;
         JTextField input, output;
         public void init(){
              Container c = getContentPane();
              c.setLayout(new FlowLayout());
              JLabel inputLabel = new JLabel();
              inputLabel.setText("Enter an Integer and press Enter");
              c.add(inputLabel);
              JTextField input = new JTextField(10);
              input.addActionListener(this);
              c.add(input);
              JLabel outputLabel = new JLabel();
              outputLabel.setText("Fibonacci Value is");
              c.add(outputLabel);
              JTextField output = new JTextField(20);
              output.setEditable(false);
              c.add(output);
         } // GUI
         public void actionPerformed(ActionEvent e) {
              long number, fibonacciValue;
              number = Long.parseLong(input.getText());
              fibonacciValue = fibonacci(number);
              output.setText(Long.toString(fibonacciValue));
         } // Action Listener
         public long fibonacci(long x) {
              if (x==0 || x==1)
                   return x;
              else
                   return fibonacci(x-1) + fibonacci(x-2);
         } // Fibonacci Module
    }

    I don't see anything obviously wrong with your code, but it's been a while since i coded applets, so there might still be something wrong. What exactly does not work? You see the 2 labels and the text fields? Can you enter any text into the text field? Have you looked into the Java Console wether it prints any exceptions? (and please use the [ code][ /code] tags to mark your code (without the spaces obviously))

  • Please help...Problem with 3D Applet using JRE 1.5

    Hi all,
    I have an applet which uses sun opengl 1.3.1 plugin to render some 3D graphics. It is working fine with JRE 1.4.2_06, but not showing the 3D graphics with the latest JRE 1.5 release. I tried to repaint using available API with no success.
    Please help asap,
    Thanks in advance
    prasad

    I wonder if you have the same problem as me... Maybe we can find a solution for us both, no need for me to open a new thread for this topic then...
    I wrote an applet using Swing and compiled it using JavaSDK 1.4.x. I also installed the 1.4.x JRE for both of my test browsers. In Mozilla 1.1 the applet displays properly all the time. In IE6 SP1 it sometimes works as intended but sometimes the applet simply stops working, as per my Java Console somewhere after invoking the "start" method. Parts of the applet simply become gray and when I resize it - I have a JFrame in my applet - the whole JFrame becomes gray and does not respond to input nor it redraws itself. Shutting down IE does not close the JFrame(although the java console reports normal program termination and cleanup) and the only way to close it is through the task manager. I am using Windows 98SE btw.
    Does it sound similiar to your problem? It happens ONLY with IE, not with Mozilla. Anybody has an idea on what it could be? I doubt there's an error in my code...

  • Please Help! Can't get my applet to work!!

    PLEASE HELP
    I have been trying to get an applet to work which when viewed in an applet viewer works fine.
    HOWEVER, when I try to view it through the browser it will not be viewed. The html code I've used is very simple and you can see that below . I've downloaded the JAVA plugin but that doesn't work either!
    The errors that come up say --- java.lang.ClassFormatError.Class already loaded
    Then if the window is expanded says --- java.lang.NoClassDefFoundError:javax/swing/JApplet
    I haven't a clue what's wrong!!
    HTML CODE
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
    <HTML>
    <HEAD><TITLE>Chess Game
    </TITLE>
    </HEAD>
    <BODY>
    <applet Code ="ChessClient20.class" Width =450 Height =555>
    </applet>
    </BODY>
    </html>
    Thanks for any help!!

    Thanks, have now found original mail. I think I've lost the plot. will check out those suggestions.

  • Converting application to applet.... please Help

    This program shown works fine in appletveiwer however will not work with I.E. nor Netscape. I do have the java plugins for both and using a netscape version over 6.0. I made the program usint jdk1.3. Can anyone please help me out?
    java code
    // TabbedPaneFlags.java: Use tabbed pane to select figures
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TabbedPaneFlags extends javax.swing.JApplet
    // Create a tabbed pane to hold figure panels
    private JTabbedPane jtpFigures = new JTabbedPane();
    // Load Images
    private JLabel lblUSA = new JLabel(new ImageIcon("USA.gif"));
    private JLabel lblUK = new JLabel(new ImageIcon("uk.gif"));
    private JLabel lblGER = new JLabel(new ImageIcon("germany.gif"));
    private JLabel lblCAN = new JLabel(new ImageIcon("canada.gif"));
    private JLabel lblCHI = new JLabel(new ImageIcon("china.gif"));
    private JLabel lblIND = new JLabel(new ImageIcon("india.gif"));
    // Main method
    //public static void main(String[] args)
    // TabbedPaneFlags frame = new TabbedPaneFlags();
    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // frame.setSize(450, 350);
    // frame.setVisible(true);
    // Constructor
    // public TabbedPaneFlags()
    public void init()
    // setTitle("Tabbed Pane Demo");
    jtpFigures.add(lblUSA,"USA");
    jtpFigures.add(lblUK,"UK");
    jtpFigures.add(lblGER,"Germany");
    jtpFigures.add(lblCAN,"Canada");
    jtpFigures.add(lblCHI,"China");
    jtpFigures.add(lblIND,"India");
    // Place tabbed pane
    this.getContentPane().add(jtpFigures, BorderLayout.CENTER);
    html code
    <html>
    <head>
    <title>AppletMenuDemo</title>
    </head>
    <body>
    <applet
    code = "TabbedPaneFlags.class"
    width = 450
    height = 350
    alt="You must have a JDK 1.2-enabled browser to view the applet">
    </applet>
    </body>
    </html>

    You should try the html converter so that the browsers are forced to invoke their plugin... or prompt the user to download one.
    To turn an applet into an application is easy. You act like the browser. You make a frame, construct the applet, and put the applet in the frame, (They are components) ... Then just call the init() method. Then add a window listener to call the stop() method when the frame gets closed.
    Application to an applet is a bit harder. You need to figure out what needs to be in the init method. Depending on what the application does you can also run into security issues.

Maybe you are looking for