I have problem in my rendered jtable.i want to add those values in my database,i am able to do it,but the thing is what i want to do,on clicking add button automatically my row has to shift downward and have to get ready for taking the input of the user.I am able to collect that values into the database but not able to do this event.i had already wasted three days but dnt know how to implimenet this in my jdialog ,jtable i am attaching screenshotaswell please see it and help me.I know that action listener and item listeners are those methods which can do this but i dnt know where to how to impliment them,as in picture you can see this is jtable rendered now i want to shift row down by clicking the add button onevery event and and finally when user will fill evertythng into the table we will save the data into the database
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ERP;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import net.proteanit.sql.DbUtils;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.table.AbstractTableModel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.*;
import javax.swing.event.TableModelListener;
import javax.swing.event.TableModelEvent;
/**
*
* @author rohtash
*/
public class SellStock extends javax.swing.JDialog
{
ResultSet rs=null;
PreparedStatement pt=null;
Connection con=new DBConn().connect();
JComboBox comboBox5 = new JComboBox();
DefaultTableModel def;
JTable mytable;
/**
* Creates new form SellStock
*/
public SellStock(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
SellStocks();
editablecombo();
Datecombo();
ItemCombo();
/*addtable.addActionListener(new Myactions());*/
/* CellEditable();*/
System.out.println("hello");
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jSlider1 = new javax.swing.JSlider();
jSeparator2 = new javax.swing.JSeparator();
jSeparator3 = new javax.swing.JSeparator();
jDesktopPane1 = new javax.swing.JDesktopPane();
jLabel1 = new javax.swing.JLabel();
CustomerName_Combo = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
Date_Combo = new javax.swing.JComboBox();
jScrollPane1 = new javax.swing.JScrollPane();
Tableeditor = new javax.swing.JTable();
addtable = new java.awt.Button();
button1 = new java.awt.Button();
button3 = new java.awt.Button();
button4 = new java.awt.Button();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
checkbox1 = new java.awt.Checkbox();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("SellStock");
setAlwaysOnTop(true);
setAutoRequestFocus(false);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
getContentPane().add(jSeparator2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 240, -1, -1));
getContentPane().add(jSeparator3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 272, 400, 0));
jLabel1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
jLabel1.setForeground(java.awt.Color.white);
jLabel1.setText("CustomerName");
jDesktopPane1.add(jLabel1);
jLabel1.setBounds(30, 20, 100, 20);
CustomerName_Combo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CustomerName_ComboActionPerformed(evt);
}
});
jDesktopPane1.add(CustomerName_Combo);
CustomerName_Combo.setBounds(370, 20, 160, 22);
jLabel2.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
jLabel2.setForeground(java.awt.Color.white);
jLabel2.setText("Date of Sale");
jDesktopPane1.add(jLabel2);
jLabel2.setBounds(30, 120, 90, 20);
jDesktopPane1.add(Date_Combo);
Date_Combo.setBounds(450, 120, 90, 22);
Tableeditor.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null}
},
new String [] {
"Item", "Description", "Location", "SellQuantity", "Price", "StkQuantity", "TotalPrice"
}
));
Tableeditor.addInputMethodListener(new java.awt.event.InputMethodListener() {
public void caretPositionchanged(java.awt.event.InputMethodEvent evt) {
TableeditorCaretPositionchanged(evt);
}
public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
}
});
jScrollPane1.setViewportView(Tableeditor);
jDesktopPane1.add(jScrollPane1);
jScrollPane1.setBounds(0, 160, 580, 170);
addtable.setLabel("ADD");
addtable.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addtableActionPerformed(evt);
}
});
jDesktopPane1.add(addtable);
addtable.setBounds(20, 340, 41, 24);
button1.setLabel("Sell");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button1ActionPerformed(evt);
}
});
jDesktopPane1.add(button1);
button1.setBounds(160, 510, 60, 24);
button3.setLabel("Cancel");
button3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button3ActionPerformed(evt);
}
});
jDesktopPane1.add(button3);
button3.setBounds(230, 510, 55, 24);
button4.setLabel("Help");
jDesktopPane1.add(button4);
button4.setBounds(300, 510, 42, 24);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane2.setViewportView(jTextArea1);
jDesktopPane1.add(jScrollPane2);
jScrollPane2.setBounds(20, 410, 520, 96);
checkbox1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
checkbox1.setForeground(java.awt.Color.white);
checkbox1.setLabel("Damaged Item");
jDesktopPane1.add(checkbox1);
checkbox1.setBounds(20, 380, 108, 19);
jLabel3.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
jLabel3.setForeground(java.awt.Color.white);
jLabel3.setText("Note");
jDesktopPane1.add(jLabel3);
jLabel3.setBounds(210, 380, 60, 20);
getContentPane().add(jDesktopPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 580, 540));
pack();
}// </editor-fold>
private void CustomerName_ComboActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/*private class Myactions implements ActionListener{
@Override
public void actionPerformed(ActionEvent event){
if(event.getSource()==addtable){
Object[]newdata={"","",""};
def.addRow(newdata);
}
}
}
*/
private void addtableActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
/* int rows = Tableeditor.getRowCount();
int colm = Tableeditor.getColumnCount();
int row=0;
String SellQuant =(String)Tableeditor.getValueAt(row, 3);
String Prices = (String)Tableeditor.getValueAt(row, 4);
String StockQuan =(String)Tableeditor.getValueAt(row, 5);
try {
//<editor-fold defaultstate="collapsed" desc="comment">
//</editor-fold>for
/*String sql="INSERT INTO customerdata (Customer_Name,Date) VALUES (?,?)";
pt=con.prepareStatement(sql);
pt.setString(1,CustomerName_Combo.getSelectedItem().toString());
} pt.setString(2,Date_Combo.getSelectedItem().toString());
pt.setString(3,Tableeditor.getValueAt(i, j).toString());*/
/*
String sql1="INSERT INTO Sell_Table(SellQuantity,StkQuantity,Price) VALUE (?,?,?)";
pt=con.prepareStatement(sql1);
pt.setString(1,SellQuant);
pt.setString(2, StockQuan);
pt.setString(3, Prices);
/* pt.setString(3,CustomerName_Combo.getSelectedItem().toString());
pt.setString(4,Date_Combo.getSelectedItem().toString());*/
/* pt.addBatch();
pt.executeBatch();
Tableeditor.firePropertyChange(StockQuan, colm, row);
Tableeditor.firePropertyChange(Prices, colm, row);
}
catch (SQLException ex) {
ex.printStackTrace();
}
dispose();
*/
}
private void button3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
dispose();
}
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void TableeditorCaretPositionchanged(java.awt.event.InputMethodEvent evt) {
// TODO add your handling code here:
}
public void editablecombo(){
CustomerName_Combo.setEditable(true);
CustomerName_Combo.getSelectedIndex();
CustomerName_Combo.addActionListener(Date_Combo);
/* String text1 = (String)CustomerName_Combo.getSelectedItem();*/
CustomerName_Combo.addItem("hello");
CustomerName_Combo.addItem("check");
CustomerName_Combo.addItem("Bye");
String choice = CustomerName_Combo.getSelectedItem().toString();
/* CustomerName_Combo.addActionListener(CustomerName_Combo);*/
}
public void Datecombo(){
Date_Combo.setEditable(true);
}
public void SellStocks(){
TableColumn ItemColumn = Tableeditor.getColumnModel().getColumn(0);
JComboBox comboBox5 = new JComboBox();
ItemColumn.setCellEditor(new DefaultCellEditor(comboBox5));
TableColumn Description = Tableeditor.getColumnModel().getColumn(1);
JComboBox comboBox1 = new JComboBox();
Description.setCellEditor(new DefaultCellEditor(comboBox1));
}
public void ItemCombo(){
TableColumn Description = Tableeditor.getColumnModel().getColumn(1);
JComboBox comboBox1 = new JComboBox();
Description.setCellEditor(new DefaultCellEditor(comboBox1));
TableColumn Location = Tableeditor.getColumnModel().getColumn(2);
JComboBox comboBox2 = new JComboBox();
Location.setCellEditor(new DefaultCellEditor(comboBox2));
TableColumn ItemColumn = Tableeditor.getColumnModel().getColumn(0);
JComboBox comboBox5 = new JComboBox();
ItemColumn.setCellEditor(new DefaultCellEditor(comboBox5));
try
{
String sql="select * from Itemdetails";
pt=con.prepareStatement(sql);
rs=pt.executeQuery();
while(rs.next())
{
String Item=rs.getString("ItemCode");
comboBox5.addItem(Item);
comboBox5.addActionListener(comboBox1);
comboBox5.addItemListener(null);
String Descriptions=rs.getString("ItemDescription");
comboBox1.addItem(Descriptions);
/* comboBox1.addActionListener(comboBox5);*/
/* String items = (String) comboBox5.getSelectedItem();
Object o =comboBox1.getAction();
if (o == null) {
comboBox1.setModel(new DefaultComboBoxModel());
} else {
comboBox1.setModel(new DefaultComboBoxModel((String[]) o));
}*/
}
/*String Locat=rs.getString("Location");
comboBox2.addItem(Locat);*/
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,ex);
}
}
public class CustomTableCellRenderer extends DefaultTableCellRenderer{}
/* public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
.setVisible(true);
...
} else {
label.setVisible(false);
}
}/*
/*public void CellEditable(){
String invSL = new Mixed_Calculation().toString();
int rows = Tableeditor.getRowCount();
for(int row = 0; row<rows ; row++)
{
String SellQuant = (String) Tableeditor.getValueAt(row, 3);
String Prices = (String) Tableeditor.getValueAt(row, 4);
}}*/
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(SellStock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SellStock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SellStock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SellStock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
SellStock dialog = new SellStock(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox CustomerName_Combo;
private javax.swing.JComboBox Date_Combo;
private javax.swing.JTable Tableeditor;
private java.awt.Button addtable;
private java.awt.Button button1;
private java.awt.Button button3;
private java.awt.Button button4;
private java.awt.Checkbox checkbox1;
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JSlider jSlider1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}

New Topic/Question
Reply


MultiQuote

|