Solved

Applet problem:

Posted on 2001-07-11
19
437 Views
Last Modified: 2013-11-23
I am trying to compile this, but keep getting errors:

CartonResearch.java:17: invalid method declaration; return type required
  public DisplayIcon()

CartonResearch.java:38: cannot resolve symbol
symbol : variable KeyEvent
location: class CartonResearch
  button.setMnemonic(KeyEvent.UK_C);

CartonResearch.java:39: cannot resolve symbol
symbol : classAcrionListener
location: class CartonResearch
  button.addAcrionListener(new ActionListener()

CartonResearch.java:76: cannot resolve symbol
symbol : class DisplayIcon
location: class CartonResearch
  frame.setContentPane(new DisplayIcon());

..........................................................

Here is my code:

I knoe that these are simple, and I am doing something I shouldn't be.

Can someone tell me why I am receiving these errors, and tell me what I can do to fix this?



import javax.swing.*;
import java.awt.*;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

public class CartonResearch extends JPanel
{
   JLabel VG1;  

   //private static String labelPrefix = "This application will pull carton data from history";
   
   public DisplayIcon()
   {
     
      ImageIcon icon = new ImageIcon("images/VenGroup.bmp", "");
      setLayout(new GridLayout(1,0));

      VG1 = new JLabel("Run SQL", icon, JLabel.CENTER);
      //Set the position of the text, relative to the icon
      VG1.setVerticalTextPosition(JLabel.BOTTOM);
      VG1.setHorizontalTextPosition(JLabel.CENTER);

      //Add labels to the JBufferedPane
      //add(VG1);

   }

   public Component createComponents()
   {
      //final JLabel label = new JLabel(labelPrefix);

      JButton button = new  JButton("Click me to run the query!");
      button.setMnemonic(KeyEvent.VK_C);
      button.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {}
      });

      JPanel pane = new JPanel();
      pane.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
      pane.setLayout(new GridLayout(0, 1));
      pane.add(button);
      //pane.add(label);
      pane.add(VG1);
     
      return pane;
   }

   public static void main(String[] args)
   {
      try
      {
         UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
      }  catch (Exception e) {}

      JFrame frame = new JFrame("Carton Research");

      frame.addWindowListener(new WindowAdapter()
      {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
      });

     
      CartonResearch app = new CartonResearch();
      Component contents = app.createComponents();
      frame.getContentPane().add(contents, BorderLayout.CENTER);
      frame.setContentPane(new DisplayIcon());

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setVisible(true);
   }
}
         

Thanks
berg
0
Comment
Question by:berg1375
  • 9
  • 6
  • 2
  • +2
19 Comments
 
LVL 2

Expert Comment

by:iDeb
ID: 6273128
1. You haven't given a return type for DisplayIcon()
 it should be
   public void DisplayIcon()

2. Import the KeyEvent class
   
    import java.awt.event.KeyEvent;

3. import the ActionListener interface
    import java.awt.event.ActionListener;
0
 
LVL 2

Expert Comment

by:iDeb
ID: 6273155
if i guess right, you've copied this code from a class called DisplayIcon, so i suggest you rename the DisplayIcon method to be CartonResearch. So now it will be the constructor, and it will look like this

public CartonResearch()
{
..the code in DisplayICon()
}
0
 
LVL 4

Author Comment

by:berg1375
ID: 6273179
Can you explain the renaming of DisplayIcon to CartonREsearch?

I took code from here and there, and put it together to try a few things. Obviously it didn't work well though.
0
 
LVL 4

Author Comment

by:berg1375
ID: 6273217
Okay I changed a few things liek you suggested,and it compiles, but when I try java CartonResearch it gives errors:

Exception in thread "main" java.lang.NullPointerExceprion
  at java.awt.Container.addImpl(nknown source)
  at java.awt.Container.a(unknown source)
  at CartonResearch.createComponents(CartonResearch.java:53)
  at CartonResearch.main(CartonResearch.java:77)

What do these mean??????????
0
 
LVL 4

Author Comment

by:berg1375
ID: 6273261
Okay I switched around a few things and recompiled, and it runs, but it only brings up an epmty Applet. It seems that it is not recognizing any of the controls! Here is the modified code:

import javax.swing.*;
import java.awt.*;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CartonResearch extends JPanel
{
   JLabel VG1;  

   //private static String labelPrefix = "This application will pull carton data from history";
   
   public void CartonResearch()
   {
     
      ImageIcon icon = new ImageIcon("images/VenGroup.bmp", "");
      setLayout(new GridLayout(1,0));

      VG1 = new JLabel("Run SQL", icon, JLabel.CENTER);
      //Set the position of the text, relative to the icon
      VG1.setVerticalTextPosition(JLabel.BOTTOM);
      VG1.setHorizontalTextPosition(JLabel.CENTER);

      //Add labels to the JBufferedPane
      add(VG1);

   }

   public Component createComponents()
   {
      //final JLabel label = new JLabel(labelPrefix);

      JButton button = new  JButton("Click me to run the query!");
      button.setMnemonic(KeyEvent.VK_C);
      button.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {}
      });

      JPanel pane = new JPanel();
      pane.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
      pane.setLayout(new GridLayout(0, 1));
      pane.add(button);
      //pane.add(label);
     
      return pane;
   }

   public static void main(String[] args)
   {
      try
      {
         UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
      }  catch (Exception e) {}

      JFrame frame = new JFrame("Carton Research");

      frame.addWindowListener(new WindowAdapter()
      {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
      });

     
      CartonResearch app = new CartonResearch();
      Component contents = app.createComponents();
      frame.getContentPane().add(contents, BorderLayout.CENTER);
      frame.setContentPane(new CartonResearch());

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setVisible(true);
   }
}
         

