Skip to main content


Showing posts from February, 2011

Converting list into set in java

import java.util.List; import java.util.Set; import java.util.ArrayList; import java.util.HashSet; public class list2Set{  public static void main(String args[]) {    try{        // Creating one sample list        List sampleList=new ArrayList();        // Adding some element into list        sampleList.add("One");        sampleList.add("Two");        sampleList.add("Three");        sampleList.add("One");       // Converting list in set. During this conversion all the duplicate elements are removed.       Set mySet=new HashSet(sampleList);       System.out.println(mySet);    }   catch(Exception e){     e.printStackTrace();    }  } }

Converting queue into list in java

import java.util.List; import java.util.Queue; import java.util.LinkedList; import java.util.ArrayList; public class queue2List {   public static void main(String args[])   {     try{           //Creating queue           Queue queue1=new LinkedList();           //Adding some elements into queue           queue1.add("One");           queue1.add("Two");           queue1.add("Three");           //Converting queue into list            List list1=new ArrayList( queue1 );            System.out.println(list1);       }       catch(Exception e){            e.printStackTrace();      }  } }

Java code for creating menu.

import java.awt.*; import java.awt.event.*; import javax.swing.*; import static java.awt.event.InputEvent.CTRL_DOWN_MASK; public class sampleMenu extends JFrame implements ActionListener { private JMenuBar menuBar; private JMenu File,Edit; private JMenuItem nw,openFile,clse; private JMenuItem cut,copy,paste; public sampleMenu(){ super("Sample Menu!"); //Creating Menu bar menuBar=new JMenuBar(); //Creating File Menu... File=new JMenu("File"); //Creating shortcut for file.... File.setMnemonic('f'); //Creating sub menu related to File.... nw=new JMenuItem("New"); //Adding short cut for file new nw.setAccelerator(KeyStroke.getKeyStroke('N',CTRL_DOWN_MASK)); nw.addActionListener(this); openFile=new JMenuItem("Open File"); //Adding short cut for file open openFile.setAccelerator(KeyStroke.getKeyStroke('O',CTRL_DOWN_MASK)); openFile.addActionListener(this); clse=n

Java code for creating buttons and adding some action for buttons.

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class mainWindow extends JFrame implements ActionListener{ private JButton Button1; private JButton Button2; public mainWindow(){ //Set the name of window.... super("Window Name"); //Getting container handle Container container=getContentPane(); //Adding layout .... container.setLayout(null); //Set the background color of the window... container.setBackground(new Color(111,143,170)); //Creating first button..... Button1=new JButton("Button1"); //Set the position of button1..... Button1.setBounds(new Rectangle(10,30,80,30)); //Adding action listener for button1.. Button1.addActionListener(this); //Add the button1 to window.... container.add(Button1); Button2=new JButton("Button2"); Button2.setBounds(new Rectangle(100,30,80,30)); Button2.addActionListener(this); container.add(Button2); } //Adding action for button

Getting current screen resolution using java

import java.awt.*; public class screenDim { public static void main(String[] args) { try{ Toolkit newTollKit=Toolkit.getDefaultToolkit(); Dimension screenDim=newTollKit.getScreenSize(); System.out.println("Screen height in pixels: "+screenDim.height); System.out.println("Screen width in pixels: "+screenDim.width); } catch(Exception e){ e.printStackTrace(); } } }