Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to make application use getModified

Posted on 2009-05-07
6
Medium Priority
?
476 Views
Last Modified: 2013-11-23
I have an application that identifies which button on the mouse you clicked but I need to use the getModified method with an inputEvent object.
The actual question is : write an application that lets you determine the integer value returned by the inputEvent method getModifiers() when you click your left or right mouse button on a JFrame.
I don't know how to change the event code to accomplish this.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JLeftOrRight extends JFrame implements MouseListener
{
 final int MAX = 20;
 String message[] = new String[MAX];
 int msgCount = 0;
 final int STARTX = 10;
   final int STARTY = 20;
   int x, y;
 
 
 
 public JLeftOrRight()
   {
      setTitle("LeftOrRight");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      addMouseListener(this);
   }
 public void mouseClicked(MouseEvent e)
   {
      	 int whichButton = e.getButton();
      String msg;
      if(msgCount == MAX)
        clearScreen();
      message[msgCount] = "You pressed the mouse.";
      if (whichButton == MouseEvent.BUTTON1)
        msg = "button 1.";
      else 
        msg = "button 2.";
      
      message[msgCount] = message[msgCount] +
         " You used " + msg;
	   ++msgCount;
      repaint();
 
		 
    			}
			
		 public void mouseEntered(MouseEvent e)
   {
      if(msgCount == MAX)
        clearScreen();
       message[msgCount] = "You entered the frame";
       ++msgCount;
       repaint();
   }
  public void mouseExited(MouseEvent e)
   {
             if(msgCount == MAX)
        clearScreen();
      message[msgCount] = "You exited the frame";
	 ++msgCount;
      repaint();
 
	}
	 public void mousePressed(MouseEvent e)
   {
   }
 
   public void mouseReleased(MouseEvent e)
   {
   }
	  public void paint(Graphics g)
   {
      super.paint(g);
final int STARTX = 10;
   final int STARTY = 20;
   int x, y;
      x = STARTX;
      y = STARTY;
      for(int a = 0; a < msgCount; ++a)
         g.drawString(message[a], x, y += 20);
   }
 
   public void clearScreen()
   {
      msgCount = 0;
      for(int a = 0; a < MAX; ++a)
         message[a] = " ";
      repaint();
   }
	
			
	 public static void main(String[] args)
   {
      JLeftOrRight mFrame = new JLeftOrRight();
      final int WIDTH = 750;
      final int HEIGHT = 500;
      mFrame.setSize(WIDTH, HEIGHT);
 
      mFrame.setVisible(true);
   }
}

Open in new window

0
Comment
Question by:mabcoleman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24332558
Why do you need to change it?
Questions seems to just asking you to display what it is

int modifier = event.getModifiers();

0
 

Author Comment

by:mabcoleman
ID: 24332626
I thought I was supposed to use that method instead of the code I have used to determine which button is used
0
 
LVL 92

Expert Comment

by:objects
ID: 24332632
perhaps it means something like this:

      if (whichButton == MouseEvent.BUTTON1)
        msg = "button 1. getModifier()="+modifier;
      else
        msg = "button 2. getModifier()="+modifier;

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 24333673
Try the following to print your modifiers - note that a small number are synonyms (check constants):
import static java.awt.event.InputEvent.*;
 
 
public class EventPrinter {
 
    public static String eventToString(java.awt.event.InputEvent e) {
	final String NEWLINE = System.getProperty("line.separator");
	int mods = e.getModifiers();
	StringBuilder sb = new StringBuilder();
	for (int i = SHIFT_MASK; i <= ALT_GRAPH_DOWN_MASK; i <<= 1) {
	    if ((mods & i) == SHIFT_MASK) sb.append("SHIFT_MASK").append(NEWLINE);
	    if ((mods & i) == CTRL_MASK) sb.append("CTRL_MASK").append(NEWLINE);
	    if ((mods & i) == BUTTON3_MASK) sb.append("BUTTON3_MASK").append(NEWLINE);
	    if ((mods & i) == META_MASK) sb.append("META_MASK").append(NEWLINE);
	    if ((mods & i) == ALT_MASK) sb.append("ALT_MASK").append(NEWLINE);
	    if ((mods & i) == BUTTON2_MASK) sb.append("BUTTON2_MASK").append(NEWLINE);
	    if ((mods & i) == BUTTON1_MASK) sb.append("BUTTON1_MASK").append(NEWLINE);
	    if ((mods & i) == ALT_GRAPH_MASK) sb.append("ALT_GRAPH_MASK").append(NEWLINE);
	    if ((mods & i) == SHIFT_DOWN_MASK) sb.append("SHIFT_DOWN_MASK").append(NEWLINE);
	    if ((mods & i) == CTRL_DOWN_MASK) sb.append("CTRL_DOWN_MASK").append(NEWLINE);
	    if ((mods & i) == META_DOWN_MASK) sb.append("META_DOWN_MASK").append(NEWLINE);
	    if ((mods & i) == ALT_DOWN_MASK) sb.append("ALT_DOWN_MASK").append(NEWLINE);
	    if ((mods & i) == BUTTON1_DOWN_MASK) sb.append("BUTTON1_DOWN_MASK").append(NEWLINE);
	    if ((mods & i) == BUTTON2_DOWN_MASK) sb.append("BUTTON2_DOWN_MASK").append(NEWLINE);
	    if ((mods & i) == BUTTON3_DOWN_MASK) sb.append("BUTTON3_DOWN_MASK").append(NEWLINE);
	    if ((mods & i) == ALT_GRAPH_DOWN_MASK) sb.append("ALT_GRAPH_DOWN_MASK").append(NEWLINE);
	}
	return sb.toString().trim();
    }
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24333796
mabcoleman, as a point of procedure, you should normally simply accept comments as answers rather than closing the question. That's normally done when you're not awarding point
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24333803
Oh - i think you did now - perhaps you made an adjustment? :-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question