Can you explain to me why it is doing this, so I can learn form this and not do it again?
0
 
LVL 27

Expert Comment

by:rrz
ID: 6273420
Why do you keep calling this an applet? It is an application.
Also does ImageIcon accept .bmp image?
I would try a .gif or a .jpeg image.
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 30 total points
ID: 6273431
1) The above code is not an applet, it is an application.
2) The constructor should not be void :

Line 20 :

    public void CartonResearch()

should be :

    public CartonResearch()

3) Your main() method is all screwed.

First you create a CartonResearch object.
The you get a JPanel from it and add it to the contentPane.
Then you create another CartonResearch object and set it as the content pane. You also create a windowlistener that quits when the window is closed and set the close operation of the window to quit. You only need to do one of those, not both.

Set lines 77 and 78 to this :

    frame.getContentPane().add(contents, BorderLayout.CENTER);
    frame.getContentPane().add(app,BorderLayout.NORTH);
 
0
 
LVL 4

Author Comment

by:berg1375
ID: 6273462
So I suck! That is why I am trying to learn  :-)
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6273511
You don't suck, you are just trying to assemble a program using odd bits of code.

Extra pointer here.

You have surplus import statements :

Once you have imported javax.swing.* you do not need to import :

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

The * covers them.

Similarly with java.awt.* and

import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

You can do all your imports in three lines like this :

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



0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 15

Expert Comment

by:ozymandias
ID: 6273531
It's still a bit convoluted but I have tidied up :

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

public class CartonResearch extends JPanel{

  JLabel VG1;

  public CartonResearch(){

     ImageIcon icon = new ImageIcon("images/VenGroup.bmp", "");
     setLayout(new GridLayout(1,0));
     VG1 = new JLabel("Run SQL", icon, JLabel.CENTER);
     VG1.setVerticalTextPosition(JLabel.BOTTOM);
     VG1.setHorizontalTextPosition(JLabel.CENTER);
     add(VG1);
  }

  public Component createComponents(){

     JButton button = new  JButton("Click me to run the query!");
     button.setMnemonic(KeyEvent.VK_C);
     button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){}
     });
     JPanel pane = new JPanel();
     pane.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
     pane.setLayout(new GridLayout(0, 1));
     pane.add(button);
     return pane;
  }

  public static void main(String[] args){
       try{
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
       }catch(Exception e){}

       JFrame frame = new JFrame("Carton Research");
       CartonResearch app = new CartonResearch();
       Component contents = app.createComponents();
       frame.getContentPane().add(contents, BorderLayout.CENTER);
       frame.getContentPane().add(app,BorderLayout.NORTH);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.pack();
       frame.setVisible(true);
  }
}
0
 
LVL 4

Author Comment

by:berg1375
ID: 6273540
Okay, I changed everything, and it compiles, and runs. It shows everything but the icon. I changed the icon to a gif, but either way, I can work around that.

Can someone explain to me the fundamental problems I had, or post a link that will give me a detailed structure of the way I should be doing this. It must be simply stated, since I am not too familiar with Java, and the structures.

Thanks
berg
0
 
LVL 4

Author Comment

by:berg1375
ID: 6273606
What I need is a structure layout of Java. Something liek:

