Compiling error

Choice font = new Choice();      
      for ( int i = 0; i < fontList.length; i++ )
       {
      add(font);
      font.addActionListener(this);
      font.addItem(fontList[i]);
      font.setBackground(Color.lightGray);
      }

      public void actionPerformed(ActionEvent e) {
             if (e.getSource() instanceof Choice) {
                 String choice = (String) e.getSelectedItem();
                 editorPane.setFont( new Font( choice,Font.ITALIC,10) );
            }
      }


Error:
cannot resolve symbol
method addActionListener()
class java.awt.Choice
>> font.addActionListener(this);

Error:
cannot resolve symbol
method getSelectedItem()
class java.awt.event.ActionEvent
>>  String choice = (String) e.getSelectedItem();

-------------------------------------------------------------------

Any expert please help me to debug this code.
tloipAsked:
Who is Participating?
 
CeldricConnect With a Mentor Commented:
Well, since you do include the jdk in your path, i guess you include the neccesary libraries..

Well, like they have said b4..this error:
Error:
cannot resolve symbol
method getSelectedItem()
class java.awt.event.ActionEvent

means thats the ActionEvent does not have a getSelectedItem method and its right:

http://java.sun.com/j2se/1.3/docs/api/java/awt/event/ActionEvent.html

That is the specification for the class ActionEvent, and there it is listed all methods that
are implemented within the class and inherited from others.
getSelectedItem is not one of them.

as for the choice class:
http://java.sun.com/j2se/1.3/docs/api/java/awt/Choice.html
it doesnt have an addActionListener either, although it has like 4 different listeners
ActionListener isnt one of them.

Sorry to say, you got to find different methods to implement your application.

Good luck

0
 
sciuriwareCommented:
Obviously Choice has no method 'addActionListener()'.
Ditto ActionEvent has no method 'getSelectedItem()'.
Please refer to the JAVADOC for the methods that exist for those classes.
;JOOP!
0
 
MogalManicConnect With a Mentor Commented:
Is this what you want?

            Choice font = new Choice();
            for (int i = 0; i < fontList.length; i++) {
                  add(font);
                  font.addInputMethodListener(this);
                  font.addItem(fontList[i]);
                  font.setBackground(Color.lightGray);
            }
      }


      public void actionPerformed(ActionEvent e) {
            if (e.getSource() instanceof Choice) {
                  String choice = (String) e.getActionCommand();
                  editorPane.setFont(new Font(choice, Font.ITALIC, 10));
            }
      }
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
tloipAuthor Commented:
what do u mean no method?
can u specify it?
0
 
sciuriwareCommented:
The methods you wrote down do not exist for those classes.
;JOOP!
0
 
tloipAuthor Commented:
an error occurred:

addInputMethodListener(java.awt.event.InputMethodListener) in java.awt.Component cannot be applied to (gui)

> font.addInputMethodListener(this)
0
 
tloipAuthor Commented:
doesn't exist?
So do i need to modify my code or just add code to it?

0
 
sciuriwareCommented:
If you think that Choice can have an actionListener ... read the JAVADOC.
;JOOP!
0
 
MogalManicCommented:
What do you want to accomplish?  

The Font class does NOT have an actionListener event.  It DOES have an inputMethod listener event.  Read the javadoc for the Font class and the Choice class.
0
 
CeldricCommented:
I think we need to see which libraries are you importing and if those classes are where
they are supposed to be regardless of method use, the error is because it
cant find the class, not an invalid method.

Also, it will be helpful if you tell us in which plataform are you coding...
Netbeans, Eclipse? Are you trying to make a webapp? an applet?

and MogalManic is correct

addInputMethodListener(java.awt.event.InputMethodListener) in java.awt.Component cannot be applied to (gui)
> font.addInputMethodListener(this)

This means that you cant attach a InputMethodListener to a GUI object.

We need more info here..
0
 
tloipAuthor Commented:
i'm using dos platform (j2sdk1.4.2_04).
this is one part of my program.
This part allows user to select the font face being used display the message (typed in textfield) in editorpane.

i imported these:

/* * My GUI Client */
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
//for HTML Headers
import javax.swing.text.StyledEditorKit.*;
import javax.swing.text.html.HTMLEditorKit.*;
import javax.swing.text.html.*;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkEvent.EventType;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
//for layout managers
import java.awt.event.*;
//for action and window events
import java.io.*;
import java.net.*;
import java.awt.GraphicsEnvironment;
0
 
CeldricCommented:
Ah, now I think thats your problem, you see, (Well, in my case, I dont know if its by default)
Well, whenever I use only the command line to code, I have to manually
direct the jdk to the libraries.

So, no matter if you declare the imports in your java file, the JDK wont find it until you
point it to the right jar or libraries outside the file...heres an example:

This class uses the openjms library, although I declare it inside the java file, I still need
to tell the JDK where those imports are, this is done with the -classpath option.

Java -classpath c:\myJ2EEjdk\lib\openjms.jar com.softtek.mypackage.MyClass

The syntax is: Java -classpath <library location> <class package>

If still in doubt, I recommend you learn to use either Netbeans or MyEclipse they do all this
for you, and they are both great coding plataforms, plenty of info out there too.

Oh! Remember to have your enviromental variables set, JAVA_HOME is very important

Hope this helped.
0
 
tloipAuthor Commented:
I have NetBean as well but i really no idea on how to run the file.
I'm running the server...something like i will type like this :
>>>    java gui 127.0.0.2 2222
I need to run 3 files at one go to run my program.

Is it the problem occured on what platform i'm using and isn't because the code problem?
0
 
tloipAuthor Commented:
usually before i run my file..I might type..

>>>  path=C:\jsdk1.4.2_04\bin

0
 
tloipAuthor Commented:
Ok, thanks for giving comment. I can solve it with your guidance.
0
 
tloipAuthor Commented:
I did that..I'm not sure correct or not but it doesn't work. Means didn't show the message in selected font face.

public void itemStateChanged(ItemEvent e) {
             if (e.getSource() instanceof Choice) {
                  String choice = (String) e.getItem();
                  editorPane.setFont( new Font( choice,Font.PLAIN,10 ) );
            }
      }
0
All Courses

From novice to tech pro — start learning today.