Mouse click to boolean - java

Hello from novice java developer, I created a MouseListener and MouseAdapter in a thread to control mouse action for mouse pressed, released and drag action. Each action will do specific things but i could not assign each MouseEvent e from each action to a variable.
So, how can deal with this problem? I also wonder if the method parameter MouseEvent e is specific to each method?
Here is my code:
Thread thread = new Thread() {
public void run() {
addMouseListener(new MouseAdapter() {
//#override deleted because i want to use e as a different action.
public void mouseaction(MouseEvent e) {
/* In here i want to control MouseEvent e action
(drag, pressed and released) and do specific things in with e event
and if e changes state should be changed in code during while(true) */
}
}
}

You can get all this information from the mouseEvent by calling the method getModifiersEx(), for example:
int eventType = e.getModifiersEx();
if (eventType & MOUSE_DRAGGED > 0) {
// Code to be executed when mouse is dragged
}
if (eventType & MOUSE_PRESSED > 0) {
// Code to be executed when mouse button is pressed
}
...
Note that the eventType is a bit field where multiple bits can be activated simultaneously.

//#override deleted because i want to use e as a different action.
public void mouseaction(MouseEvent e)
You can't just make up method names. You need to implement the methods of the listener. You need to handle the mousePressed, mouseReleased methods separately. For the mouseDragged you need to implement the MouseMotionListener.
Read the section from the Swing tutorial on Implementing Listener. You can find sections on:
How to Implement a MouseListener
How to Implement a MouseMotionListener
which both contain working examples.

I'll address this concern:
I also wonder if the method parameter MouseEvent e is specific to each method?
Every time this method is invoked by Swing, a new Event is generated. Your #Override annotation makes no difference.
So when user clicks somewhere, a MouseEvent N°2556 is generated for it, and the method is invoked with that event as a parameter.
When user drags the mouse away, a MouseEvent N°2557 is generated, and the method is again invoked with this new event as a parameter.
More broadly: All those MouseEvents will always be different instances. They are immutable, as well.
This means if you want to persist some information for your game loop to see, you need to store the relevant conditions in a field somewhere. And you won't be able to access it from an anonymous class because you won't have a handle to it. Here is a quick and dirty example (shameless reuse of #FrankPuffer's code):
public class MyMouseAdapter extends MouseAdpater {
public boolean isMousePressed = false; // This info is persisted here
public void mouseaction(MouseEvent e) { // This is only triggered upon user input
int eventType = e.getModifiersEx();
if (eventType & MouseEvent.MOUSE_PRESSED) {
isMousePressed = true;
}
if (eventType & MouseEvent.MOUSE_RELEASED) {
isMousePressed = false;
}
}
}
public static void main(String[] argc){
// Before the game loop:
MyMouseAdapter myAdapter = new MyMouseAdapter();
jpanel.addMouseListener(myAdapter);
// In the game loop
while(true) {
if(myAdapter.isMousePressed) { // This info is available anytime now!
// Do something
}
}
}

Related

Explain the KeyListener in Java

I am someone new to Java and working with the robot class. I would like to make an emergency stop function for my robot so when it does something wrong I can make the automation end. While robot so far has been easy the key listener escapes me, please explain in a "my first keylistener" style, thank you!
Here is what I have so far:
public static void keyboard(String input, HWND window) throws Exception {
System.out.println("Keyboard Typing:\n" + input);
//This is just to stop the error, but I don't know how
//to actually listen
KeyEvent e = null;
for (int i = 0; i < input.length(); i++) {;
keepFocus(window);
if(stopBot(e /*How to pass a key press*/) == true){
break;
}
char c = input.charAt(i);
keyboardHandler(c);
Thread.sleep(80);
}
}
public static boolean stopBot(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_END){
return true;
}
return false;
}
There is other code but this is all that is relevant to my problem
The easiest way I could think about implementing a key event is by making your current class extend a Key Listener class. Basically, you want your current class to be listening for keystrokes in the background. You'll start by adding the implementation to your class name:
public YourClassName implements KeyListener {
}
Now, your class is able to listen for key strokes in the background. You'll now need to add a listener in your code and then give it instructions on what to do when hit. You'll add the listener by invoking the following method:
this.addKeyListener()
You can also replace "this" with any other instantiated object capable of handling action events. Now, you just need to be able to instruct the program on what to do when the listener picks up a key stroke event. The key listener will evoke one method from the KeyListener class you are extending: keyPressed(KeyEvent e), keyRelease(KeyEvent e), or keyTyped(KeyEvent e). These are the functions that will then run whatever code you'd like when a key-event is picked up and passed through the function. You will need to override these methods like so:
#Override
public void keyReleased(KeyEvent e) {
//whatever you want to happen in the case of
this event, I assume stop your robot
}
So, as you can see, you don't need to worry about invoking your own KeyEvent, Java's addKeyListener will take care of this as long you've extended the class and given instructions on what to do for each event! Hope this helps!

How do I register an object's event listener to an event handler outside its class?

I have 2 classes, one JFrame class and one JButton class called LightsOutButton. The JFrame class has a collection of these LightsOutButton objects. I need to do actions when the individual buttons are pressed, but I can't access the JFrame class's methods from the event handler in my LightsOutButton class.
It seems like the best way to work around this would be to register each button with the JFrame class's event handler. How would I do that?
`// this is my button object. It has registered its action listener to itself(which I want to change)
public class LightsOutButton extends JButton implements ActionListener {
private boolean onOrOff; // true = on, false = off
private int iCoord;
private int jCoord;
public LightsOutButton() {
super.addActionListener(this);
onOrOff = false;
super.setContentAreaFilled(false);
}
}
// this is my JFrame class. This is the class that I want to handle each button object's events
public void actionPerformed(ActionEvent e) {
// Get the button that was clicked
JButton button = (JButton)e.getSource();
if(button == newGame) {
reset();
}
if(button == manual) {
if (manualMode == false) {
manualMode = true;
}
if (manualMode == true) {
manualMode = false;
}
}
// this is the implementation that I'm wishing for here:
if(button = LightsOutButton){
// do something
}
}`
Basically as I understand it, you have a group of LightsOutButton which have been added to a JFrame. In LightsOutButton you are trying to perform some action, but need to access the JFrame in some way to achieve it...
I would say you have the wrong concept. Sure, there's no reason why LightsOutButton can't have it's own event handlers to manage it's internal state, but beyond that, it shouldn't be concerned about anybody else.
It seems like the best way to work around this would be to register each button with the JFrame class's event handle
Would be an accurate assessment. How you do it will depend on how you've managed your code, but probably the simplest would be to add the LightsOutButtons to some kind of List or array and add and register the handler from within a loop.
Identification of each light will be your next problem, but you could use the reference from the List/array, or set the name property of each light or the actionCommand property depending on your needs.
If each light has a "particular" task to do, you could consider using a Action or specialised ActionListener for each one, but that comes down to needs
in your JFrame class, when you add you buttons
LightsOutButton b = new LightsButton(..);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//you have acces to JFrame methods ..
});

