If/else statements in ANTLR using listeners

By default, ANTLR 4 generates listeners. But if you give org.antlr.v4.Tool the command line parameter -visitor, ANTLR generates visitor classes for you. These work much like listeners, but give you more control over which (sub) trees are walked/visited. This is particularly useful if you want to exclude certain (sub) trees (like else/if blocks, as in … Read more

Android OnClickListener – identify a button

You will learn the way to do it, in an easy way, is: public class Mtest extends Activity { Button b1; Button b2; public void onCreate(Bundle savedInstanceState) { … b1 = (Button) findViewById(R.id.b1); b2 = (Button) findViewById(R.id.b2); b1.setOnClickListener(myhandler1); b2.setOnClickListener(myhandler2); … } View.OnClickListener myhandler1 = new View.OnClickListener() { public void onClick(View v) { // it was … Read more

Create a custom event in Java

You probably want to look into the observer pattern. Here’s some sample code to get yourself started: import java.util.*; // An interface to be implemented by everyone interested in “Hello” events interface HelloListener { void someoneSaidHello(); } // Someone who says “Hello” class Initiater { private List<HelloListener> listeners = new ArrayList<HelloListener>(); public void addListener(HelloListener toAdd) … Read more

File changed listener in Java

I’ve written a log file monitor before, and I found that the impact on system performance of polling the attributes of a single file, a few times a second, is actually very small. Java 7, as part of NIO.2 has added the WatchService API The WatchService API is designed for applications that need to be … Read more

How do I pass the value (not the reference) of a JS variable to a function? [duplicate]

In modern browsers, you can use the let or const keywords to create a block-scoped variable: for (let i = 0; i < results.length; i++) { let marker = results[i]; google.maps.event.addListener(marker, ‘click’, () => change_selection(i)); } In older browsers, you need to create a separate scope that saves the variable in its current state by … Read more

Value Change Listener to JTextField

Add a listener to the underlying Document, which is automatically created for you. // Listen for changes in the text textField.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { warn(); } public void removeUpdate(DocumentEvent e) { warn(); } public void insertUpdate(DocumentEvent e) { warn(); } public void warn() { if (Integer.parseInt(textField.getText())<=0){ JOptionPane.showMessageDialog(null, “Error: Please enter number … Read more

JavaScript: remove event listener

You need to use named functions. Also, the click variable needs to be outside the handler to increment. var click_count = 0; function myClick(event) { click_count++; if(click_count == 50) { // to remove canvas.removeEventListener(‘click’, myClick); } } // to add canvas.addEventListener(‘click’, myClick); EDIT: You could close around the click_counter variable like this: var myClick = … Read more