February 09, 2012

How to clear table value in java

Clear JTable Values



JTable table = null;
defaultTableModel = new DefaultTableModel();
table = new JTable(defaultTableModel);


defaultTableModel.getDataVector().removeAllElements();
table.repaint();
table.revalidate();


Thanking you....

February 06, 2012

Writing to a file in java

How to write to Text file in java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;


public class FileWriting {
   
    private FileOutputStream text_fos = null;
    private PrintStream textOp_psm =null;
    private  int  phoneNumber = 12345667;

    private void setFile() throws FileNotFoundException {
        text_fos = new FileOutputStream(new File("belazy.txt"));
        textOp_psm = new PrintStream(text_fos);
        textOp_psm.print("bla bla bla ............");
        textOp_psm.println(" yep yep yep ");
        textOp_psm.print("phone number is :"+phoneNumber);
        textOp_psm.println(" by belazy");
        textOp_psm.close();
       
        System.out.println(" completed ");
       
    }


   
    public static void main(String[] args) {
        FileWriting f = new FileWriting();
        try{
        f.setFile();
        }catch (Exception e) {
            // TODO: handle exception
        }
    }
}

Look here to know how to read and write an image in java blzyjava

How to add a image on java button on mouse over

Setting Icon for button that changes on mouse over  



       ImageIcon icon = new ImageIcon("javacode/image.gif");
            ImageIcon iconOver = new ImageIcon("javacode/image_over.gif");
            btn = new JButton(icon);
            btn.setBackground(Color.white);
            btn.setRolloverIcon(iconOver);
            btn.setBorder(null);
            //btn.setToolTipText("java blog belazy");

This code will show you how to put image on button in java which change on mouse over .
//



java source codes

Example for POJO class

POJO Entity Class example


public class IPAddress {
   
    private int ipAddressId = 0;
    private String ipAddress = null;
    private String description = null;
   
    public int getIpAddressId() {
        return ipAddressId;
    }
    public void setIpAddressId(int ipAddressId) {
        this.ipAddressId = ipAddressId;
    }
    public String getIpAddress() {
        return ipAddress;
    }
    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
   

}

Round bordered TextField in java

How to a make a round border or round corner text field / text area in java swing


Frequently asked questions like how is it possible to make my javatexfield and textarea either round corner or round border. Here is a solution for that, so please go through the code and reply
/*
*@author Elizebath
*/
public class RoundJTextField extends JTextField {
private Shape shaping;
public RoundJTextField(int size) {
super(size);
setOpaque(false);
}
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
super.paintComponent(g);
}
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
}
public boolean contains(int x, int y) {
if (shaping == null || !shaping.getBounds().equals(getBounds())) {
shaping = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 15, 15);
}
return shaping.contains(x, y);
}
}

JTextField t = new RoundJTextField(20);


more

How to create maximize and minimize java swing frame

How to create a window in java that having system screen size/dimension



JFrame frame = new JFrame();
frame.setExtendedState(Frame.MAXIMIZED_BOTH);


Or

Dimension screenDimension = getScreenDimension();
frame.setSize(screenDimension.width,screenDimension.height );

private Dimension getScreenDimension() {
Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension dimension = toolkit.getScreenSize(); return dimension; }

How to set screen dimension to application window.

Hi Friends,

This is a small blog.





Please provide your valuable feedbacks



Thanking you.... +Belazy L 

February 05, 2012

Java calculator source code

A simple calculator in java with source code

Description : In this project simple calculator in java, i used getSource method to know which button is clicked, i didnt assigned any value to the button





package calculator;
import javax.swing.*;
import java.awt.event.*;
/*
 * java calculator
 */
