Dear all,
I have these code below (netbeans) :
All cells in columns "saldo Awal" are set to 58000000000.00, but appear in cell is 5.8E9
What should I fix in my code ?
Thanks,
Kusno
import javax.swing.DefaultCellEditor;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.Component;
import java.text.NumberFormat;
import javax.swing.JTable;
import javax.swing.JOptionPane;
import java.sql.*;
import javax.swing.JTable.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.*;
import java.lang.Math.*;
import javax.swing.JLabel.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.text.*;
import java.util.*;
public class FrmTrxSaldoAwal extends javax.swing.JFrame {
String[] strJdl = {"Kode Anggaran","Nama Anggaran","Group","Saldo Awal"};
DefaultTableModel tabMode;
public FrmTrxSaldoAwal()
{
tabMode = new DefaultTableModel(null,strJdl)
{
public boolean isCellEditable(int row, int column)
{
if(column <= 2)
{
return false;
}
return true;
}
};
initComponents();
formatColumn();
isiTable();
}
void isiTable()
{
Data clsData = new Data();
lbTahun.setText(clsData.getPeriodeAktif());
String SQL = "SELECT masterdetailanggaran.KodeDetailAnggaran, masterdetailanggaran.NamaDetailAnggaran, mastergroupanggaran.NamaGroupAnggaran FROM masterdetailanggaran INNER JOIN mastergroupanggaran on masterdetailanggaran.KodeGroupAnggaran = mastergroupanggaran.KodeGroupAnggaran ORDER BY KodeDetailAnggaran";
tabMode.setRowCount(0);
try{
Koneksi getCn = new Koneksi();
Connection cn = getCn.getConnection();
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery(SQL);
rs.last();
int row = rs.getRow();
rs.beforeFirst();
int i=0;
pb.setValue(0);
int maju;
double majud;
while(rs.next())
{
String kodeAnggaran = rs.getString(1);
String namaAnggaran = rs.getString(2);
String namaGroup = rs.getString(3);
double saldoAwal = 5800000000.00 ;//getSaldoAwal(kodeAnggaran);
Object[] data = {kodeAnggaran,namaAnggaran,namaGroup,saldoAwal};
tabMode.addRow(data);
i = i+1;
majud = (i/row) * 100;
maju = (int)majud;
pb.setValue(maju);
}
rs.close();
st.close();
cn.close();
pb.setValue(0);
}
catch(SQLException e)
{
}
}
private Object getSaldoAwal(String kodeAnggaran)
{
String SQL = "SELECT SaldoAwal FROM trxanggaran WHERE KodeDetailAnggaran = '" + kodeAnggaran + "' AND Tahun = '" + lbTahun.getText().trim() + "'";
float SaldoAwal = 0;
try{
Koneksi getCn = new Koneksi();
Connection cn = getCn.getConnection();
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery(SQL);
if(rs.next())
{
SaldoAwal = rs.getFloat(1);
}
rs.close();
st.close();
cn.close();
}
catch(SQLException e)
{
}
return SaldoAwal;
}
class numericOnly extends DefaultCellEditor
{
numericOnly()
{
super(new JTextField());
}
public boolean stopCellEditing()
{
try
{
String editingValue = (String)getCellEditorValue();
char[] source = editingValue.toCharArray();
char[] result = new char[source.length];
int j = 0;
for (int i = 0; i < result.length; i++)
{
char x = source;
if (Character.isDigit(source) || (x=='.'))
{
result[j++] = source;
}
else
{
JTextField textField = (JTextField)getComponent();
textField.setBorder(new LineBorder(Color.red));
textField.selectAll();
textField.requestFocusInWindow();
JOptionPane.showMessageDialog(tblAnggaran,"Saldo awal harus berupa angka.","Perhatian!",JOptionPane.ERROR_MESSAGE);
return false;
}
}
}
catch(ClassCastException exception)
{
return false;
}
return super.stopCellEditing();
}
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column)
{
Component c = super.getTableCellEditorComponent(
table, value, isSelected, row, column);
((JComponent)c).setBorder(new LineBorder(Color.black));
return c;
}
}
private class CurrencyRenderer extends DefaultTableCellRenderer
{
public CurrencyRenderer()
{
super();
setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
}
public void setValue(Double value)
{
// if ((value != null) && (value instanceof Number))
// {
// Number numberValue = (Number) value;
// NumberFormat formatter = NumberFormat.getNumberInstance();
// formatter.setGroupingUsed(true);
// formatter.setMaximumFractionDigits(2);
// value = formatter.format(numberValue);
// }
super.setValue(value);
}
}
void formatColumn()
{
tblAnggaran.getColumnModel().getColumn(0).setPreferredWidth(60);
tblAnggaran.getColumnModel().getColumn(1).setPreferredWidth(190);
tblAnggaran.getColumnModel().getColumn(2).setPreferredWidth(100);
// tblAnggaran.getColumnModel().getColumn(3).setCellRenderer(new CurrencyRenderer());
tblAnggaran.getColumnModel().getColumn(3).setPreferredWidth(100);
// TableCellEditor fce = new numericOnly();
// tblAnggaran.setDefaultEditor(Object.class, fce);
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
lbTahun = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
tblAnggaran = new javax.swing.JTable();
cmdSave = new javax.swing.JButton();
cmdExit = new javax.swing.JButton();
pb = new javax.swing.JProgressBar();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Saldo Awal");
setAlwaysOnTop(true);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel1.setText("Tahun :");
lbTahun.setFont(new java.awt.Font("Tahoma", 1, 12));
tblAnggaran.setModel(tabMode);
tblAnggaran.addInputMethodListener(new java.awt.event.InputMethodListener() {
public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
}
public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
tblAnggaranInputMethodTextChanged(evt);
}
});
jScrollPane1.setViewportView(tblAnggaran);
cmdSave.setText("Simpan");
cmdSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdSaveActionPerformed(evt);
}
});
cmdExit.setText("Keluar");
cmdExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdExitActionPerformed(evt);
}
});
pb.setStringPainted(true);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
.add(layout.createSequentialGroup()
.add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 53, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(lbTahun, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 53, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 56, Short.MAX_VALUE)
.add(pb, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 480, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(cmdSave)
.add(18, 18, 18)
.add(cmdExit)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(lbTahun, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(pb, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(cmdExit)
.add(cmdSave))
.addContainerGap())
);
pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension dialogSize = getSize();
setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
}// </editor-fold>
private boolean dataSudahBenar()
{
Data data = new Data();
String x = data.getFieldStringValue("trxperiode", "tahun", lbTahun.getText().trim(), "SaldoAwaldisimpan");
if(x.equals("1"))
{
JOptionPane.showMessageDialog(this,"Saldo awal sudah tidak bisa diubah lagi. Sudah dikunci.","Informasi",JOptionPane.OK_OPTION);
return false;
}
return true;
}
private void cmdSaveActionPerformed(java.awt.event.ActionEvent evt) {
int result = JOptionPane.showConfirmDialog(this,"Data yang telah disimpan tidak dapat dirubah kembali.\nIngin tetap simpan data ?","Konfirmasi",JOptionPane.YES_NO_OPTION);
String SQL;
if(result==JOptionPane.YES_OPTION)
{
Data Exec = new Data();
double row = tabMode.getRowCount();
int maju = 0;
double majud = 0.0;
pb.setMaximum(100);
for(int i=0; i<row; i++)
{
String kodeAnggaran = (String)tabMode.getValueAt(i, 0);
Object s = tabMode.getValueAt(i, 3);
//double saldoAwal = (Double)tabMode.getValueAt(i,3);
System.out.println(s);
double saldoAwal = (Double)s;
System.out.println(saldoAwal);
SQL = "UPDATE trxanggaran SET SaldoAwal = " + saldoAwal + " WHERE KodeDetailAnggaran = '" + kodeAnggaran + "' AND Tahun = '" + lbTahun.getText().trim() + "'";
Exec.ExecuteSQL(SQL);
majud = (i/row) * 100;
maju = (int)majud;
pb.setValue(maju);
}
SQL = "UPDATE trxperiode SET SaldoAwaldiSimpan = '1' WHERE Tahun = '" + lbTahun.getText().trim() + "'";
Exec.ExecuteSQL(SQL);
tabMode.setRowCount(0);
pb.setValue(0);
}
}
private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
setVisible(false);
}
private void tblAnggaranInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton cmdExit;
private javax.swing.JButton cmdSave;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lbTahun;
private javax.swing.JProgressBar pb;
private javax.swing.JTable tblAnggaran;
// End of variables declaration
}