Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Stats
Redirect System.out & System.err to JTextarea
  1. package test.win;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.event.HierarchyBoundsListener;
  5. import java.awt.event.HierarchyEvent;
  6. import java.io.IOException;
  7. import java.io.OutputStream;
  8. import java.io.PrintStream;
  9. import java.util.Date;
  10.  
  11. import javax.swing.JFrame;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.JTextArea;
  14. import javax.swing.SpringLayout;
  15. import javax.swing.SwingUtilities;
  16.  
  17. public class MyFrame extends JFrame implements Runnable {
  18.  
  19.     private JTextArea textArea;
  20.     private MyFrame thisFrame;
  21.  
  22.     public MyFrame() {
  23.         this.textArea = new JTextArea();
  24.         this.thisFrame = this;
  25.         this.setOut();
  26.         this.getContentPane().setLayout(new SpringLayout());
  27.         JScrollPane sPane = new JScrollPane(this.textArea);
  28.         sPane.addHierarchyBoundsListener(new HierarchyBoundsListener() {
  29.  
  30.             @Override
  31.             public void ancestorMoved(HierarchyEvent e) {
  32.                 // TODO Auto-generated method stub
  33.  
  34.             }
  35.  
  36.             @Override
  37.             public void ancestorResized(HierarchyEvent e) {
  38.                 // TODO Auto-generated method stub
  39.                 JScrollPane pane = (JScrollPane) e.getSource();
  40.                 pane.setPreferredSize(new Dimension(thisFrame.getContentPane().getWidth(), thisFrame.getContentPane().getHeight()));
  41.             }
  42.  
  43.         });
  44.         this.getContentPane().add(sPane);
  45.  
  46.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47.         this.setSize(300, 300);
  48.         this.setLocation(300, 300);
  49.         this.setVisible(true);
  50.     }
  51.  
  52.     private void appendText(final String text) {
  53.         SwingUtilities.invokeLater(new Runnable() {
  54.  
  55.             @Override
  56.             public void run() {
  57.                 // TODO Auto-generated method stub
  58.                 textArea.append(text);
  59.             }
  60.  
  61.         });
  62.     }
  63.  
  64.     private void setOut() {
  65.         OutputStream os = new OutputStream() {
  66.  
  67.             @Override
  68.             public void write(int b) throws IOException {
  69.                 // TODO Auto-generated method stub
  70.                 appendText(String.valueOf((char) b));
  71.             }
  72.  
  73.             @Override
  74.             public void write(byte[] b, int off, int len) throws IOException {
  75.                 // TODO Auto-generated method stub
  76.                 appendText(new String(b, off, len));
  77.             }
  78.  
  79.             @Override
  80.             public void write(byte[] b) throws IOException {
  81.                 // TODO Auto-generated method stub
  82.                 write(b, 0, b.length);
  83.             }
  84.  
  85.         };
  86.  
  87.         System.setErr(new PrintStream(os, true));
  88.         System.setOut(new PrintStream(os, true));
  89.     }
  90.  
  91.     @Override
  92.     public void run() {
  93.         while (true) {
  94.             // TODO Auto-generated method stub
  95.             try {
  96.                 Thread.sleep(1000);
  97.                 System.out.println(new Date());
  98.             } catch (InterruptedException e) {
  99.                 // TODO Auto-generated catch block
  100.                 e.printStackTrace();
  101.             }
  102.         }
  103.     }
  104.  
  105.     /**
  106.      * @param args
  107.      */
  108.     public static void main(String[] args) {
  109.         // TODO Auto-generated method stub
  110.  
  111.         new Thread(new MyFrame()).start();
  112.     }
  113.  
  114. }
Detect another program running status by using socket
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.net.InetSocketAddress;
  4. import java.net.ServerSocket;
  5. import java.net.SocketException;
  6. import java.util.ArrayList;
  7.  
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JOptionPane;
  11.  
  12. public class MyFrame extends JFrame {
  13.  
  14.     private ServerSocket ss;
  15.  
  16.     public MyFrame() {
  17.  
  18.         try {
  19.             // create a new socket and bind it to listen
  20.             ss = new ServerSocket();
  21.             ss.bind(new InetSocketAddress(100));
  22.         } catch (SocketException e) {
  23.             // if this socket is in using, presume the program is running
  24.             JOptionPane.showMessageDialog(this, "Application already in running.");
  25.             System.exit(1);
  26.         } catch (Exception e) {
  27.             JOptionPane.showMessageDialog(this, "Application encountered some problem.");
  28.             System.exit(1);
  29.         }
  30.         this.getContentPane().add(new JLabel("Hello world!"));
  31.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.         this.setSize(300, 300);
  33.         this.setLocation(300, 300);
  34.         this.setVisible(true);
  35.     }
  36.  
  37.     /**
  38.      * @param args
  39.      */
  40.     public static void main(String[] args) {
  41.         // TODO Auto-generated method stub
  42.  
  43.         new MyFrame();
  44.     }
  45.  
  46. }
