Monday, 9 November 2015

Launch url in default browser by java swing application

For launching url in users default browser  by java swing application follwing codes are used

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class MyApp implements ActionListener
{

   JFrame f1;
   JButton browse;

   MyApp()
   {

      f1 = new JFrame("MyApp");
      browse = new JButton("Browse Google");

      browse.addActionListener(this);

      f1.add(browse);
      f1.setLayout(new FlowLayout(FlowLayout.LEFT));
      f1.setVisible(true);
      f1.setSize(200,200);
      f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }


   public void actionPerformed(ActionEvent ae)
   {
     String s = ae.getActionCommand();

      if(s.equalsIgnoreCase("Browse Google"))
      {
        try{
        Desktop d =  Desktop.getDesktop();

        d.browse(new URI("www.google.com"));}
        catch(Exception e){}

      }

     else{
      System.out.println("No action performed");
     }
   }

  public static void main(String[] args)
  {
    MyApp app = new MyApp();
  }
}
Output




Sunday, 8 November 2015

How to open a website from program in Java

Input

import java.awt.*;
import java.net.*;

public class mydesktop
{
  public static void main(String[] args)
  {
     try{

     Desktop d = Desktop.getDesktop();

     d.browse(new URI("www.google.com"));

      }

     catch(Exception e)
     {}
   }
}

Output


Wednesday, 4 November 2015

Java Program to display a JFrame

JFrame is a class of package javax,swing.It is imported first.Then the JFrame class object is made.
Some properties are added to it .It is required to display JFrame.

setvisible function for making JFrame visible
setsize function for setting size of frame
"welcome" is passed as parameter as title of the JFrame

Program requirements :Jdk 7 or above (at least) ,notepad++ editor,command prompt.
settings :Environment variables should be set.

Program:

import javax.swing.JFrame;

class myFrame
{

  public static void main(String[] args)
  {

     JFrame f1 = new JFrame("Welcome");
     f1.setSize(400,400);
     f1.setVisible(true);

   }
}


Output