Popular Posts
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... Word break tag : <wbr/> (HTML5) The  HTML  <wbr>  tag  is  used  defines  a  potential  line  break  point  if  needed.  This  stands  for  Word  BReak. This  is  u... 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
package test.win;

import java.awt.Dimension;
import java.awt.event.HierarchyBoundsListener;
import java.awt.event.HierarchyEvent;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;

public class MyFrame extends JFrame implements Runnable {

    private JTextArea textArea;
    private MyFrame thisFrame;

    public MyFrame() {
        this.textArea = new JTextArea();
        this.thisFrame = this;
        this.setOut();
        this.getContentPane().setLayout(new SpringLayout());
        JScrollPane sPane = new JScrollPane(this.textArea);
        sPane.addHierarchyBoundsListener(new HierarchyBoundsListener() {

            @Override
            public void ancestorMoved(HierarchyEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void ancestorResized(HierarchyEvent e) {
                // TODO Auto-generated method stub
                JScrollPane pane = (JScrollPane) e.getSource();
                pane.setPreferredSize(new Dimension(thisFrame.getContentPane().getWidth(), thisFrame.getContentPane().getHeight()));
            }

        });
        this.getContentPane().add(sPane);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300, 300);
        this.setLocation(300, 300);
        this.setVisible(true);
    }

    private void appendText(final String text) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                textArea.append(text);
            }

        });
    }

    private void setOut() {
        OutputStream os = new OutputStream() {

            @Override
            public void write(int b) throws IOException {
                // TODO Auto-generated method stub
                appendText(String.valueOf((char) b));
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                // TODO Auto-generated method stub
                appendText(new String(b, off, len));
            }

            @Override
            public void write(byte[] b) throws IOException {
                // TODO Auto-generated method stub
                write(b, 0, b.length);
            }

        };

        System.setErr(new PrintStream(os, true));
        System.setOut(new PrintStream(os, true));
    }

    @Override
    public void run() {
        while (true) {
            // TODO Auto-generated method stub
            try {
                Thread.sleep(1000);
                System.out.println(new Date());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        new Thread(new MyFrame()).start();
    }

}
Detect another program running status by using socket
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class MyFrame extends JFrame {

    private ServerSocket ss;

    public MyFrame() {

        try {
            // create a new socket and bind it to listen
            ss = new ServerSocket();
            ss.bind(new InetSocketAddress(100));
        } catch (SocketException e) {
            // if this socket is in using, presume the program is running
            JOptionPane.showMessageDialog(this, "Application already in running.");
            System.exit(1);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Application encountered some problem.");
            System.exit(1);
        }
        this.getContentPane().add(new JLabel("Hello world!"));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300, 300);
        this.setLocation(300, 300);
        this.setVisible(true);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        new MyFrame();
    }

}
Get multiple screen devices
GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
CSS priority
<html>
<head>
    <title>CSS priority</title>
    <style type="text/css">
    div#divline {
        color : blue !important;  // this will take higher priority for style display
    }
    </style>
</head>
<body>
    <div id="divline" style="color:red;">This is a line set font color red using attribute 'style'.</div>
</body>
</html>
Add attachment as inline resource when sending mail
static void Main(string[] args)
{
    // creat client instance
    SmtpClient client = new SmtpClient("smtp.mail.com", 25);

    // create mail message
    MailMessage message = new MailMessage();
    message.From = new MailAddress("from@mail.com", "I'm a sender");
    message.To.Add("to@mail.com");

    // create attachment instance
    Attachment attachment = new Attachment(@"C:\about-pic.gif");
    // set attachment name (for assign resource name later)
    attachment.Name = Path.GetFileName(@"C:\about-pic.gif");
    attachment.ContentId = "about_pic";
    // set attachment same to mail
    attachment.NameEncoding = message.BodyEncoding;
    attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

    // set attachment as embeded resources
    attachment.ContentDisposition.Inline = true;
    attachment.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;


    message.Attachments.Add(attachment);
    message.Subject = "test attchement";
    message.Body = "<html><head><title>test attchemtn</title></head><body style='background-color:#aaaaff;'>"
        + "<h1>test attchement</h1><img src='cid:"+attachment.ContentId+"' /></body></html>";
    message.IsBodyHtml = true;
    client.Send(message);

}
Create instance using Class
import java.lang.reflect.Constructor;

public class Case012 {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        // General way to create a new instance
        // Case012 c = new Case012("dddd");

        Class c = Class.forName("sb.test.c010.Case012");

        Constructor constructor1 = c.getConstructor(String.class);
        constructor1.newInstance("test");

        Constructor constructor2 = c.getConstructor(int.class, int.class);
        constructor2.newInstance(10, 20);
    }

    public Case012(String s) {
        System.out.printf("Create instance with string parameter : %s", s);
        System.out.println();
    }

    public Case012(int x, int y) {
        System.out.println("Create instance with two int paramters.");
        System.out.printf("Sum of two number is : %d", x + y);
        System.out.println();
    }
}
Check for multiple textbox size
var JSUtil = {
    IsTextCountsInRange: function(obj, min, max) {
        if (obj) {
            if (typeof (obj) == "string") {
                var targetElement = document.getElementById(obj);
                if (targetElement && targetElement.value) {
                    return targetElement.value.toString().length >= min && targetElement.value.toString().length <= max;
                } else {
                    return false;
                }
            } else if (typeof (obj) == "object" && obj.value) {
                return obj.value.toString().length >= min && obj.value.toString().length <= max;
            }
        } else {
            return false;
        }
    }
}