Home arrow static arrow Java Programming [Archive] - JTable doesnt populate new data ..
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - JTable doesnt populate new data ..
This topic has 3 replies on 1 page.

Posts:5
Registered: 8/2/04
JTable doesnt populate new data ..  
Aug 9, 2004 2:10 PM



 
Hi I have a Jtable which populate the data from the database properly. However when i get data for a new date it still shows old data... I'm calling firetabledatachanged(). i have no clue why its doing so..
I'm using Defaulttable model

Heres my code

public class ProductionMaintenance_2 extends javax.swing.JDialog {
ProdMaintenance dbu = new ProdMaintenance();
ListSelectionModel rowSM;
private Vector initialVector = new Vector();
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy");
private Date date = new Date();
private Vector colNames = new Vector();
private Vector data = new Vector();
private ProductionMaintenanceTableModel model;
int selectedRow;
Vector innerVector = new Vector();
ProdMaintenanceBean[] beanArray = null;

public ProductionMaintenance_2() {
setModal(true);
data = dbu.getValues(DATE_FORMAT.format(date));
System.out.println(((Vector)data.get(0)).size());
getCodes(data);
beanArray = dbu.getProdMaintenanceArray();
initComponents();
setBounds(200,200, 900, 500);
setResizable(false);
}

private void initComponents() {
colNames.add("Part No"); colNames.add("Description"); colNames.add("Produced");
colNames.add("Released");colNames.add("Comments");/
java.awt.GridBagConstraints gridBagConstraints;
jPanel1 = new javax.swing.JPanel();
dateField = new javax.swing.JTextField();
dateField.setText(DATE_FORMAT.format(date));
calendarButton = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
saveButton = new javax.swing.JButton();
closeButton = new javax.swing.JButton();
tablePane = new javax.swing.JScrollPane();
productionMaintenanceTable = new javax.swing.JTable();

getContentPane().setLayout(new java.awt.GridBagLayout());

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Production Maintenance");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

jPanel1.setLayout(new java.awt.GridBagLayout());

dateField.setFont(new java.awt.Font("Dialog", 0, 18));
dateField.setToolTipText("");
dateField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
dateFieldActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.ipadx = 136;
gridBagConstraints.ipady = 2;
gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
jPanel1.add(dateField, gridBagConstraints);

calendarButton.setText("Cal");
calendarButton.setToolTipText("Cal");
calendarButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calendarButtonActionPerformed(evt);
actionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
gridBagConstraints.ipadx = 9;
gridBagConstraints.ipady = 4;
gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 20);
jPanel1.add(calendarButton, gridBagConstraints);

jLabel1.setText("Production Date");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 9;
gridBagConstraints.ipady = 14;
jPanel1.add(jLabel1, gridBagConstraints);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.ipadx = 6;
gridBagConstraints.insets = new java.awt.Insets(33, 35, 0, 0);
getContentPane().add(jPanel1, gridBagConstraints);

jPanel2.setLayout(new java.awt.GridLayout(3, 0, 0, 20));

jPanel2.setMinimumSize(new java.awt.Dimension(100, 150));
jPanel2.setPreferredSize(new java.awt.Dimension(100, 118));
saveButton.setForeground(new java.awt.Color(0, 153, 153));
saveButton.setIcon(new javax.swing.ImageIcon("C:\\FoundryDefect\\icons
save16.gif"));
saveButton.setText("Save");
saveButton.setToolTipText("Saves the changes");
saveButton.setName("save");
jPanel2.add(saveButton);
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveButtonActionPerformed(evt);
actionPerformed(evt);
}
});

