Re: Strange Appletviewer/HTML difference problem... (please help)

Discussion in 'Java' started by Steve Claflin, Sep 3, 2003.

  1. Al Murphy wrote:
    >
    > Hi,
    >
    > I hope that you can help me with this one cos I'm at the end of my
    > teather. I've written a cool little applet (using Eclipse editor) that
    > appears to work perfectly using the appletviewer command from inside
    > Eclipse. However when I point to it using an external HTML file - it
    > does not work properly
    >
    > I'm using the following tag:-
    >
    > <APPLET code = "MessageDigestCreator.class" ARCHIVE = "a.jar" width =
    > 600 height = 650>
    > Upgrade your browser
    > </APPLET>
    >
    > The only thing that I can thing of is the fact that I am using some
    > alien code. You see I wanted to have a drop-down menu associated with
    > a "JTextField" object. You know that cut/copy/paste thing we all love
    > so much. So what did I do? I found the code online that generates this
    > behaviour.
    >
    > It worked (in the appletviewer mode) but not via a HTML tag. I
    > noticed 1 interesting thing about this code. When I compiled it not
    > only did it make a "PopupMenuMouseListener.class" file but also
    > PopupMenuMouseListener$1.class -> PopupMenuMouseListener$6.class
    > files. I have never seen java .class files of this nature.
    >
    > Anyway - anyone have any similiar problems? I'm stuck and would
    > greatly appreciate any suggestions/comments/ideas.
    >
    > Cheers,
    > Al.
    >
    > ===========CODE AS FOLLOWS:
    >
    > import java.awt.*;
    > import java.awt.datatransfer.*;
    > import java.awt.event.*;
    > import javax.swing.*;
    > import javax.swing.event.*;
    > import javax.swing.text.*;
    >
    > /**
    > * @author Default
    > *
    > * To change the template for this generated type comment go to
    > * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and
    > Comments
    > */
    > public class PopupMenuMouseListener extends MouseAdapter
    > {
    > private JPopupMenu popup = new JPopupMenu();
    >
    > private JMenuItem undoItem, cutItem, copyItem, pasteItem, deleteItem,
    > selectAllItem;
    > private JTextComponent textComponent;
    > String savedstring="";
    > String lastactionselected="";
    >
    >
    > public PopupMenuMouseListener()
    > {
    > Action action = new AbstractAction("Undo")
    > {
    > public void actionPerformed(ActionEvent ae)
    > {
    > if(lastactionselected.compareTo("")!=0)
    > {
    > textComponent.setText("");
    > textComponent.replaceSelection(savedstring);
    > }
    > }
    > };
    >
    > undoItem = popup.add(action);
    > undoItem.setMnemonic('t');
    > popup.addSeparator();
    > action = new AbstractAction("Cut")
    > {
    > public void actionPerformed(ActionEvent ae)
    > {
    > lastactionselected="c";
    > savedstring=textComponent.getText();
    > textComponent.cut();
    > }
    > };
    >
    > cutItem = popup.add(action);
    > cutItem.setMnemonic('t');
    >
    > action = new AbstractAction("Copy")
    > {
    > public void actionPerformed(ActionEvent ae)
    > {
    > lastactionselected="";
    > textComponent.copy();
    > }
    > };
    >
    > copyItem = popup.add(action);
    > copyItem.setMnemonic('c');
    > action = new AbstractAction("Paste")
    > {
    > public void actionPerformed(ActionEvent ae)
    > {
    > lastactionselected="p";
    > savedstring=textComponent.getText();
    > System.out.println("in paste code savedstring is: "+savedstring);
    > textComponent.paste();
    > }
    > };
    >
    > pasteItem = popup.add(action);
    > pasteItem.setMnemonic('p');
    >
    > action = new AbstractAction("Delete")
    > {
    > public void actionPerformed(ActionEvent ae)
    > {
    > lastactionselected="d";
    > savedstring=textComponent.getText();
    > textComponent.replaceSelection("");
    > }
    > };
    >
    > deleteItem = popup.add(action);
    > deleteItem.setMnemonic('d');
    > popup.addSeparator();
    >
    > action = new AbstractAction("Select All")
    > {
    > public void actionPerformed(ActionEvent ae)
    > {
    > lastactionselected="s";
    > savedstring=textComponent.getText();
    > textComponent.selectAll();
    > }
    > };
    >
    > selectAllItem = popup.add(action);
    > selectAllItem.setMnemonic('a');
    > }
    >
    > public void mouseClicked(MouseEvent e)
    > {
    > //if (e.getModifiers()==InputEvent.BUTTON3_MASK)
    > if(SwingUtilities.isRightMouseButton(e))
    > {
    > if (!(e.getSource() instanceof JTextComponent))
    > {
    > return;
    > }
    >
    > textComponent = (JTextComponent)e.getSource();
    > // 1.3 version
    > //textComponent.requestFocus();
    > // 1.4 preferred method
    > textComponent.requestFocus();
    > //textComponent.requestDefaultFocus();
    > //textComponent.requestFocusInWindow();
    >
    >
    > boolean enabled = textComponent.isEnabled();
    > boolean editable = textComponent.isEditable();
    > boolean nonempty = !(textComponent.getText()==null ||
    > textComponent.getText().equals(""));
    >
    > boolean marked = textComponent.getSelectedText()!=null;
    > boolean pasteAvailable =
    > Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).isDataFlavorSupported(DataFlavor.stringFlavor);
    > undoItem.setEnabled(enabled && editable);
    > cutItem.setEnabled(enabled && editable && marked);
    > copyItem.setEnabled(enabled && marked);
    > pasteItem.setEnabled(enabled && editable && pasteAvailable);
    > deleteItem.setEnabled(enabled && editable && marked);
    > selectAllItem.setEnabled(enabled && nonempty);
    > popup.show(e.getComponent(),e.getX(), e.getY());
    > }
    > }
    >
    > } // end PopupMenuMouseListener
    >
    > ==========CODE END


    Classes like PopupMenuMouseListener$1.class are the result of anonymous
    inner classes, like the new AbstractAction("Select All") ... section of
    your code. Those class files need to be in the archive a.jar on your
    server. The appler viewer is probably finding them locally.


    --
    Steve
    --
    http://www.steveclaflin.com
    Steve Claflin, Sep 3, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RCS
    Replies:
    3
    Views:
    3,035
    Jonathan Mcdougall
    Sep 13, 2003
  2. Replies:
    7
    Views:
    2,330
    Andrew Thompson
    Dec 16, 2005
  3. D. Patrick
    Replies:
    6
    Views:
    14,787
    Oliver Wong
    Jul 4, 2006
  4. Momo
    Replies:
    4
    Views:
    363
  5. Replies:
    1
    Views:
    533
    Andrew Thompson
    Feb 4, 2007
Loading...

Share This Page