class ScientificCalculator implements ActionListener
{
JFrame calculatorFrame;
JTextField calculatorTxt;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;
static double a=0,b=0,result=0;
static int operator=0;
ScientificCalculator()
{
calculatorFrame=new JFrame("Java Calculator");
calculatorTxt=new JTextField();
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b0=new JButton("0");
bdiv=new JButton("/");
bmul=new JButton("*");
bsub=new JButton("-");
badd=new JButton("+");
bdec=new JButton(".");
beq=new JButton("=");
bdel=new JButton("Del");
bclr=new JButton("Clear");

calculatorTxt.setBounds(30,40,280,30);
b7.setBounds(40,100,50,40);
b8.setBounds(110,100,50,40);
b9.setBounds(180,100,50,40);
bdiv.setBounds(250,100,50,40);

b4.setBounds(40,170,50,40);
b5.setBounds(110,170,50,40);
b6.setBounds(180,170,50,40);
bmul.setBounds(250,170,50,40);

b1.setBounds(40,240,50,40);
b2.setBounds(110,240,50,40);
b3.setBounds(180,240,50,40);
bsub.setBounds(250,240,50,40);

bdec.setBounds(40,310,50,40);
b0.setBounds(110,310,50,40);
beq.setBounds(180,310,50,40);
badd.setBounds(250,310,50,40);

bdel.setBounds(60,380,100,40);
bclr.setBounds(180,380,100,40);

calculatorFrame.add(calculatorTxt);
calculatorFrame.add(b7);
calculatorFrame.add(b8);
calculatorFrame.add(b9);
calculatorFrame.add(bdiv);
calculatorFrame.add(b4);
calculatorFrame.add(b5);
calculatorFrame.add(b6);
calculatorFrame.add(bmul);
calculatorFrame.add(b1);
calculatorFrame.add(b2);
calculatorFrame.add(b3);
calculatorFrame.add(bsub);
calculatorFrame.add(bdec);
calculatorFrame.add(b0);
calculatorFrame.add(beq);
calculatorFrame.add(badd);
calculatorFrame.add(bdel);
calculatorFrame.add(bclr);

calculatorFrame.setLayout(null);
calculatorFrame.setVisible(true);
calculatorFrame.setSize(350,500);
calculatorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calculatorFrame.setResizable(false);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
badd.addActionListener(this);
bdiv.addActionListener(this);
bmul.addActionListener(this);
bsub.addActionListener(this);
bdec.addActionListener(this);
beq.addActionListener(this);
bdel.addActionListener(this);
bclr.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
calculatorTxt.setText(calculatorTxt.getText().concat("1"));

if(e.getSource()==b2)
calculatorTxt.setText(calculatorTxt.getText().concat("2"));

if(e.getSource()==b3)
calculatorTxt.setText(calculatorTxt.getText().concat("3"));

if(e.getSource()==b4)
calculatorTxt.setText(calculatorTxt.getText().concat("4"));

if(e.getSource()==b5)
calculatorTxt.setText(calculatorTxt.getText().concat("5"));

if(e.getSource()==b6)
calculatorTxt.setText(calculatorTxt.getText().concat("6"));

if(e.getSource()==b7)
calculatorTxt.setText(calculatorTxt.getText().concat("7"));

if(e.getSource()==b8)
calculatorTxt.setText(calculatorTxt.getText().concat("8"));

if(e.getSource()==b9)
calculatorTxt.setText(calculatorTxt.getText().concat("9"));

if(e.getSource()==b0)
calculatorTxt.setText(calculatorTxt.getText().concat("0"));

if(e.getSource()==bdec)
calculatorTxt.setText(calculatorTxt.getText().concat("."));

if(e.getSource()==badd)
{
a=Double.parseDouble(calculatorTxt.getText());
operator=1;
calculatorTxt.setText("");
}


if(e.getSource()==bsub)
{
a=Double.parseDouble(calculatorTxt.getText());
operator=2;
calculatorTxt.setText("");
}

if(e.getSource()==bmul)
{
a=Double.parseDouble(calculatorTxt.getText());
operator=3;
calculatorTxt.setText("");
}

if(e.getSource()==bdiv)
{
a=Double.parseDouble(calculatorTxt.getText());
operator=4;
calculatorTxt.setText("");
}

if(e.getSource()==beq)
{
b = calculatorTxt.getText().equals("") ? 0:Double.parseDouble(calculatorTxt.getText());
switch(operator)
{
case 0: result=b;
break;

case 1: result=a+b;
break;

case 2: result=a-b;
break;

case 3: result=a*b;
break;

case 4: result=a/b;
break;

default: result=0;
}

calculatorTxt.setText(""+result);
}

if(e.getSource()==bclr)
calculatorTxt.setText("");

if(e.getSource()==bdel)
{
String s=calculatorTxt.getText();
calculatorTxt.setText("");
for(int i=0;i<s.length()-1;i++)
calculatorTxt.setText(calculatorTxt.getText()+s.charAt(i));
}

}

public static void main(String...s)
{
new ScientificCalculator();
}
}



Java scientific calculator full source code

java source code available here


http://javabelazy.blogspot.in/