closeButton.setForeground(new java.awt.Color(0, 153, 153));
closeButton.setIcon(new javax.swing.ImageIcon("C:\\FoundryDefect\\icons
close16.gif"));
closeButton.setText("Close");
closeButton.setToolTipText("Closes this window");
closeButton.setName("close");
jPanel2.add(closeButton);
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeButtonActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.ipadx = 16;
gridBagConstraints.ipady = 2;
gridBagConstraints.insets = new java.awt.Insets(264, 390, 0, 62);
getContentPane().add(jPanel2, gridBagConstraints);

tablePane.setMinimumSize(new java.awt.Dimension(453, 395));
tablePane.setPreferredSize(new java.awt.Dimension(430, 395));
//initTable();
productionMaintenanceTable = new javax.swing.JTable();
model = new ProductionMaintenanceTableModel(data,colNames);
productionMaintenanceTable.setModel(model);
/*{
boolean[] canEdit = new boolean [] {
false, false, true, true, true
};

public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});*/
TableCellEditor tableCellEditor = new CustomTextFieldEditor(productionMaintenanceTable,new JTextField(),100,true);
TableCellEditor tableCellEditor1 = new CustomTextFieldEditor(productionMaintenanceTable,new JTextField(),100,true);
TableCellEditor tableCellEditor2 = new CustomTextFieldEditor(productionMaintenanceTable,new JTextField(),100,true);
productionMaintenanceTable.getColumnModel().getColumn(2).setCellEditor(tableCellEditor);
productionMaintenanceTable.getColumnModel().getColumn(3).setCellEditor(tableCellEditor1);
productionMaintenanceTable.getColumnModel().getColumn(4).setCellEditor(tableCellEditor2);
productionMaintenanceTable.setToolTipText("");
productionMaintenanceTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
removeUnwantedColumns();
//productionMaintenanceTable.getTableHeader().setReorderingAllowed(false);
//productionMaintenanceTable.getTableHeader().setResizingAllowed(false);
productionMaintenanceTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
ListSelectionModel rowSM = productionMaintenanceTable.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) return;
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
//System.out.println("No Rows Selected");
} else {
selectedRow = lsm.getMinSelectionIndex();
//System.out.println("Selected Row is "+selectedRow);
}
}
});

productionMaintenanceTable.setMinimumSize(new java.awt.Dimension(300, 300));
productionMaintenanceTable.setRowHeight(20);
tablePane.setViewportView(productionMaintenanceTable);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.ipadx = 270;
gridBagConstraints.insets = new java.awt.Insets(9, 22, 0, 182);
getContentPane().add(tablePane, gridBagConstraints);
pack();
}

private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
String[] update = model.getUpdatedIds();
System.out.println("Leng of Update is "+update.length);
Vector insertVector = new Vector();
Vector updateVector = new Vector();
//Add first
for(int i=0;i<update.length;i++){
if(update.trim().length() > 0){
updateVector.add(getNewValues(update));
}
}
}

private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
System.exit(0);
}

private void calendarButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
DateChooser dateChooser = new DateChooser(this,"Select a Date");
Date newDate = dateChooser.select(new Date(dateField.getText()));
if(newDate != null){
dateField.setText(DATE_FORMAT.format(newDate));
data = dbu.getValues(DATE_FORMAT.format(newDate));
getCodes(data);
model.addRow();
//model.setDataVector(data,colNames);
//initTable();
productionMaintenanceTable.updateUI();
beanArray = dbu.getProdMaintenanceArray();
//model.setDataVector(data,colNames);
((ProductionMaintenanceTableModel)productionMaintenanceTable.getModel()).fireTableStructureChanged();
removeUnwantedColumns();
}
//dateChooser.show();
}

private void dateFieldActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
data = dbu.getValues(dateField.getText());
getCodes(data);
productionMaintenanceTable.updateUI();
((ProductionMaintenanceTableModel)productionMaintenanceTable.getModel()).fireTableStructureChanged();
beanArray = dbu.getProdMaintenanceArray();
model.addRow();
//model.setDataVector(data,colNames);
//initTable();
//model.setDataVector(data,colNames);
//model.fire();
removeUnwantedColumns();
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new ProductionMaintenance_2().show();
}

private void getCodes(Vector a){
Vector tempV = new Vector();
if( a != null && a.size() > 0){
for(int i=0;i<a.size();i++){
tempV = (Vector)a.get(i);
initialVector.add(tempV.get(0));
}
}
}