How to wait for a mouse click

class GameFrameClass extends javax.swing.JFrame {
public void MyFunc()
{
UserButton.setText(Str);
UserButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
UserButtonActionPerformed(evt);
}
});
getContentPane().add(UserButton);
}
private void UserButtonActionPerformed(java.awt.event.ActionEvent evt) {
//print some stuff after mouse click
}
}
In someother class i define this function
void functionAdd()
{
GameFrameClass gfc = new GameFrameClass()
gfc.MyFunc()
System.out.println("PRINT THIS AFTER MOUSE CLICK")
}
If someone can look into this code. I want to wait on the mouse click . Is there a way i can print the line System.out.println("PRINT THIS AFTER MOUSE CLICK") after the mouse is being clicked . For now this happens immediately and i am not able to wait for the mouse click . Is there a way of doing it ? Apart from doing it inside the function UserButtonActionPerformed() . Please let me know .
This is a really "bad" way to do it...
private void UserButtonActionPerformed(java.awt.event.ActionEvent evt) {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
System.out.println("PRINT THIS AFTER MOUSE CLICK");
removeMouseListener(this);
}
});
}
A better way would be to have a flag in the actionPerformed method that would "enable" a mouse listener (which you added earlier). This listener would check the flag on each click and when set to true it would flip the flag (to false) and process the event...
It's hard to tell from the wording, but I assume he or she simply wants to execute code after the button is triggered (and not actually wait). For that, you need to add the code inside the method being invoked inside the actionlistener (in this case UserButtonActionPerformed).
So:
private void UserButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(...);
}
Also, following the Java coding conventions will help people answering your questions in the future.
You can always wait in UserButtonActionPerformed by defining it in the same class. If that is the case then you should not have the problem you are facing
Events are managed on a different thread which is the event dispatching thread, they are not managed by the thread that is executing your code (which presumably is the main thread).
This means that you can attach listeners to GUI elements but the only thing you can do to "wait" for the click is to execute the code inside the actionPerformed callback.
There is no way to pause the execution since the addActionListener doesn't do anything to effectively catch the event, it just adds the listener. Theoretically you could lock the main thread waiting to be notified by the event dispatch one but that would just be bad design.