February 02, 2012

slidder game in java

Java games with source code


Click the link below

Java games link1

Fun with java link2

Download java applet games link3

Java applet game with source code link4

java applet online link5

Free java applet games link6

Learn how to program applet java games  link7

Java Applet tutorial link8

http://javabelazy.blogspot.in/

February 01, 2012

Float value constraint to two decimal points


How to Format a double value to 2 decimal places

 
 
 import java.text.*; 
 public class DecimalPlaces {
        public static void main(String[] args) {
            double d = 1.234567; 
 DecimalFormat df = new DecimalFormat("#.##");
          System.out.print(df.format(d)); 
 }
    }
 
 

Description : How to confined the value in java upto two decimal places. Decimal value allowed only upto two spaces.

Network monitoring in java to get all computer names in the network


Java Network Monitoring : To get all intra networked computer list


//Java network program to list out all available computers in intranet

import java.io.*;

public class NetworkUsers
{
static String all;
public static void main(String[] args)
{try {
// Execute command

Process child = Runtime.getRuntime().exec("net view");
// Get the input stream and read from it
InputStream in = child.getInputStream();
int c;
while ((c = in.read()) != -1)
{
char e=(char)c;
System.out.println(e);
//process((char)c);
}
in.close();
} catch (IOException e) { } }

private static void process(char c) {
// TODO Auto-generated method stub
System.out.print(c);
//all =String.valueOf(c) +all;
// System.out.println(all);
}


}


 Please provide your valuable comments

Dijkstra algorithm implementation in java.

Dijkstra algorithm implementation in java.




public class DijkstraAlgorithm {
public static void computePaths(Vertex source) {
source.minDistance = 0.;
PriorityQueue vertexQueue = new PriorityQueue();
vertexQueue.add(source);

while (!vertexQueue.isEmpty()) {
Vertex u = vertexQueue.poll();// Visit each edge exiting u
for (Edge e : u.adjacencies) {
Vertex v = e.target;
double weight = e.weight;
double distanceThroughU = u.minDistance + weight;
if (distanceThroughU < v.minDistance) {
vertexQueue.remove(v);
v.minDistance = distanceThroughU;
v.previous = u;
vertexQueue.add(v);
}}}}

public static List getShortestPathTo(Vertex target) {
List path = new ArrayList();
for (Vertex vertex = target; vertex != null; vertex = vertex.previous)
path.add(vertex);
Collections.reverse(path);
return path;
}

public static void main(String[] args) {
Vertex v0 = new Vertex("a");
Vertex v1 = new Vertex("b");
Vertex v2 = new Vertex("c");
Vertex v3 = new Vertex("d");
Vertex v4 = new Vertex("e");

Vertex []vertices= new Vertex[5];
Edge []ed=new Edge[4];

for(int i=0;i<5;i++)
{
vertices[i]= new Vertex("a"+i);
for(int j=0;j<5;j++)
{
ed[j]=new Edge(vertices[j],10+j);
}
//vertices[i].adjacencies =new Edge[]{ed[]};
}

System.out.println(vertices.length +"and "+vertices[3]);
//v0.adjacencies = new Edge[] { new Edge(v1, 8), new Edge(v2, 10),
//new Edge(v3, 8) };
//v1.adjacencies = new Edge[] { new Edge(v0, 5), new Edge(v2, 3),
//new Edge(v4, 7) };
//v2.adjacencies = new Edge[] { new Edge(v0, 10), new Edge(v1, 3) };
//v3.adjacencies = new Edge[] { new Edge(v0, 8), new Edge(v4, 12) };
//v4.adjacencies = new Edge[] { new Edge(v1, 7), new Edge(v3, 2) };
//Vertex[] vertices = { v0, v1, v2, v3, v4 };
computePaths(vertices[0]);
for (Vertex v : vertices) {
System.out.println("Distance to the vertex ( " + v + ") is : " + v.minDistance);
List path = getShortestPathTo(v);
System.out.println("Shortest path: " + path);
System.out.println(" Dijkstra algorithm to compute shortest distance ");
}}}

Please make necessary modification to run this code

Router simulator : you can create upto 10 routers, software that shows the working of a router, how shortest path is computed, calculating routing table, comparing 5 algorithms,

You can download Router simulator (Dijkstra algorithm implemented) running jar file from the below link

Server1

File name :  javabelazy35744
password : AppleIphone

Facebook comments