Java JSP – Forward Action Example

<html>

<%
double freeMem = Runtime.getRuntime().freeMemory();
double totlMem = Runtime.getRuntime().totalMemory();
double percent = freeMem/totlMem;
if (percent < 0.5) {
%>

<jsp:forward page="/jsp/forward/one.jsp"/>

<% } else { %>

<jsp:forward page="two.html"/>

<% } %>

</html>

Java Swing – Set JFrame Background Image

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.FlowLayout;

public class PutImageOnJFrame extends JPanel {

    public PutImageOnJFrame() {
        setOpaque(false);
        setLayout(new FlowLayout());
    }

    public static void main(String[]args) {
        JFrame myFrame = new JFrame("Put Image");
        JButton button1 = new JButton("Sample 1");
        JButton button2 = new JButton("Sample 2");

        PutImageOnJFrame c = new PutImageOnJFrame();
        c.add(button1);
        c.add(button2);

        myFrame.add(c);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setSize(400,400);
        myFrame.setVisible(true);
    }

    public void paint(Graphics g) {
        //IT DEPEND ON YOUR PICTURE AND PUT IT'S LOCATION IN
        Image a=Toolkit.getDefaultToolkit().getImage("E:\\_JAVA_\\---SOURCE CODE---\\bg.jpg");
        g.drawImage(a,0,0,getSize().width,getSize().height,this);
        super.paint(g);
    }

}

Java AWT – Getting Data from Computer Clipboard

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class Main {

    public void displayTextFromClipboard() {

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Clipboard clipboard = toolkit.getSystemClipboard();
        Transferable tran = null;

        try {
            //The parameter to getContents is not currently used so null should
            //be sent. If the clipboard is currently not available (for example
            //it is used by another application) the method throws an
            //IllegalStateException.
            tran = clipboard.getContents(null);
        } catch (IllegalStateException ex) {
            System.out.println("The clipboard is unavailable.");
            return;
        }

        if (tran != null && tran.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            try {
                String clipboardContent = (String)tran.getTransferData(DataFlavor.stringFlavor);
                System.out.println(clipboardContent);
            } catch (UnsupportedFlavorException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        new Main().displayTextFromClipboard();
    }

}

Page 1 of 212