8 Replies - 526 Views - Last Post: 13 August 2017 - 02:29 PM Rate Topic: -----

#1 King Hebes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-January 16

Mnemonic Not Working

Posted 13 August 2017 - 01:42 AM

Hello. I have set mnemonics for 2 buttons in my program. When I press alt while the program is running the letters I chose for the mnemonics become underlined, but when pressing the assigned mnemonic key(ex. 's' for stop or 'p' for play) with or without alt being held, I get no action. It does not fire off the button. Here is the code. Button code begins line 50.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.AudioClip;
import java.net.URL;

public class JukeBoxControls extends JPanel
{
   private JComboBox musicCombo;
   private JButton stopButton, playButton;
   private AudioClip[] music;
   private AudioClip current;
   
   //Sets up the GUI for the juke box.
   
   public JukeBoxControls()
   {
      URL url1, url2, url3, url4, url5, url6;
      url1 = url2 = url3 = url4 = url5 = url6 = null;
      
      //Obtain and store the audio clips to play
      
      try
      {
         url1 = new URL("file", "localhost", "westernBeat.wav");
         url2 = new URL("file", "localhost", "classical.wav");
         url3 = new URL("file", "localhost", "jeopardy.au");
         url4 = new URL("file", "localhost", "newAgeRythm.wav");
         url5 = new URL("file", "localhost", "eightiesJam.wav");
         url6 = new URL("file", "localhost", "hitchcock.wav");
      }
      catch (Exception exception) {}
      
      music = new AudioClip[7];
      music[0] = null; //corresponds to "Make a Selection..."
      music[1] = JApplet.newAudioClip(url1);
      music[2] = JApplet.newAudioClip(url2);
      music[3] = JApplet.newAudioClip(url3);
      music[4] = JApplet.newAudioClip(url4);
      music[5] = JApplet.newAudioClip(url5);
      music[6] = JApplet.newAudioClip(url6);
      
      //Create the list of strings for the combo box option
      String[] musicNames = {"Make A Selection...", "Western Beat", "Classical Melody",
                     "Jeopardy Theme", "New Age Rythm", "Eighties Jam", "Alfred Hitchcock's Theme"};
      
      musicCombo = new JComboBox(musicNames);
      musicCombo.setBackground(Color.cyan);
      
      //Set up the buttons
      playButton = new JButton("Play", new ImageIcon ("play.gif"));
      playButton.setBackground(Color.cyan);
      playButton.setMnemonic('p');
      stopButton = new JButton("Stop", new ImageIcon ("stop.gif"));
      stopButton.setBackground(Color.cyan);
      stopButton.setMnemonic('s');
      
      //Set up this panel
      setPreferredSize(new Dimension(250, 100));
      setBackground(Color.cyan);
      add(musicCombo);
      add(playButton);
      add(stopButton);
      
      musicCombo.addActionListener(new ComboListener());
      stopButton.addActionListener(new ButtonListener());
      playButton.addActionListener(new ButtonListener());
      
      current = null;
   }
   
   //Represents the action listener for the combo box.
   
   private class ComboListener implements ActionListener
   {
      //Stops playing the current selection (if any) and resets the current selection
      //to the one chosen.
      
      public void actionPerformed(ActionEvent event)
      {
         if (current != null)
            current.stop();
         current = music[musicCombo.getSelectedIndex()];
      }
   }
   
   //Represents the action listener for both control buttons.
   
   private class ButtonListener implements ActionListener
   {
      //Stops the current selection (if any) in either case.
      //If the play button was pressed, start playing it again.
      
      public void actionPerformed(ActionEvent event)
      {
         if (current != null)
            current.stop();
            
         if (event.getSource() == playButton)
            if (current != null)
               current.play();
      }
   }
}


This post has been edited by King Hebes: 13 August 2017 - 01:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Mnemonic Not Working

#2 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,819
  • Joined: 25-December 13

Re: Mnemonic Not Working

Posted 13 August 2017 - 03:17 AM

How can the code be compiled and executed for testing?

Quote

It does not fire off the button

How do you know? How are you trying to debug the code to see where it executes? I don't see any print statements to show where the code is executing.

This post has been edited by NormR: 13 August 2017 - 03:17 AM

Was This Post Helpful? 0
  • +
  • -

#3 King Hebes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-January 16

Re: Mnemonic Not Working

Posted 13 August 2017 - 12:39 PM

Oh I'm using a JFrame to launch the program.

import javax.swing.*;

public class JukeBox
{
   //Creates and displays the controls for a juke box.
   
   public static void main(String[] args)
   {
      JFrame frame = new JFrame("Java Juke Box");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new JukeBoxControls());
      
      frame.pack();
      frame.setVisible(true);
   }
} 


Was This Post Helpful? 0
  • +
  • -

#4 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,819
  • Joined: 25-December 13

Re: Mnemonic Not Working

Posted 13 August 2017 - 12:49 PM

Have you tried debugging the code to see where it is executing so you can isolate the problem?

For example add some print statements in the listeners to see if they are being executed.

This post has been edited by NormR: 13 August 2017 - 12:50 PM

Was This Post Helpful? 1
  • +
  • -

#5 King Hebes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-January 16

Re: Mnemonic Not Working

Posted 13 August 2017 - 01:13 PM

well the buttons work fine. But trying to use the button via the mnemonic does not work. the listener does not activate. it's like the button is not being pressed. I've never used a mnemonic before. From what I can read it sounds as simple as simply setting one to the component. Is there something I am doing incorrectly?
Was This Post Helpful? 0
  • +
  • -

#6 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,819
  • Joined: 25-December 13

Re: Mnemonic Not Working

Posted 13 August 2017 - 01:27 PM

Quote

use the button via the mnemonic does not work. the listener does not activate.

The code calls the listener for me when I press: ALT and s or ALT and p

How did you debug the code to see if the listener was called?
Was This Post Helpful? 0
  • +
  • -

#7 King Hebes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-January 16

Re: Mnemonic Not Working

Posted 13 August 2017 - 01:45 PM

I added a println in here. I'm wondering if it's a mac issue?

private class ButtonListener implements ActionListener
   {
      //Stops the current selection (if any) in either case.
      //If the play button was pressed, start playing it again.
      
      public void actionPerformed(ActionEvent event)
      {
         if (current != null)
            current.stop();
            
         if (event.getSource() == playButton)
            if (current != null)
               current.play();
      
      System.out.println("THis should fire if the listener is working.");
      }
   }


Was This Post Helpful? 0
  • +
  • -

#8 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,819
  • Joined: 25-December 13

Re: Mnemonic Not Working

Posted 13 August 2017 - 02:15 PM

Does the program have the focus?
Click on it before trying the button presses.

I'm on a Win10 system

This post has been edited by NormR: 13 August 2017 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#9 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1565
  • View blog
  • Posts: 3,513
  • Joined: 05-April 11

Re: Mnemonic Not Working

Posted 13 August 2017 - 02:29 PM

https://developer.ap...ntegration.html Goto the Mnemonics section

Quote

Using mnemonics is discouraged in OS X, because mnemonics violate the principles of macOS Human Interface Guidelines. If you are developing a Java application for multiple platforms and some of those platforms recommend the use of mnemonics, just include a single setMnemonics() method that is conditionally called (based on the platform) when constructing your interface.


They then continue to suggest an alternative and show you how :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1