Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs down jpopupmenu action listner not working, desperately needed help

    I am using jpopupmenu in an APPLET. how to add action listener for this jpopupmenu.

    i tried using contentpane(), but it is not working.


    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.PrintStream;
    import netscape.javascript.JSObject;
    import javax.swing.*;
    import java.awt.event.*;

    public class htmlMenu extends JApplet
    implements ActionListener
    {

    public void init()
    {

    Container content_pane = getContentPane ();
    content_pane.setLayout (new FlowLayout ());
    try
    {
    winLocal = JSObject.getWindow(this);
    }
    catch(Exception _ex)
    {
    System.out.println("Error: Applet cannot get window name. \nThis error will throw unhandled exceptions.\nMake sure you have the MAYSCRIPT attribute in your object or applet tag!");
    }
    paramItemCap_ = getParameter("itemcap");
    paramDisableList_ = getParameter("disable");
    paramCommonFunction_ = getParameter("commonfunction");
    boolMenuEnabled = true;
    if(paramItemCap_ != null && paramItemCap_ != "")
    intItemCap = Integer.parseInt(paramItemCap_) + 1;
    System.out.println("Item Cap Value :" + intItemCap);
    mnuMain = new JPopupMenu();
    mnuSub = new JMenu[intItemCap];
    mnuItem = new JMenuItem[intItemCap];
    strAryFunctions = new String[intItemCap];
    if(!getNextParam(1))
    {
    intItemCap = 0;
    intLenDisableList = 0;
    }
    for(x = 1; x < intItemCap; x++)
    {
    intMaxItemID = x;
    intMenuParentID = Integer.parseInt(strMenuData.substring(0, intDLoc1));
    strMenuLabel = strMenuData.substring(intDLoc1 + 1, intDLoc2);
    strMenuFunction = strMenuData.substring(intDLoc2 + 1);
    if(strMenuFunction.length() < 1)
    strMenuFunction = paramCommonFunction_;
    if(strMenuLabel.equals("-"))
    buildSeparator(intMenuParentID);
    else
    if(strMenuFunction.equals("none") || strMenuFunction.equals("submenu"))
    {
    mnuSub[x] = new JMenu(strMenuLabel);
    mnuSub[x].addActionListener(this);
    if(intMenuParentID == 0)
    mnuMain.add(mnuSub[x]);
    else
    mnuSub[intMenuParentID].add(mnuSub[x]);
    } else
    if(strMenuFunction.equals("url"))
    {
    menuURL = getParameter("url" + x);
    dlLoc = menuURL.indexOf("|");
    if(dlLoc != -1)
    {
    strTemp = menuURL.substring(dlLoc + 1);
    menuURL = menuURL.substring(0, dlLoc);
    if(strTemp.equals("_top"))
    {
    strPre = "top.location.href='";
    strPost = "';";
    } else
    if(strTemp.equals("_new"))
    {
    strPre = "window.open('";
    strPost = "','NewWindow');";
    } else
    {
    strPre = "top." + strTemp + ".location.href='";
    strPost = "';";
    }
    } else
    {
    strPre = "location.href=";
    strPost = "';";
    }
    strAryFunctions[x] = strMenuFunction;
    mnuItem[x] = new JMenuItem(strMenuLabel);
    mnuItem[x].setActionCommand(strPre + menuURL + strPost);
    if(intMenuParentID == 0)
    mnuMain.add(mnuItem[x]);
    else
    mnuSub[intMenuParentID].add(mnuItem[x]);
    } else
    {
    strAryFunctions[x] = strMenuFunction;
    mnuItem[x] = new JMenuItem(strMenuLabel);
    mnuItem[x].setActionCommand(strMenuFunction);
    if(intMenuParentID == 0)
    mnuMain.add(mnuItem[x]);
    else
    mnuSub[intMenuParentID].add(mnuItem[x]);
    }
    if(!getNextParam(x + 1))
    x = intItemCap;
    }

    content_pane.add(mnuMain);

    if(paramDisableList_ != null && paramDisableList_.length() > 0)
    {
    paramDisableList_ += ",";
    strItemID = "0";
    intLenDisableList = paramDisableList_.length();
    for(x = 0; x <= intLenDisableList - 1; x++)
    {
    intChar = paramDisableList_.charAt(x);
    if(intChar != 44)
    strItemID += (char)intChar;
    else
    try
    {
    intItemID = Integer.parseInt(strItemID);
    if(intItemID <= intMaxItemID)
    setItemEnabled(intItemID, false);
    strItemID = "0";
    }
    catch(NumberFormatException _ex)
    {
    strItemID = strItemID;
    getAppletContext().showStatus("Invalid Disable Parameter");
    }
    }

    }
    }

    private void setItemEnabled(int intItemID_, boolean boolEnabled_)
    {
    mnuItem[intItemID_].setEnabled(boolEnabled_);
    }

    private void buildSeparator(int parentID_)
    {
    if(parentID_ == 0)
    mnuMain.addSeparator();
    else
    mnuSub[menuItemParent].addSeparator();
    }

    private boolean getNextParam(int x_)
    {
    strMenuData = getParameter("item" + x_);
    if(strMenuData == null)
    return false;
    intDLoc1 = strMenuData.indexOf("|");
    if(intDLoc1 == -1)
    return false;
    intDLoc2 = strMenuData.lastIndexOf("|");
    if(intDLoc2 == intDLoc1)
    {
    strMenuData += "|";
    intDLoc2 = strMenuData.length() - 1;
    }
    return true;
    }

    public String getAppletInfo()
    {
    return "htmlMenu Java Applet\nComputer Task Group, Inc.\nCopyright 1999";
    }

    public void setMenuEnabled(boolean state_)
    {
    boolMenuEnabled = state_;
    }

    public void actionPerformed(ActionEvent e_)
    {
    String strCommand = e_.getActionCommand();
    String strArguments[] = {
    strCommand
    };
    if(strCommand == null || strCommand.length() < 1)
    try
    {
    winLocal.eval("alert('The page author has failed to supply an appropriate action for this menu item')");
    }
    catch(Exception _ex)
    {
    getAppletContext().showStatus("Error accessing function or window");
    }
    else
    winLocal.eval(strCommand);
    }

    public void showMenu(int intX_, int intY_)
    {
    if(boolMenuEnabled)
    mnuMain.show(this, intX_, intY_);
    }

    public htmlMenu()
    {
    intItemCap = 100;
    }

    JSObject winLocal;
    JPopupMenu mnuMain;
    JMenuItem mnuItem[];
    JMenu mnuSub[];
    int intItemID;
    int intMaxItemID;
    int intMenuParentID;
    int intLenDisableList;
    int intDLoc1;
    int dlLoc;
    int intDLoc2;
    int menuItemParent;
    int intItemCap;
    int intChosenItem;
    int intChar;
    int x;
    boolean boolMenuEnabled;
    String menuURL;
    String strTemp;
    String strPre;
    String strPost;
    String strAryFunctions[];
    String strMenuLabel;
    String strMenuFunction;
    String strMenuData;
    String strItemID;
    String paramDisableList_;
    String paramItemCap_;
    String paramCommonFunction_;
    }

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    wow messy, please wrap your code and only post the relevant portions.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •