JMenus and ActionListeners

I'm making a MineSweeper clone and need some pointers on how to let people start a new game by either going to Game and choosing new or Ctrl_N.
Here is what I have so far:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Menus extends JMenu
private static TimerDisplay TD=new TimerDisplay();
public static JMenuBar menus()
JMenuBar mb=new JMenuBar();
JMenu game=new JMenu("Game");
game.setMnemonic(KeyEvent.VK_G);
mb.add(game);
JMenuItem nw=new JMenuItem("New");
nw.setMnemonic(KeyEvent.VK_N);
nw.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
nw.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
// What could I put here.
JMenuItem exit=new JMenuItem("Exit");
exit.setMnemonic(KeyEvent.VK_E);
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
System.exit(0);
game.add(nw);
game.add(new JSeparator());
game.add(exit);
return mb;
}

I would probably opt for a different way of thinking about this. First off, I'd get rid of all of the statics there and make Menus a true OOP class. Next, I'd try to have it communicate in an OOP fashion with the class that displays and runs the game itself, here in my example called "MyGame". I'd give MyGame class an exit() method to allow it to decide how to exit, and a reset() method to allow it to decide how it wants to reset for a new game. This way the menus shouldn't have to and doesn't worry about these things. Something like so:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Menus //extends JMenu
  private JMenuBar menubar = new JMenuBar();
  // a timerdisplay object may not be needed in the menu class
  //private TimerDisplay timerDisplay = new TimerDisplay();
  private MyGame myGame;
  Menus()
  //public void setGameDisplay(MyGame myGame, TimerDisplay timerDisplay)
  public void setGameDisplay(MyGame myGame)
    this.myGame = myGame;
    //this.timerDisplay = timerDisplay; // probably not needed
    initMenus();
  public JMenuBar getMenuBar()
    return menubar;
  public void initMenus()
    JMenu gameMenu = new JMenu("Game");
    gameMenu.setMnemonic(KeyEvent.VK_G);
    menubar.add(gameMenu);
    JMenuItem nw = new JMenuItem("New");
    nw.setMnemonic(KeyEvent.VK_N);
    nw.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
        ActionEvent.CTRL_MASK));
    nw.addActionListener(new ActionListener()
      public void actionPerformed(ActionEvent event)
        myGame.reset();
    JMenuItem exit = new JMenuItem("Exit");
    exit.setMnemonic(KeyEvent.VK_E);
    exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,
        ActionEvent.ALT_MASK));
    exit.addActionListener(new ActionListener()
      public void actionPerformed(ActionEvent event)
        //System.exit(0);
        myGame.exit();
    gameMenu.add(nw);
    gameMenu.add(new JSeparator());
    gameMenu.add(exit);
}Then it could all be tied together with a Controller or Main class that creates the MyGame class, creates the Menus class and adds one to the other.

