Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-19-2011, 05:18 PM   PM User | #1
katnyan
New to the CF scene

 
Join Date: May 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
katnyan is an unknown quantity at this point
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_;
}
katnyan is offline   Reply With Quote
Old 05-20-2011, 01:41 PM   PM User | #2
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
wow messy, please wrap your code and only post the relevant portions.
angst is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:09 PM.


Advertisement
Log in to turn off these ads.