Differentiating between left, middle, right mouse click

Here’s some sample code.

Main.java:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame
{
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) { 
            System.exit(1);
         }
      });
 
      getContentPane().addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent me) {
            if ((me.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
               System.out.print("Left");
            }
            else if ((me.getModifiers() & InputEvent.BUTTON2_MASK) != 0) {
               System.out.print("Middle");
            }
            else if ((me.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
               System.out.print("Right");
            }
            System.out.println(" mouse button was clicked!");
         }
      });
   }
  
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(300, 300);
      main.setVisible(true);
   }
}

Detecting if the right button was clicked in Swing

To determine whether a right mouse click button was clicked or not, write code in the mouseClicked function of the mouseListener Interface.

   class MyMouseListener extends MouseAdapter
   {
      public void mouseClicked(MouseEvent me)
      {
         if ((me.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
            // right mouse clicked......   
         }
      }
   };
 
   /* getModifiers() gives constant modifier for a particular click();
      BUTTON3_MASK is constant modifier for the right moouse click */

Detecting a triple-click in Swing

Use the method getClickCount() as shown in following example.

Main.java:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame
{
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) { 
            System.exit(1);
         }
      });
 
      getContentPane().addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent me) {
            if (me.getClickCount() == 3) {
               System.out.println("Button was clicked 3 times!");
            }
         }
      });
   }
  
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(300, 300);
      main.setVisible(true);
   }
}

Converting RTF to ASCII text using RTFEditorKit

Main.java:

import javax.swing.text.rtf.*;
import javax.swing.text.*;
import java.io.*;
 
public class Main 
{
   public static void main(String []args) throws Exception {
      RTFEditorKit rtf = new RTFEditorKit();
      Document doc = rtf.createDefaultDocument();
 
      FileInputStream fis = new FileInputStream("c:\richtext.rtf");
      rtf.read(fis,doc,0);
      System.out.println(doc.getText(0,doc.getLength()));
   }
}

You can find the file richtext.rtf here.

Output:

 
This is rich text
 
This is RICH TEXT
 
THIS IS RICH TEXT
 
and if you don't believe it
 
I'll tell ya another fun story

Of course, you can also call an external program to do the conversion for you, eg. http://www.a2001.com/english/down/rtf.html

Determining if a pathname is absolute

A pathname is absolute if it starts with the root.

Main.java: (on a Windows system):

import java.io.*;
 
public class Main {
   public static void main(String args[]) {
      checkAbsolute("autoexec.bat");
      checkAbsolute("c:\windows\system32\.\cloud.gif");
      checkAbsolute("//windows\..\autoexec.bat");
      checkAbsolute("\windows\..\autoexec.bat");
      checkAbsolute("windows\system32");
   }
 
   public static void checkAbsolute(String s) {
      System.out.print(new File(s).isAbsolute() ? "absolute" : "not absolute");
      System.out.println("t" + s);
   }
}

outputs:

not absolute	autoexec.bat
absolute	c:windowssystem32.cloud.gif
absolute	//windows..autoexec.bat
not absolute	windows..autoexec.bat
not absolute	windowssystem32

Difference between getAbsolutePath and getCanonicalPath

getCanonicalPath() resolves relative paths. It also restores original capitalization. You can use canonical paths to determine if two paths are the same, eg. c:windows..autoexec.bat and c:autoexec.bat will be equal if you get the path using getCanonicalPath but different with getAbsolutePath.

Main.java:

import java.io.*;
 
public class Main {
   public static void main(String args[]) {
      printPath("autoexec.bat");
      printPath("c:\windows\system32\.\cloud.gif");
      printPath("c:\windows\..\autoexec.bat");
      printPath("c:\windows");
   }
 
   public static void printPath(String s) {
      try {
         System.out.println(s);
         System.out.println("tgetAbsolutePath()t" + new File(s).getAbsolutePath());
         System.out.println("tgetCanonicalPath()t" + new File(s).getCanonicalPath());
      }
      catch(IOException e) {
         System.out.println(e); 
      }
   }
}

outputs:

autoexec.bat
	getAbsolutePath()		C:\autoexec.bat
	getCanonicalPath()	C:autoexec.bat
c:windowssystem32.cloud.gif
	getAbsolutePath()		c:windowssystem32.cloud.gif
	getCanonicalPath()	C:WINDOWSSYSTEM32cloud.gif
c:windows..autoexec.bat
	getAbsolutePath()		c:windows..autoexec.bat
	getCanonicalPath()	C:autoexec.bat
c:windows
	getAbsolutePath()		c:windows
	getCanonicalPath()	C:WINDOWS

Counting the number of lines in a file

Main.java:

import java.io.*;
 
public class Main {
   public static void main(String args[]) {
      if (args.length != 1) {
         System.err.println("Usage: java Main <textfile>");
         System.exit(1);
      }
 
      try {
         BufferedReader br = new BufferedReader(new FileReader(args[0]));
         int total = 0;
         while (br.readLine() != null) total++;
         System.out.println(total);
      }
      catch(FileNotFoundException e) {
         System.err.println(e);
      }
      catch(IOException e) {
         System.err.println(e);
      }
   }
}

Writing a String to a file

Typically, Create an instance of FileWriter (specify the filename) and wrap it up in a BufferedWriter.

Main.java:

import java.io.*;
 
public class Main {
   public static void main(String args[]) {
      String s = "First line" + System.getProperty("line.separator");
      s += "Second line";
 
      try {
         BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"));
         bw.write(s);
         bw.close();
      } catch(IOException e) {
         System.err.println(e);
      }
   }
}

Making a file read-only

Use the method setReadOnly in the File class. Notice that there is no method to determine whether a file is read-only or not, neither is there a method to undo the setReadOnly operation:

Main.java:

import java.io.*;
   
public class Main {
   public static void main(String[] args) {
      File file = new File("Main.java");
      boolean status = file.setReadOnly(); 
      System.out.println(file + " " + (status ? "":"un") + "successfully set to read-only");
   }
}

When you execute (Windows DOS-prompt) attrib main.java, you’ll see:

  A    R     MAIN~1.JAV    C:Main.java