Get multiple screen devices
  1. GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
CSS priority
  1. <html>
  2. <head>
  3.     <title>CSS priority</title>
  4.     <style type="text/css">
  5.     div#divline {
  6.         color : blue !important;  // this will take higher priority for style display
  7.     }
  8.     </style>
  9. </head>
  10. <body>
  11.     <div id="divline" style="color:red;">This is a line set font color red using attribute 'style'.</div>
  12. </body>
  13. </html>
Add attachment as inline resource when sending mail
  1. static void Main(string[] args)
  2. {
  3.     // creat client instance
  4.     SmtpClient client = new SmtpClient("smtp.mail.com", 25);
  5.  
  6.     // create mail message
  7.     MailMessage message = new MailMessage();
  8.     message.From = new MailAddress("from@mail.com", "I'm a sender");
  9.     message.To.Add("to@mail.com");
  10.  
  11.     // create attachment instance
  12.     Attachment attachment = new Attachment(@"C:\about-pic.gif");
  13.     // set attachment name (for assign resource name later)
  14.     attachment.Name = Path.GetFileName(@"C:\about-pic.gif");
  15.     attachment.ContentId = "about_pic";
  16.     // set attachment same to mail
  17.     attachment.NameEncoding = message.BodyEncoding;
  18.     attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
  19.  
  20.     // set attachment as embeded resources
  21.     attachment.ContentDisposition.Inline = true;
  22.     attachment.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
  23.  
  24.  
  25.     message.Attachments.Add(attachment);
  26.     message.Subject = "test attchement";
  27.     message.Body = "<html><head><title>test attchemtn</title></head><body style='background-color:#aaaaff;'>"
  28.         + "<h1>test attchement</h1><img src='cid:"+attachment.ContentId+"' /></body></html>";
  29.     message.IsBodyHtml = true;
  30.     client.Send(message);
  31.  
  32. }
Create instance using Class
  1. import java.lang.reflect.Constructor;
  2.  
  3. public class Case012 {
  4.  
  5.     /**
  6.      * @param args
  7.      * @throws Exception
  8.      */
  9.     public static void main(String[] args) throws Exception {
  10.         // TODO Auto-generated method stub
  11.  
  12.         // General way to create a new instance
  13.         // Case012 c = new Case012("dddd");
  14.  
  15.         Class c = Class.forName("sb.test.c010.Case012");
  16.  
  17.         Constructor constructor1 = c.getConstructor(String.class);
  18.         constructor1.newInstance("test");
  19.  
  20.         Constructor constructor2 = c.getConstructor(int.class, int.class);
  21.         constructor2.newInstance(10, 20);
  22.     }
  23.  
  24.     public Case012(String s) {
  25.         System.out.printf("Create instance with string parameter : %s", s);
  26.         System.out.println();
  27.     }
  28.  
  29.     public Case012(int x, int y) {
  30.         System.out.println("Create instance with two int paramters.");
  31.         System.out.printf("Sum of two number is : %d", x + y);
  32.         System.out.println();
  33.     }
  34. }
Check for multiple textbox size
  1. var JSUtil = {
  2.     IsTextCountsInRange: function(obj, min, max) {
  3.         if (obj) {
  4.             if (typeof (obj) == "string") {
  5.                 var targetElement = document.getElementById(obj);
  6.                 if (targetElement && targetElement.value) {
  7.                     return targetElement.value.toString().length >= min && targetElement.value.toString().length <= max;
  8.                 } else {
  9.                     return false;
  10.                 }
  11.             } else if (typeof (obj) == "object" && obj.value) {
  12.                 return obj.value.toString().length >= min && obj.value.toString().length <= max;
  13.             }
  14.         } else {
  15.             return false;
  16.         }
  17.     }
  18. }