App name{
 function1{
 }
 function2{
 }
 Main{
 }.......

I would like to know what rules constrain me adding functions where? I knwo that I call them all from the main function, but does it matter where above the main function I add functions?

For example, if I add a JDBC connection code to this, is there a specific place I have to add the function or just anywhere above main? What if I were to add a combobox on the grid? woudl that be in the components, or a seperate function?

I just want to know the structure so I can write good code.
0
 
LVL 35

Expert Comment

by:girionis
ID: 6273634
 berg instead of some links I will you a title of a book to read. "Java How to Program" 3rd edition, by Deitel and Deitel. Excellent! It is the best book on Java I have ever read. Try it and you will not regret. It is really simple and covers from the most elementary to the most advanved parts of the Java Programming Language. Buy the 3rd edition that covers Swing as well.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6273640
I would start with the Java Tutorial (http://java.sun.com/docs/books/tutorial/?frontpage-spotlight).

There is a QuickStart Guide to Swing GUIs (http://java.sun.com/docs/books/tutorial/uiswing/mini/index.html).

Also, if you have the JDK there should be a directory called demo in the JDK directory and a subdirectory called jfc (Java Foundation Classes - the real name of Swing).

Look at the applications in there.
There is one called SwingSet2 which you can run by typing:

    java -jar SwingSet2.jar

This illustrates a lot of the major components in swing and will display the source code for each one.
0
 
LVL 4

Author Comment

by:berg1375
ID: 6273678
I have a book on Java at home it os by some IVOR HORTON? or something. It seems pretty good, but like I said it is at home though.

Ozy, evertime I try running a demo from jdk, I get can't resolve symbol errors. Why is this? I'm running Java 1.4.
0
 
LVL 4

Author Comment

by:berg1375
ID: 6273719
Thanks for going beyond my expectations like usual.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6274213
Hmmm...I do not have 1.4. I am still using 1.3.1.

I suspect that you have a problem with your class path.
When you reference an object, say :

   JPanel myPanel = new JPanel();

The JVM has to be able to find the JPanel class somewhere in the classpath. An inability to "resolve a symbol" normally means that either a) you have not included an import statement that covers that class or b) that the JVM cannot find it. Since it is Sun's code, I suspect that it is b).

The JDK will have installed all the required classes in a set of jar (Java ARchive) files. There is usually an environments variable called CLASSPATH which points to these files so that the classes they contain can be used.

I do not know what system you have but I will assume it is some kind of wintel system.

At a command prompt type :

    echo %CLASSPATH%

this should return the variable the JVM is using.
Make sure that it is correct and that the files it points to exist.

You can manually set your classpath in a number of ways.
In the enviroment settings in the System item in control panel, by typing set CLASSPATH = "list", where list is a semicolon separated list of files and directories of by specifying it on the command line when you invoke java, like so :

    java -cp "list" classname

Your classpath should normally have . (current working directory) as the first item in the list.

Have a look at these and let me know how you get on.

Cheers.
0
 
LVL 4

Author Comment

by:berg1375
ID: 6274406
I think I am getting the hang of this crazy Java thing  :-)

I added a combobox, with minimal difficulty. I know it isn't much, but it is a step in the right direction! Now I need to figure out how to connect to a DB2 database on an AS400, and be able to use the values of 2 text boxes in the SQL statement the user will build.

This should be rather amusing all in itself.

Thanks again
berg
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6274635
My pleasure, thanks for the points.

BTW, the answer to your comment above about the structure of Java is that there is no easy answer.

There are no canonical formulae for how to design your objects.

Object design is a whole discpline in itself, quite separate from actual object programming.

The answer really lies in experience (as a euphemism for trial and error) and learning to "think in Objects". I suspect that this is why Bruce Eckel's books (Thinking in Java and Thinking in C++) are among the best and most widely recommended - not just because they are free - (http://www.bruceeckel.com), but because reading them represents, for a lot of readers, a quantum leap in their understanding, not just of Java or C++, but of the priciples of object programming.

You will find, after a relatively short time, that this type of thought becomes second nature.
Things like "is a" and "has a" relationships. The ideas behind encapsulation, inheritance and polymorphism. Writing neat, well rounded, reusable code. You will only notice them when they are absent, or when someone says, "Why are you doing this like that ?", and your immediate response is, "Well, it's obvious isn't it ?".

As a very brief, general, and by no means complete set of principles, here's my thoughts :

1) Start off by defining your objects in terms of very general functionality. This may be no more than a name and less that 10 words about function. This really defines the internal logic of the object, "What does it do ?"

2) Always split process/logic from display as completely as possible, and split data/data access from process/logic as completely as possible. This is part of the Model View Controller (MVC) design pattern.

3) Then define, again in very general terms, their relationships, both interms of a hierarchy, "is a / has a" realtionships and the types of messages they will send to each other (method calls).

4) For each object define its properties. Decide if they are static, final, private public etc. As a general rule, make as many of an objects properties as possible private or protected. If an object has a public property...ask why...there's probably no good reason.

5) This will, in part, define your first set of methods. These are not "behavioural" methods, but accessor methods, i.e. the get and set methods for all the properties you have just defined.

6) Next, look at the public methods of your objects, i.e the way they will communicate with each other. This is all about sending messages between objects. An obejct has a function. It performs this function in response to a message from another object and may then return somthing in response to that message.

In a simple application that entire process might take only 5 minutes.

I'm sure lots of people will leap in to pick holes in or shoot down what I've laid out above, but hey, if it was simple there wouldn't be so many books on the subject.

In response your question about main(), generally you want to be doing as little as possible in your main() method. In a typical swing app (as I would write it) my main method would look something like this :

     public static void main(String[]){
          MyClass mc = new MyClass(args);
          mc.setSize(400,400);
          mc.setVisible(true);
     }
     

...and that;s it. basically I would invoke the public class of the application whoch would probably extend JFrame and the constructor of that class would take it from there. It would in turn invoke its child component classes and any logic or data classes required and their constructors would continue the cascade. It might not even have the setSize() and setVisible() calls. It might include some checking for args, but that's all.

I hope I haven't covered too much stuff that you already knew.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now