Java ListSelectionListener interface with keyboard

I have implemented ListSelectionListener as you can see below, so that after a specific line in the first table is being chosen, the second table gets updated accordingly.
class SelectionListener implements ListSelectionListener {
public SelectionListener(){}
#Override
public void valueChanged(ListSelectionEvent e)
{
if (e.getSource() == myTrumpsAndMessages.jTable1.getSelectionModel()
&& myTrumpsAndMessages.jTable1.getRowSelectionAllowed()
&& e.getValueIsAdjusting())
{
int selected = myTrumpsAndMessages.jTable1.getSelectedRow();
clearjTable(jTable4);
showSubscribers(selected);
}
}
}
Is there a way to invoke the listener not only when the mouse is choosing, but also when the choice is being made from the keyboard?
The reason for the unusual experience - no notification on selection via keyboard - is a subtle different setting of valueIsAdjusting for keyboard vs. mouse-triggered selection events:
keyboard triggered selection (even with modifiers) only fires once (with adjusting == false)
mouse triggered selection always fires twice (first with true, second with false)
That fact combined with the unusual logic (which #Robin spotted, +1 to him :-)
if (e.getSource() == myTrumpsAndMessages.jTable1.getSelectionModel()
&& myTrumpsAndMessages.jTable1.getRowSelectionAllowed()
// typo/misunderstanding or feature? doing stuff only when adjusting
&& e.getValueIsAdjusting())
(reacting only if the selection is adjusting) leads to not seeing keyboard triggered changes.
Is there a way to invoke the listener not only when the mouse is choosing, but also when the choice is being made from the keyboard?
The listener will be triggered, independent of the source of the selection change. So yes, this is perfectly possible and even the default behavior. So nothing special must be done to get this working.
Looking at the code of your listener, I would suggest to rewrite it to
class SelectionListener implements ListSelectionListener {
public SelectionListener(){}
#Override
public void valueChanged(ListSelectionEvent e){
if ( e.getValueIsAdjusting() ){
return;
}
if (e.getSource() == myTrumpsAndMessages.jTable1.getSelectionModel() &&
myTrumpsAndMessages.jTable1.getRowSelectionAllowed() ) {
int selected = myTrumpsAndMessages.jTable1.getSelectedRow();
clearjTable(jTable4);
showSubscribers(selected);
}
}
}
Note the quick break from the method when getValueIsAdjusting() returns true as this is the behavior you want in most cases.
I've just tried a ListSelectionListener and the valueChanged() event is actually being triggered on keyboard selection change as well. See my example below:
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
System.out.println(list.getSelectedValue());
}
});