Similar Messages

  • Actions and ActionListeners as parameters

    Hi everybody,
    I want to overload a method so it can take either 2 Actions, 2 ActionListeners, or one of each. The issue with this is that I'd have to make 4 overloaded signatures and almost identical methods, like this:
    method( action, action )
    method( action, listener )
    method( listener, action )
    method( listener, listener )It seems like doing it this way would result in a lot of extra code, and hence become a real pain in the neck. So, I was curious, has anyone else either tried to do this before, or do you have any ideas as to how I might be able to do this more efficiently? When I looked at the API it didn't look like Actions and ActionListeners share a root class I can use.
    Thanks,
    Jezzica85

    jezzica85 wrote:
    Hi everybody,
    I want to overload a method so it can take either 2 Actions, 2 ActionListeners, or one of each. The issue with this is that I'd have to make 4 overloaded signatures and almost identical methods, like this:
    method( action, action )
    method( action, listener )
    method( listener, action )
    method( listener, listener )
    Well, if you want to support that then you are just going to have to do suffer through it, the only shortcut I can recommend is that your method(action, listener) and method(listener, action) are the same so you only have to implement 1 and just use the other as a entry point to call the one you wish to contain the code.

  • JSF Actions and ActionListeners with Tiles and forms

    I�m having a problem trying to use the Tiles functionality in Struts 1.1 with JSF and was wondering if anyone could help me.
    I have defined a very simple header, menu, content Tile that doesn�t involve nesting of tiles (ExampleTile_content1Level.jsp).
    I have 3 JSP pages, the first testHarness.jsp is NOT built using Tiles and is just used to load some test data into a session scoped bean using an actionListener and then forward to a Tile generated page (ExampleTile3.jsp) using a hard-coded action �applicationSummary� when a commandLink is pressed. This works fine for both the action and actionListener.
    ExampleTile3.jsp contains another commandLink that is meant to forward to another tile ExampleTile2.jsp. This also works until I try to add the <h:form> � </h:form> tag around the outside of the <h:panelGrid> tags in ExampleContent1.jsp when the action and actionListener then fail to fire and I get an �Error on Page� message in Explorer the detail of which says �Error �com_sun_rave_web_ui_appbase_renderer_CommandLinkRendererer� is null or not an object�.
    However I need a form so that I can bind UI controls to data from the bean stored in the session scope. This is only a problem when I use Tiles to define the pages. Does anyone know what I am doing wrong?
    Any help would be much appreciated.
    Tiles.xml
       <definition name="example3" path="/pages/exampleTile_content1Level.jsp" >
              <put name="headerClass" value="someStyle"/>
              <put name="menuClass" value="someStyle"/>
              <put name="contentClass" value="someStyle"/>
              <put name="header-title" value="/pages/exampleHeader.jsp" />
              <put name="menu" value="/pages/exampleMenu.jsp" />
              <put name="content" value="/pages/exampleContent1.jsp" />
       </definition>
       <definition name="example2" path="/pages/exampleTile_content1Level.jsp" >
              <put name="headerClass" value="someStyle"/>
              <put name="menuClass" value="someStyle"/>
              <put name="contentClass" value="someStyle"/>
              <put name="header" value="/pages/exampleHeader.jsp" />
              <put name="menu" value="/pages/exampleHeader.jsp" />
              <put name="content" value="/pages/exampleContent2.jsp" />
       </definition>ExampleTile3.jsp
    <f:view>
         <h:form>
              <tiles:insert definition="example3" flush="false" />
         </h:form>
    </f:view> ExampleTile2.jsp
    <f:view>
         <h:form>
              <tiles:insert definition="example2" flush="false" />
         </h:form>
    </f:view> Faces-config.xml
    <navigation-rule>
        <from-view-id>/pages/testHarness.jsp</from-view-id>
           <navigation-case>
                <from-outcome>applicationSummary</from-outcome>
                <to-view-id>/pages/exampleTile3.jsp</to-view-id>
              <redirect/>
           </navigation-case>
    </navigation-rule>
    <navigation-rule>
        <from-view-id>/pages/exampleTile3.jsp</from-view-id>
           <navigation-case>
                <from-outcome>nextPage</from-outcome>
                <to-view-id>/pages/exampleTile2.jsp</to-view-id>
                <redirect/>
           </navigation-case>
    </navigation-rule> ExampleTile_content1Level.jsp
    <tiles:importAttribute scope="request"/>
    <h:panelGrid columns="1" >
         <f:subview id="header-title">
              <tiles:insert name="header-title" flush="false" />
         </f:subview>
         <f:subview id="menu">
              <tiles:insert name="menu" flush="false" />
         </f:subview>
         <f:subview id="content">
              <tiles:insert name="content" flush="false" />
         </f:subview>
    </h:panelGrid> ExampleHeader.jsp / ExampleMenu.jsp
    <tiles:importAttribute scope="request"/>
    <h:panelGrid columns="1" columnClasses="someSyle">
         <h:outputFormat value="This is the {0}.">
              <f:param value="Header / Menu as appropriate "/>         
         </h:outputFormat>
    </h:panelGrid> ExampleContent1.jsp
    <tiles:importAttribute scope="request"/>
    <h:form>     <----- Fails with this tag included but works without it.
    <h:panelGrid columns="1" >
              <h:outputFormat value="This is the {0}.">
                   <f:param value="Content on the FIRST page"/>
              </h:outputFormat>
              <h:commandLink action="nextPage" immediate="false">
                   <h:outputText value="Click to go to next page"/>
              </h:commandLink>
    </h:panelGrid>
    </h:form> ExampleContent2.jsp
    <tiles:importAttribute scope="request"/>
    <h:panelGrid columns="1" >
         <h:outputFormat value="This is the {0}.">
              <f:param value="Content on the SECOND page"/>
         </h:outputFormat>
    </h:panelGrid>

    jezzica85 wrote:
    Hi everybody,
    I want to overload a method so it can take either 2 Actions, 2 ActionListeners, or one of each. The issue with this is that I'd have to make 4 overloaded signatures and almost identical methods, like this:
    method( action, action )
    method( action, listener )
    method( listener, action )
    method( listener, listener )
    Well, if you want to support that then you are just going to have to do suffer through it, the only shortcut I can recommend is that your method(action, listener) and method(listener, action) are the same so you only have to implement 1 and just use the other as a entry point to call the one you wish to contain the code.

  • What's the difference between Action Objects and ActionListeners

    Hello,
    in the Introduction to JSF two ways handling action are demonstrated. One way is "Combining Component Data and Action Object" the other way is "Handling Events."
    What's the difference? When do I use this or that way?
    The one thing I understand is when using "Combining Component Data and Action Objects" I use the default ActionListener and I may directly access the input values.
    TIA,
    Juergen

    Hi Juergen,
    The bottomline is 'not much', only in the details to these two approaches differ.
    The ActionListener object will have events broadcast to it for a particular object by the event mechanism in JSF. You can add one to a specific component via the f:action_listener tag. This will have your listener getting events during specific points in the request processing lifecycle (you specify via your return value for getPhaseId).
    The Action class that an actionRef reference refers to an Action that will be invoked during the invoke application phase. (see pg 71).
    I would recommend using actionRefs and Action objects wherever possible and going to ActionListeners only when you need notifications during a specific phase of the request/response cycle.
    Hope this helps,
    -bd-
    http://bill.dudney.net

  • JMenus and applets

    does anyone know if you can use JMenus with applets because i tried it and it compiles ok but when run says something about the applet not being initiated.
    thanks

    Yes you can use them. "Applet not inited" error comes from something else, check your codebase, try to run it in appletviewer, check out the Java console for errors.

  • JButton and ActionListeners

    I would like to change the ActionListeners registered to a JButton when the user clicks on a JButton. However there is a problem with this (see below). A typical procedure would go as follows:
    1) User clicks on button
    2) Get the appropriate listener from a list of listeners
    3) Remove the current listener(s) from the button
    4) Add the new listener(s)
    However, for some reason, the instructions of the new listener class are getting called. Is there a way of preventing this and if so, can someone please guide me in the right direction?
    Stephen

    It does not seem to happen like what you have said. Please see the test code below:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
    public class Temp extends JFrame {
         private JButton b = null;
         public Temp() {
              super("Test");
              setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              getContentPane().setLayout(new BorderLayout());
              b = new JButton("Click Me!");
              b.addActionListener(new ActionListener1());
              getContentPane().add(b);
              pack();
              setVisible(true);
         class ActionListener1 implements ActionListener {
              public void actionPerformed(ActionEvent evt) {
                   System.out.println("Action Listener 1");
                   b.removeActionListener(this);
                   b.addActionListener(new ActionListener2());
         class ActionListener2 implements ActionListener {
              public void actionPerformed(ActionEvent evt) {
                   System.out.println("Action Listener 2");
         public static void main(String args []) {
              new Temp();
    }The first time you click the button action listener 1 code gets executed. From the second time onwards the action listener 2 code gets executed.
    Hope this helps
    Sai Pullabhotla

  • Reflection, menus, and ActionListeners

    Hi everybody,
    I was doing some reading in the Java APIs and came across reflection, and it seemed like a good idea for some menus I'm implementing, but I'm not fully sure, so I'd like some advice, please, if anyone would be willing.
    I have a lot of menus on a menu bar, and each menu item on the bar pops up a different kind of window (each window is distinct and has its own constructor; they're all encapsulated objects). My original idea was to add actionListeners to each menu item like so:
    for each item:
    item.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    // the constructor for a particular window
    });The issue with that approach, as I've learned, is that this repetitive call, while it works, bloats my classes, makes them almost impossible to read, and involves a lot of cutting and pasting so it's prone to mistakes.
    So...then I stumbled on reflection. From reading the API, it appears (please correct me if I'm wrong) that you can get particular methods and constructors from classes using reflection, and then call them. This would allow me to make two small overloaded methods, to cut down on the amount of code, like so:
    public JMenuItem addConstructorAction( JMenuItem item, Constructor c ) {
    item.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    c.newInstance();
    return item;
    public JMenuItem addConstructorAction( JMenuItem item, Method m ) {
    item.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    m.invoke( params );
    return item;
    }So, am I right that this is how reflection works, or have I misread something? If anyone could give me advice on how to use this properly or an alternate idea, I'd appreciate it.
    Thanks,
    Jezzica85
    PS -- Sorry about the indenting on the code examples; I don't know why they didn't indent, hopefully they're still readable.
    Edited by: jezzica85 on Jan 14, 2009 7:04 AM

    Well, I didn't think this would turn into a SSCCE situation, but that's OK-- I probably should have assumed that to begin with. Anyway, here's a small program that shows what I'm talking about:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    public class MenuTest {
         public static void main(String[] args) {
              try {
                   JFrame frame = new JFrame();
                   frame.setSize( 300, 300 );
                   frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                   JMenuBar test = new JMenuBar();
                   JMenu menu = new JMenu( "myTest" );
                   JMenuItem item = new JMenuItem( "TestWindow test" );
                   item.addActionListener( new ActionListener() {
                        public void actionPerformed( ActionEvent ex ) {
                             new TestWindow( "TestWindow" );
                   menu.add( item );
                   JMenuItem item2 = new JMenuItem( "TestWindow2 test" );
                   item2.addActionListener( new ActionListener() {
                        public void actionPerformed( ActionEvent ex ) {
                             new TestWindow2( "TestWindow2" );
                   menu.add( item2 );
                   test.add( menu );
                   frame.setJMenuBar( test );
                   frame.setVisible( true );
              } catch( Exception e ) {
                   e.printStackTrace();
                   System.exit( -1 );
    class TestWindow extends JDialog {
         public TestWindow( String title ) {
              setTitle( title );
              setSize( 300, 300 );
              setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
              setVisible( true );
    class TestWindow2 extends JDialog {
         public TestWindow2( String title ) {
              setTitle( title );
              add( new JLabel( "blah" ) );
              setSize( 300, 300 );
              setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
              setVisible( true );
    }Basically, each menu item calls up a different kind of window (these are very simplified obviously). In my real application I have more than 40 different windows (because the application needs to do a lot), so repeating the actionListener code can get tedious. It doesn't look like much here, but 9 lines of code (including whitespace for readability) times at least 40 menu items is more than 350 lines of code for menu items alone. That's what I'm trying to avoid.
    Thanks,
    Jezzica85

  • JMenus and JMenuItems

    i have a JMenu called jMenu2, in its action performed method i would like it to add to the bottom of its list a new JMenuItem while the program is running.
    private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
           jMenu2.add("hi");
    }so why can i not see this new menu item? i have tried many refreshing methods from JMenu with no luck

    I wouldn't expect it to show up immediately if it's being added as a result of clicking another item on the same JMenu, only next time you pull that JMenu down. If that doesn't happen, I would try creating a new JMenuItem("hi"), call setVisible(true) on it, and then add it to the JMenu. You shouldn't need to revalidate or repaint anything.

  • JMenus and KeyboardFocusManager

    I'd like my menus to do different things depending on where focus was before the menu was invoked (ctrl/C should copy the appropriate string or object, for example). If the menu is invoked via the mouse, rather than a keyboard shortcut, it has the focus, so I can't simply use the current focus owner.
    Adding a PropertyChangeListener for "focusOwner" and retaining previous focus seems kind of indirect, so I'm hoping there is a simpler and standard way of doing this. Could someone point me in the right direction?

    What I'm trying to do is have the actions invoked when a menu is selected via the MOUSE understand where focus was before the menu was invoked, since using the mouse causes focus to move to the menu.

  • PJC and actionlisteners

    Hi,
    I'm developing a bean, and want to use it as a pjc in a form, using the VBean package of Forms.
    The component extends VBean and implements a DocumentListener.
    I instansiate a JPanel and add the component.
    The set_property methods works fine and the document listener is working.
    The component that I'm using extends JFrame and implements ActionListener, WindowListener, DocumentListener, FocusListener.
    Problem is: The PJC is not listening to /firing various events
    How do I get the PJC to listen to actions like keyboard short-cuts that when running the bean outside forms works as expected. What am I missing ?
    Any thoughts much appreciated
    Thanks

    I havn't implemented the addtional listeners for the class that extends vbean
    This is what I've got so far:
    public class mybean extends VBean implements DocumentListener
    private Component mComp = new JPanel()
    /*** Constructor*/
    public mybean() {
    super();
    eclass = new Eclass(this);
    // this class has all the listeners implemented
    try {
    mComp = (JPanel) eclass.getedComponent();
    } catch (Exception ee) {
    ee.printStackTrace();
    How do I get the listeners in the class I instansiate active in the vbean implementation

  • Rootpanes and ActionListeners

    Hi,
    I have an Actionlistener on the rootpane of a JFrame to catch when the user hits 'ESC' (using 'registerKeyboardAction'). This works ok no matter what Swing object has the focus, except when a JTable or JTree has the focus. Is this a bug and is there a work around?

    Figured it out.
    Use the following to deactivate the ESC key for JTables and JTrees
    jTable1.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).getParent().remove(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false));
    jTree1.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).getParent().remove(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false));

  • Executable JARs, JFrames, and ActionListeners

    I'm fairly new to Java, and am currently attempting to create my first JAR executable file. I am using the Eclipse IDE and JDK 5.0. I use Eclipse's export option to create a JAR executable. When the JAR is exectued, it successfully creates and displays the intial JFrame; however, a second JFrame that is created by an ActionListener on a JButton, and is based on input from a JTextArea, is never created when the JButton is clicked on. The second JFrame also accesses txt files based upon the JTextArea input, each of which are contained inside of the JAR file as well. The program compiles and runs successfully when ran as a Java Application or SWT Application in Eclipse, so I am unsure why the export ability is not working.
       public FrameClass1()
          inputTextArea = new JTextArea(29, 95);
          inputTextArea.setFont(new Font("Courier New", 0, 12));
          //Adds the inputTextArea to a JScrollPane, and then adds it to the frame's Center
          buttonPanel = new JPanel();
          submitButton = new JButton("Submit");
          submitButton.addActionListener(new
             ActionListener()
                public void actionPerformed(ActionEvent event)
                   String textInput = inputTextArea.getText();
                   //Output frame creation
                   FrameClass2 outputFrame = new FrameClass2(textInput);
                   outputFrame.setVisible(true);
          buttonPanel.add(submitButton);
          //buttonPanel is added to the frame's south portion
       }Any suggestions as to why the second frame class is not being generated and how to fix it are greatly appreciated. Thanks in advance.

    To capture the error messages, I've used this try-catch statement, though as I posted earlier, I am not familar with JARs. I have identified that my error is a IllegalArguementException, though I cannot find a precise location of where this exception occurs, as the exceptionOutputFrame is never populated with the stack trace. I've tried both e.getMessage() (below) and e.printStackTrace().toString() with both JLabels and JTextAreas, but the JPanel will not populate with data. Could anybody recommend a good method of outputing the stack trace? In addition, could someone explain the use of getResourceAsStream a little more? Should I be using the class ClassLoader's method or the interface ServletContext? I'm a little rough around the edges with the idea of a resource, so does anybody have a good online reference that I could look at?
         try
              // Insert output frame creation here.
              Class2 outputFrame = new Class2(textInput);
              outputFrame.setVisible(true);
         catch(Exception e)
              JFrame exceptionOutputFrame = new JFrame(e.getClass().getName());
              JScrollPane exceptionOutputScrollPane = new JScrollPane();
              JPanel exceptionOutputPanel = new JPanel();
              JTextArea exceptionOutputLabel = new JTextArea(e.getMessage());
              exceptionOutputPanel.add(exceptionOutputLabel);
              exceptionOutputScrollPane.add(exceptionOutputPanel);
              exceptionOutputFrame.add(exceptionOutputScrollPane);
              exceptionOutputFrame.setSize(exceptionOutputLabel.getWidth(), DEFAULT_HEIGHT);
              exceptionOutputFrame.setVisible(true);
         catch(Error e)
              JFrame errorOutputFrame = new JFrame(e.getClass().getName());
              JScrollPane errorOutputScrollPane = new JScrollPane();
              JPanel errorOutputPanel = new JPanel();
              JTextArea errorOutputLabel = new JTextArea(e.getMessage());
              errorOutputPanel.add(errorOutputLabel);
              errorOutputScrollPane.add(errorOutputPanel);
              errorOutputFrame.add(errorOutputScrollPane);
              errorOutputFrame.setSize(errorOutputLabel.getWidth(), DEFAULT_HEIGHT);
              errorOutputFrame.setVisible(true);
         }

  • WindowListeners and actionListeners

    Hey guys, thanks for all your help the other night with my actionListner problem. This time around, it isn't as much a problem of getting the program to work, it's just a matter of how to do things. I have an actionListener created like this:
    button.addActionListener(mainFrame);
    public void actionPerformed (ActionEvent e) {
    How can I create a windowListener in this manner instead of this:
              mainFrame.addWindowListener(new WindowAdapter() {
                   public void windowClosing(WindowEvent e) {
                        System.exit(0);
              } );I hope somebody can help! thanks for your time and input.
    Tom

    You do it exactly the same way, except whatever class is implementing WindowListener will need to add a lot more methods. Extending WindowAdapter allows you to over-ride only the methods you need to, like windowClosing.

  • Problem with Alt , JMenu and Mnemonics

    I have built an application with a JMenuBar containing several JMenu's, the first of which is a File menu. All JMenus and JMenuItems have mnemonics associated with them. When I press the alt key the underlines show and focus appears to be given to the File menu (though it has not dropped down). If I then press a key, for example 'S', that is a mnemonic of the 'Save' jmenuitem in the File menu, the Save action is invoked. This should not occur because the menu has not opened yet.
    The behavior is what I'd expect had an accelerator (Alt+S) been defined for the Save menu item. But I have not defined any accelerators.
    Why does this happen and more importantly, is there a work around?

    Except for the 1st line in jbinit() it's all pretty standard stuff. Here's a snippet of the code:
    public class MainFrame extends JFrame implements ChangeListener {
    JMenuBar jMenuBar1 = new JMenuBar();
    JMenu jMenuFile = new JMenu();
    JMenuItem jMenuFileNew = new JMenuItem();
    JMenuItem jMenuFileSave = new JMenuItem();
    JMenu m_editMenu = new JMenu();
    JMenuItem m_editCutMenuItem = new JMenuItem();
    JMenuItem m_editCopyMenuItem = new JMenuItem();
    JMenuItem m_editPasteMenuItem = new JMenuItem();
    public MainFrame() {
    jbInit();
    //Component initialization
    private void jbInit() {
    getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    KeyStroke.getKeyStroke(KeyEvent.VK_ALT, Event.ALT_MASK, false), "repaint");
    // file menu
    jMenuFile.setText("File");
    jMenuFile.setMnemonic(KeyEvent.VK_F);
    jMenuFileNew.setText("New...");
    jMenuFileNew.setMnemonic(KeyEvent.VK_N);
    jMenuFileNew.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    jMenuFileNewSpecial_actionPerformed(e);
    jMenuFileSave.setText("Save");
    jMenuFileSave.setMnemonic(KeyEvent.VK_S);
    jMenuFileSave.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    jMenuFileSaveSpecial_actionPerformed(e);
    jMenuFile.add(jMenuFileNew);
    jMenuFile.add(jMenuFileSave);
    // edit menu
    m_editMenu.setText("Edit");
    m_editMenu.setMnemonic(KeyEvent.VK_E);
    m_editCutMenuItem.setText("Cut");
    m_editCutMenuItem.setMnemonic(KeyEvent.VK_T);
    m_editCutMenuItem.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    editCutMenuItem_actionPerformed(e);
    m_editCopyMenuItem.setText("Copy");
    m_editCopyMenuItem.setMnemonic(KeyEvent.VK_C);
    m_editPasteMenuItem.setText("Paste");
    m_editPasteMenuItem.setMnemonic(KeyEvent.VK_P);
    m_editMenu.add(m_editCutMenuItem);
    m_editMenu.add(m_editCopyMenuItem);
    m_editMenu.add(m_editPasteMenuItem);
    jMenuBar1.add(jMenuFile);
    jMenuBar1.add(m_editMenu);
    this.setJMenuBar(jMenuBar1);
    etc...
    Pressing Alt+S invokes the action listener for the jMenuFileSave menu item. It should do nothing since there is no top level menu with a mnemonic of 'S'.

  • Remove padding on menu bar and toolbar

    Hi,
    Is there a way of removing the extra margin at the bottom of JMenus and JToolBars?
    I've highlighted the margins in red so you can see them better:
    http://img410.imageshack.us/my.php?image=paddingapp4rm.png
    Also, it it possible to remove the drag area on the toolbar and the square borders around the images in the toolbar?
    Here's the code that creates the menus and toolbars:
       public SnakeLadder()
          super("Snakes & Ladders");
          setLayout(new FlowLayout());
          JMenuBar menuBar = new JMenuBar();
          setJMenuBar(menuBar);
          menuBar.add(fileMenu());
          menuBar.add(gameMenu());
          menuBar.add(helpMenu());
          JToolBar toolBar = new JToolBar();
          add(toolBar,BorderLayout.PAGE_START);
          toolBar.setPreferredSize(new Dimension(WIDTH,30));
          toolBar.add(newGameButton());
          toolBar.add(saveGameButton());
          toolBar.add(loadGameButton());
          JPanel gamePanel = new JPanel();
          add(gamePanel,BorderLayout.CENTER);
          gamePanel.setPreferredSize(new Dimension(WIDTH,HEIGHT));
          gamePanel.setBackground(new Color(212,208,200));
          pack();
       }Thanks

    I suggest you to try your question at Java product
    forum at http://forum.sun.com/jive/index.jspa under
    Java Tools section. You may try either "Sun Java
    Studio Creator" or "Sun Java Studio Enterprise" forumWould you please stop posting the same silly message? The post is not related to what you are mentioning.

Maybe you are looking for