Clear JTable Values
JTable table = null;
defaultTableModel = new DefaultTableModel();
table = new JTable(defaultTableModel);
defaultTableModel.getDataVector().removeAllElements();
table.repaint();
table.revalidate();
Thanking you....
JTable table = null;
defaultTableModel = new DefaultTableModel();
table = new JTable(defaultTableModel);
defaultTableModel.getDataVector().removeAllElements();
table.repaint();
table.revalidate();
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");
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();
}
}
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.