Link to home
Start Free TrialLog in
Avatar of berg1375
berg1375

asked on

Applet problem:

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
Avatar of iDeb
iDeb

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;
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()
}
Avatar of berg1375

ASKER

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.
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??????????
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?
Avatar of rrz
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.
ASKER CERTIFIED SOLUTION
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So I suck! That is why I am trying to learn  :-)
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.*;



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);
  }
}
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
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.
 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.
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.
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.
Thanks for going beyond my expectations like usual.
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.
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
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.