public boolean checkDate(String dateString) throws Exception{
DATE_FORMAT.setLenient(false);
return (DATE_FORMAT.parse(dateString, new ParsePosition(0)) != null);
}

public ProdMaintenanceBean getNewValues(String value){
ProdMaintenanceBean bean = new ProdMaintenanceBean();
Vector dataV = model.getDataVector();
Vector newVector = new Vector();
Vector newVector1 = new Vector();
String t ="";
if(dataV != null){
for(int i=0;i<dataV.size();i++){
innerVector = (Vector)dataV.get(i);
newVector1 = (Vector)initialVector.get(i);
if(innerVector != null){
if(innerVector.get(0).toString().equalsIgnoreCase(value)){
newVector.add(innerVector);
bean.setPartNo(innerVector.get(0).toString());
bean.setDesc(innerVector.get(1).toString());
bean.setProduced(innerVector.get(2).toString());
bean.setRelaesed(innerVector.get(3).toString());
bean.setComments(innerVector.get(4).toString());
System.out.println("Trans Date is "+innerVector.get(5).toString());
bean.setTranDate(innerVector.get(5).toString());
bean.setFiscalYear(innerVector.get(6).toString());
System.out.println("Fiscal Year is "+innerVector.get(6).toString());
}
}
}
}
return bean;
}

private void removeUnwantedColumns(){
/*TableColumnModel columnModel = productionMaintenanceTable.getColumnModel();
columnModel.removeColumn(columnModel.getColumn( 5 ) );
columnModel.removeColumn(columnModel.getColumn( 5 ) ); */
}

public void actionPerformed(java.awt.event.ActionEvent e) {
}

private void initTable(){
TableCellEditor tableCellEditor = new CustomTextFieldEditor(productionMaintenanceTable,new JTextField(),100,false);
TableCellEditor tableCellEditor1 = new CustomTextFieldEditor(productionMaintenanceTable,new JTextField(),100,false);
TableCellEditor tableCellEditor2 = new CustomTextFieldEditor(productionMaintenanceTable,new JTextField(),100,false);
productionMaintenanceTable.getColumnModel().getColumn(2).setCellEditor(tableCellEditor);
productionMaintenanceTable.getColumnModel().getColumn(3).setCellEditor(tableCellEditor1);
productionMaintenanceTable.getColumnModel().getColumn(4).setCellEditor(tableCellEditor2);
productionMaintenanceTable.setToolTipText("");
productionMaintenanceTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
removeUnwantedColumns();
//productionMaintenanceTable.getTableHeader().setReorderingAllowed(false);
//productionMaintenanceTable.getTableHeader().setResizingAllowed(false);
productionMaintenanceTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
rowSM = productionMaintenanceTable.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) return;
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
//System.out.println("No Rows Selected");
} else {
selectedRow = lsm.getMinSelectionIndex();
//System.out.println("Selected Row is "+selectedRow);
}
}
});
}
// Variables declaration
private javax.swing.JButton calendarButton;
private javax.swing.JButton closeButton;
private javax.swing.JTextField dateField;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTable productionMaintenanceTable;
private javax.swing.JButton saveButton;
private javax.swing.JScrollPane tablePane;

}

 

Posts:343
Registered: 7/13/04
Re: JTable doesnt populate new data ..  
Aug 9, 2004 2:12 PM (reply 1 of 3)



 
dont give us all your code, be specific
 

Posts:458
Registered: 7/1/03
Re: JTable doesnt populate new data ..  
Aug 9, 2004 2:34 PM (reply 2 of 3)



 
if you dont use code tags I will beat you to death with a nerf bat. Please look at the Formatting help link above.
 

Posts:24,517
Registered: 98-02-27
Re: JTable doesnt populate new data ..  
Aug 9, 2004 8:20 PM (reply 3 of 3)



 
I agree completely with the previous two comments and I'll add my own:

"There is a Swing forum for Swing related questions. JTable is a Swing component....."

Here is a [url http://forum.java.sun.com/thread.jsp?forum=57&thread=504709]post[/url] from the Swing forum that shows how to repopulate a table with new data.
 
This topic has 3 replies on 1 page.