public class RoundTwoDecimalPlaces{
public static void main(String[] args) {
float num = 2.954165f;
float round = Round(num,2);
System.out.println("Rounded data: " + round);
}
public static float Round(float Rval, int Rpl) {
float p = (float)Math.pow(10,Rpl);
Rval = Rval * p;
float tmp = Math.round(Rval);
return (float)tmp/p;
}
public static double Round(double Rval, int Rpl) {
double p = (double) Math.pow(10, Rpl);
Rval = Rval * p;
//double tmp = Math.round(Rval);
double tmp = Math.floor(Rval);
return (double) tmp / p;
}
}
Thursday, December 30, 2010
Rounding off in Java - Round two decimal places
Monday, December 27, 2010
To avoid JTextField copy in Java
// To avoid TextField copy
public void avoidTxtFieldCopy(JTextField mytextfield)
{
JTextComponent.KeyBinding[] newBindings = {
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK),
DefaultEditorKit.beepAction),
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK),
DefaultEditorKit.beepAction),
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK),
DefaultEditorKit.beepAction)
};
Keymap k = ();
JTextComponent.loadKeymap(k, newBindings, mytextfield.getActions());
Keymap k1 = Obj_Name.getKeymap();
JTextComponent.loadKeymap(k1, newBindings, Obj_Name.getActions());
}
public void avoidTxtFieldCopy(JTextField mytextfield)
{
JTextComponent.KeyBinding[] newBindings = {
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK),
DefaultEditorKit.beepAction),
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK),
DefaultEditorKit.beepAction),
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK),
DefaultEditorKit.beepAction)
};
Keymap k = ();
JTextComponent.loadKeymap(k, newBindings, mytextfield.getActions());
Keymap k1 = Obj_Name.getKeymap();
JTextComponent.loadKeymap(k1, newBindings, Obj_Name.getActions());
}
Tuesday, December 7, 2010
MySQL DB tuning
MySQL DB tuning:
In installation directory
Ex: C:\Program Files\MySQL\MySQL Server 5.0
open my (Configuration Settings) file then
we need to change as per our table creation type MyISAM/INNODB
Ex: For MyISAM below properties need to change as per our requirement
myisam_max_sort_file_size=100G
myisam_max_extra_sort_file_size=100G
myisam_sort_buffer_size=60M
key_buffer_size=26M
read_buffer_size=64K
read_rnd_buffer_size=512K
sort_buffer_size=512K
then you will feel real difference
In installation directory
Ex: C:\Program Files\MySQL\MySQL Server 5.0
open my (Configuration Settings) file then
we need to change as per our table creation type MyISAM/INNODB
Ex: For MyISAM below properties need to change as per our requirement
myisam_max_sort_file_size=100G
myisam_max_extra_sort_file_size=100G
myisam_sort_buffer_size=60M
key_buffer_size=26M
read_buffer_size=64K
read_rnd_buffer_size=512K
sort_buffer_size=512K
then you will feel real difference
Friday, December 3, 2010
Drag and Drop between two Table(Java Swing)
I have written Program for Drag and Drop between two tables.
I followed below steps,
* Creating two(table1 and table2) non editable table with single selection(drag and drop for single cell values).
* In table1 only C and D column only u can drop.
* table2 drop cell is empty then table1 drag value drop in table1 and that value will deleted from table2.
* drop cell is not empty then existing table1 value will be added to table2.
My program as below
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/**
* DADTableDemo : Example of drag and drop option between two tables
* @author R.Amirtharaj
*/
public class DADTableDemo {
/** Main Frame */
public JFrame myframe = new JFrame("Drag and drop demo");
/** Table 1*/
public JTable table1 = new JTable();
/** Table 2*/
public JTable table2 = new JTable();
/** Table1 Model */
public DefaultTableModel tm1 = new DefaultTableModel(0, 5) {
public boolean isCellEditable(int row, int col) {
return false;
}
};
/** Table2 Model */
public DefaultTableModel tm2 = new DefaultTableModel(0, 5) {
public boolean isCellEditable(int row, int col) {
return false;
}
};
/** Table1 ColumnNames */
public String[] proptbtitle1 = {"A", "B", "C(Drop will allow)", "D(Drop will allow)", "E"};
/** Table2 ColumnNames */
public String[] proptbtitle2 = {"Z"};
// Constructor
public static void main(String args[])
{
DADTableDemo demo = new DADTableDemo();
demo.create();
}
// create Drag and Drop demo
public void create()
{
JPanel pan = new JPanel();
// Table 1 related
tm1.setColumnIdentifiers(proptbtitle1);
table1.setModel(tm1);
table1.getTableHeader().setReorderingAllowed(false);
addTable1Details();
table1.setDragEnabled(true);
table1.setDropMode(DropMode.ON);
table1.setSelectionMode(0);
table1.getTableHeader().setReorderingAllowed(false);
// Add DAD
TransferHandler dnd1 = getDND1();
table1.setTransferHandler(dnd1);
// add table1 to scrollpane
JScrollPane sp1 = new JScrollPane(table1);
// add to panel
pan.add(sp1);
// Table 1 related
tm2.setColumnIdentifiers(proptbtitle2);
table2.setModel(tm2);
table2.getTableHeader().setReorderingAllowed(false);
addTable2Details();
table2.setDragEnabled(true);
table2.setSelectionMode(0);
// Add DAD
TransferHandler dnd2 = getDND2();
table2.setTransferHandler(dnd2);
// add table1 to scrollpane
JScrollPane sp2 = new JScrollPane(table2);
// add to panel
pan.add(sp2);
// add to frame
myframe.getContentPane().add(pan);
myframe.pack();
myframe.setVisible(true);
}
/**
* Table1 details
*/
public void addTable1Details()
{
String str[][] = {{"aaa", "bbb", "", "", "eee"},
{"fff", "ggg", "hhh", "iii", "jjj"}};
for(int i=0;i -1 && row < movesupported =" (MOVE" dl =" (JTable.DropLocation)" dcol =" (JTable.DropLocation)" row =" dl.getRow();"> -1 && row < table1.getRowCount()) {
} else {
return false;
}
int intcol = dcol.getColumn();
// Allowing drop only for C and D column for Table 1
if (intcol != 2 || intcol != 3) {
return false;
}
// fetch the data and bail if this fails
String data;
try {
data = (String) support.getTransferable().getTransferData(
DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e) {
return false;
} catch (IOException e) {
return false;
}
System.out.println("data = "+data);
if (intcol == 2) {
// if field is not empty existing value will add in Table 2
if (table1.getValueAt(row, 2) != null && !table1.getValueAt(row, 2).toString().equalsIgnoreCase("")) {
String strEID = table1.getValueAt(row, 2).toString();
// adding table2
tm2.addRow(new String[]{strEID});
}
table1.setValueAt(data, row, intcol);
table1.repaint();
}
else if (intcol == 3) {
// if field is not empty existing value will add in Table 2
if (table1.getValueAt(row, 3) != null && !table1.getValueAt(row, 3).toString().equalsIgnoreCase("")) {
String strEID = table1.getValueAt(row, 3).toString();
// adding table2
tm2.addRow(new String[]{strEID});
}
table1.setValueAt(data, row, intcol);
table1.repaint();
}
return true;
}
};
return dnd1;
}
/**
* TransferHandler for Table2
* @return TransferHandler for Table2
*/
public TransferHandler getDND2() {
TransferHandler dnd2 = new TransferHandler() {
public int getSourceActions(JComponent comp) {
return MOVE;
}
private int index = -1;
// dad data reference
String transferData = "";
public Transferable createTransferable(
JComponent comp) {
index = table2.getSelectedRow();
// fetching selected data
transferData = table2.getValueAt(table2.getSelectedRow(), 0).toString();
return new StringSelection(transferData);
}
public void exportDone(JComponent comp, Transferable trans, int action) {
if (action != MOVE) {
return;
}
tm2.removeRow(index);
}
};
return dnd2;
}
}
I followed below steps,
* Creating two(table1 and table2) non editable table with single selection(drag and drop for single cell values).
* In table1 only C and D column only u can drop.
* table2 drop cell is empty then table1 drag value drop in table1 and that value will deleted from table2.
* drop cell is not empty then existing table1 value will be added to table2.
My program as below
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/**
* DADTableDemo : Example of drag and drop option between two tables
* @author R.Amirtharaj
*/
public class DADTableDemo {
/** Main Frame */
public JFrame myframe = new JFrame("Drag and drop demo");
/** Table 1*/
public JTable table1 = new JTable();
/** Table 2*/
public JTable table2 = new JTable();
/** Table1 Model */
public DefaultTableModel tm1 = new DefaultTableModel(0, 5) {
public boolean isCellEditable(int row, int col) {
return false;
}
};
/** Table2 Model */
public DefaultTableModel tm2 = new DefaultTableModel(0, 5) {
public boolean isCellEditable(int row, int col) {
return false;
}
};
/** Table1 ColumnNames */
public String[] proptbtitle1 = {"A", "B", "C(Drop will allow)", "D(Drop will allow)", "E"};
/** Table2 ColumnNames */
public String[] proptbtitle2 = {"Z"};
// Constructor
public static void main(String args[])
{
DADTableDemo demo = new DADTableDemo();
demo.create();
}
// create Drag and Drop demo
public void create()
{
JPanel pan = new JPanel();
// Table 1 related
tm1.setColumnIdentifiers(proptbtitle1);
table1.setModel(tm1);
table1.getTableHeader().setReorderingAllowed(false);
addTable1Details();
table1.setDragEnabled(true);
table1.setDropMode(DropMode.ON);
table1.setSelectionMode(0);
table1.getTableHeader().setReorderingAllowed(false);
// Add DAD
TransferHandler dnd1 = getDND1();
table1.setTransferHandler(dnd1);
// add table1 to scrollpane
JScrollPane sp1 = new JScrollPane(table1);
// add to panel
pan.add(sp1);
// Table 1 related
tm2.setColumnIdentifiers(proptbtitle2);
table2.setModel(tm2);
table2.getTableHeader().setReorderingAllowed(false);
addTable2Details();
table2.setDragEnabled(true);
table2.setSelectionMode(0);
// Add DAD
TransferHandler dnd2 = getDND2();
table2.setTransferHandler(dnd2);
// add table1 to scrollpane
JScrollPane sp2 = new JScrollPane(table2);
// add to panel
pan.add(sp2);
// add to frame
myframe.getContentPane().add(pan);
myframe.pack();
myframe.setVisible(true);
}
/**
* Table1 details
*/
public void addTable1Details()
{
String str[][] = {{"aaa", "bbb", "", "", "eee"},
{"fff", "ggg", "hhh", "iii", "jjj"}};
for(int i=0;i
} else {
return false;
}
int intcol = dcol.getColumn();
// Allowing drop only for C and D column for Table 1
if (intcol != 2 || intcol != 3) {
return false;
}
// fetch the data and bail if this fails
String data;
try {
data = (String) support.getTransferable().getTransferData(
DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e) {
return false;
} catch (IOException e) {
return false;
}
System.out.println("data = "+data);
if (intcol == 2) {
// if field is not empty existing value will add in Table 2
if (table1.getValueAt(row, 2) != null && !table1.getValueAt(row, 2).toString().equalsIgnoreCase("")) {
String strEID = table1.getValueAt(row, 2).toString();
// adding table2
tm2.addRow(new String[]{strEID});
}
table1.setValueAt(data, row, intcol);
table1.repaint();
}
else if (intcol == 3) {
// if field is not empty existing value will add in Table 2
if (table1.getValueAt(row, 3) != null && !table1.getValueAt(row, 3).toString().equalsIgnoreCase("")) {
String strEID = table1.getValueAt(row, 3).toString();
// adding table2
tm2.addRow(new String[]{strEID});
}
table1.setValueAt(data, row, intcol);
table1.repaint();
}
return true;
}
};
return dnd1;
}
/**
* TransferHandler for Table2
* @return TransferHandler for Table2
*/
public TransferHandler getDND2() {
TransferHandler dnd2 = new TransferHandler() {
public int getSourceActions(JComponent comp) {
return MOVE;
}
private int index = -1;
// dad data reference
String transferData = "";
public Transferable createTransferable(
JComponent comp) {
index = table2.getSelectedRow();
// fetching selected data
transferData = table2.getValueAt(table2.getSelectedRow(), 0).toString();
return new StringSelection(transferData);
}
public void exportDone(JComponent comp, Transferable trans, int action) {
if (action != MOVE) {
return;
}
tm2.removeRow(index);
}
};
return dnd2;
}
}
Wednesday, December 1, 2010
GeneralizedHashMap
We need to create one key with multiple values(like HashMap) then You can follow as belo
public class GHashMap {
public ArrayList alUid = new ArrayList();
public ArrayList al1 = new ArrayList();
public ArrayList al2 = new ArrayList();
public void add(String strUid,String str1,String str2)
{
alUid.add(strUid);
al1.add(str1);
al2.add(str2);
}
public String getValue1(String strUid)
{
String strVal1 = null;
if(alUid.contains(strUid))
{
int intIndex = alUid.indexOf(strUid);
strVal1 = al1.get(intIndex).toString();
}
return strVal1;
}
public String getValue2(String strUid)
{
String strVal2 = null;
if(alUid.contains(strUid))
{
int intIndex = alUid.indexOf(strUid);
strVal2 = al2.get(intIndex).toString();
}
return strVal2;
}
public String getUid(String strVal1,String strVal2)
{
String strid = null;
if((al1.contains(strVal1)) && (al1.contains(strVal1)))
{
int size = al1.size();
for(int i=0;i {
if(al1.get(i).equals(strVal1))
{
if(al2.get(i).equals(strVal2))
{
strid = alUid.get(i).toString();
}
}
}
}
return strid;
}
}
public class GHashMap {
public ArrayList alUid = new ArrayList();
public ArrayList al1 = new ArrayList();
public ArrayList al2 = new ArrayList();
public void add(String strUid,String str1,String str2)
{
alUid.add(strUid);
al1.add(str1);
al2.add(str2);
}
public String getValue1(String strUid)
{
String strVal1 = null;
if(alUid.contains(strUid))
{
int intIndex = alUid.indexOf(strUid);
strVal1 = al1.get(intIndex).toString();
}
return strVal1;
}
public String getValue2(String strUid)
{
String strVal2 = null;
if(alUid.contains(strUid))
{
int intIndex = alUid.indexOf(strUid);
strVal2 = al2.get(intIndex).toString();
}
return strVal2;
}
public String getUid(String strVal1,String strVal2)
{
String strid = null;
if((al1.contains(strVal1)) && (al1.contains(strVal1)))
{
int size = al1.size();
for(int i=0;i
if(al1.get(i).equals(strVal1))
{
if(al2.get(i).equals(strVal2))
{
strid = alUid.get(i).toString();
}
}
}
}
return strid;
}
}
Subscribe to:
Posts (Atom)