Automatic Updating of Action's isEnabled()

I have written a Swing GUI with several controls associated with the same Action subclass. The implementation of the Action subclass follows this psudocode:
public class MyGUI
{
Gizmo gizmo_; // Defined elsewhere
public class Action_StartPlayback extends AbstractAction
{
/* ctor */
public Action_StartPlayback(String text, ImageIcon icon, String desc, Integer mnem)
{
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
putValue(MNEMONIC_KEY, mnem);
}
#Override public boolean isEnabled()
{
return gizmo_ == null;
}
#Override public void actionPerformed(ActionEvent e)
{
gizmo_ = new Gizmo();
}
Action_StartPlayback act_;
};
The action is associated with both a button and a menu item, in a way similar to this psudocode:
act_ = new Action_StartPlayback(/*...*/);
// ...
JButton btn = new JButton(act_);
JMenu mnu = new JMenu(act_);
When I click the button or the menu item, the action's actionPerformed is fired correctly, gizmo_ is initialized and is non-null and everything works as expected -- except that the button and menu item are still enabled.
I expected that isEnabled would have been called again "automagically" but this is obviously not happening. isEnabled() is never called again.
This evokes two questions:
Is it OK for me to #Override the isEnabled() method as I have done here?
Assuming the answer to #1 is yes, how do I trigger a refresh of the GUI so that isEnabled() is called again, resulting in the button & menu item being disabled?
Instead of overriding setEnabled you could simply call setEnabled(false) after you intitialize your gizmo in your actionPerformed method:
#Override public void actionPerformed(ActionEvent e)
{
gizmo_ = new Gizmo();
setEnabled(false);
}
Here's the setEnabled implementation from AbstractAction:
public void setEnabled(boolean newValue) {
boolean oldValue = this.enabled;
if (oldValue != newValue) {
this.enabled = newValue;
firePropertyChange("enabled",
Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
}
}
The automagical you're looking for is the call to firePropertyChange, which notifies components based on this action that the state has changed, so the component can update its own state accordingly.
I am no pro at this, but I don't see a see an automatic way of doing this, of notifying listeners that the state of enabled has changed. Of course you can call setEnabled(false) at the start of the actionPerformed, and then code Gizmo (or a wrapper on Gizmo) to have property change support and then add a PropertyChangeListener to Gizmo, and in that listener, when the state changes to DONE, call setEnabled(true). A bit kludgy but it would work.
This is not strictly limited to Swing, but a more general Java principle. A lot of classes in the JDK (and in other libraries) have a getter and a setter for a property. Those methods are not meant to be overridden to return a dynamic value as most of the times the superclass accesses the corresponding field directly and does not go through the getters.
If you have dynamic behavior, you should call the corresponding setter each time the value changes. This will notify the super class changes have been made, and typically this will also fire a property change event to notify other interested parties.
You can find a bit more on this convention if you do a search on Java beans.
In your case, a possible solution is to let your UI class fire a PropertyChangeEvent when that gizmo instance changes, and let your actions listen for that event. When they receive such an event, they update their own enabled state.
The enabled-state is stored in both of your objects, in the AbstractAction and in the JButton.
This is important because you only need one instance of Action_StartPlayback for multiple Components like:
In the menu's button.
In a toolbar.
In a Shortcut Strgp in example.
All of them can have the same instance of Action_startPlayback. The Action_startPlayback is the only source of truth. The components are responsible to respect this source of truth so every Component will ask the AbstractAction to notify them if something has been changed. The AbstractAction will remember all the components and will notify them using the Method firePropertyChange().
But how to repaint all pending components? You must force all pending Components to ask the Action_startPlayback for the actuall enabled-state! Look at this:
#Override public void actionPerformed(ActionEvent e)
{
gizmo_ = new Gizmo();
// now, force the components to get notified.
setEnabled(true);
}

Categories