• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with JTable & abtractTablemodel

 
Erwin Bredford
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
well i have some problem with a JTable it is meant to be updated at runtime by the click of the ADD button
right. the values in The JTextField are then stored on in a Vector then the addded vale is displayed in the JTabel but it is not doing that.
that a look. here are the neccessary classes
/** main application window
used to be an Internal frame but converted it for this example
*/
import javax.swing.*;
import java.awt.*;

public class Account extends JFrame {

/** Creates new form Account */
public Account() {


initComponents();
layPanels();
}
public static void main(String args[]){
Account account =new Account();
account.setSize(500,500);
account.show();
}
/** 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.
*/
private void initComponents() {
getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

setTitle("Account");
setToolTipText("Account");

pack();
}

private void layPanels(){
//box=Box.createVerticalBox();
tablePanel=new TablePanel();
entryPanel=new EntryPanel();
getContentPane().add(entryPanel);
getContentPane().add(Box.createRigidArea(new Dimension(0,5)));
getContentPane().add(tablePanel);
try {
setSelected(true);
} catch (java.beans.PropertyVetoException e1) {
e1.printStackTrace();
}
pack();
}

// Variables declaration - do not modify
// End of variables declaration
// Box box;
private TablePanel tablePanel ;
private EntryPanel entryPanel;
}
/**this class encapsulates a vector used in storing person object*/
import java.util.*;
public class AccountPeople {
private Vector people;
/** Creates a new instance of AccountPeople */
public AccountPeople() {
people=new Vector(40,10);
}
public Person get(int index){
return (Person)people.get(index);
}

public boolean add(Person someone){
return people.add(someone);
}

public Iterator iterator(){
return people.iterator();
}

public Vector getPeople(){
return people;
}
public int getSize(){
return people.size();
}
}
/**this class is apanel that contain text boxes
*and input control
* @author chinedu
*/
import javax.swing.*;
public class EntryPanel extends javax.swing.JPanel {

/** Creates new form EntryPanel */
public EntryPanel() {
initComponents();
}

/** 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.
*/
private void initComponents() {
LogNameLabel = new javax.swing.JLabel();
PasswordLabel = new javax.swing.JLabel();
TimeLabel = new javax.swing.JLabel();
AddButton = new javax.swing.JButton();
UpdateButton = new javax.swing.JButton();
RemoveButton = new javax.swing.JButton();
ClearButton = new javax.swing.JButton();
LogNameField = new javax.swing.JTextField();
PasswordField = new javax.swing.JTextField();
TimeBox = new javax.swing.JComboBox();
setLayout(null);
setBorder(new javax.swing.border.TitledBorder(null, "Data Entry", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 11), new java.awt.Color(255, 0, 102)));
setToolTipText("EntryPanel");
setPreferredSize(new java.awt.Dimension(350, 420));
LogNameLabel.setText("Login Name");
LogNameLabel.setToolTipText("Logname");
add(LogNameLabel);
LogNameLabel.setBounds(60, 30, 70, 16);
PasswordLabel.setText("Password");
PasswordLabel.setToolTipText("Password");
add(PasswordLabel);
PasswordLabel.setBounds(60, 70, 60, 16);
TimeLabel.setText("Time (Mins)");
TimeLabel.setToolTipText("Time");
add(TimeLabel);
TimeLabel.setBounds(60, 110, 70, 16);
AddButton.setText("Add");
AddButton.setToolTipText("Add");
AddButton.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
AddButton.setMaximumSize(new java.awt.Dimension(44, 20));
AddButton.setPreferredSize(new java.awt.Dimension(44, 20));
AddButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AddButtonActionPerformed(evt);
}
});
add(AddButton);
AddButton.setBounds(280, 30, 70, 20);
UpdateButton.setText("Update");
UpdateButton.setToolTipText("Update");
UpdateButton.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
add(UpdateButton);
UpdateButton.setBounds(280, 70, 70, 20);
RemoveButton.setText("Remove");
RemoveButton.setToolTipText("Remove");
RemoveButton.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
add(RemoveButton);
RemoveButton.setBounds(280, 110, 70, 20);
ClearButton.setText("Clear");
ClearButton.setToolTipText("Clear");
ClearButton.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
ClearButton.setPreferredSize(new java.awt.Dimension(44, 20));
add(ClearButton);
ClearButton.setBounds(280, 150, 70, 20);
LogNameField.setToolTipText("Enter Login name");
add(LogNameField);
LogNameField.setBounds(140, 30, 90, 20);
PasswordField.setToolTipText("Enter Password");
add(PasswordField);
PasswordField.setBounds(140, 70, 90, 20);
TimeBox.setMaximumRowCount(4);
TimeBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "5", "10", "30", "60", "120", "240" }));
TimeBox.setToolTipText("Time");
add(TimeBox);
TimeBox.setBounds(140, 110, 90, 25);
}
private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {
addPersonAction(); // Add your handling code here:
}

// Variables declaration - do not modify
private javax.swing.JButton UpdateButton;
private javax.swing.JLabel TimeLabel;
private javax.swing.JButton RemoveButton;
private javax.swing.JLabel LogNameLabel;
private javax.swing.JTextField PasswordField;
private javax.swing.JComboBox TimeBox;
private javax.swing.JTextField LogNameField;
private javax.swing.JButton ClearButton;
private javax.swing.JButton AddButton;
private javax.swing.JLabel PasswordLabel;
// End of variables declaration
AccountPeople people=new AccountPeople();

int sno=0;
String strTime;
/** this is the main problem i am having when user click on add button it should add the person to a vecto and pass it to the TableModel which should display on the Jtable but is not working
*/
public void addPersonAction(){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
try{

sno++ ;
String logname=LogNameField.getText();
String password=PasswordField.getText();
strTime =(String)TimeBox.getSelectedItem();

Long time=Long.valueOf(strTime.trim());
Person person=new Person(sno,logname,password,time.longValue());
people.add(person);
TablePanel.TabModel tabmodel=new TablePanel().new TabModel();
tabmodel.setAccount(people);
System.out.println("Entry people size "+people.getPeople().size());
}
catch(NullPointerException e){
JOptionPane.showMessageDialog(null,"Please Enter a Field ","Error",JOptionPane.ERROR_MESSAGE);
}
JOptionPane.showMessageDialog(null,"Please Enter a Field ","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
import java.io.Serializable;
public class Person implements Serializable{

private String logName;
private String password;
private long time;
private int sno;
/** Creates a new instance of Person */

/** the person classes that represents an object person*/
public Person(int sno,String logName,String password,long time) {
this.sno=sno;
this.logName=logName;
this.password=password;
this.time=time;
}
public Person(){
this(0,null,null,0);
}

public void setLogName(String logname){
this.logName=logName;
}
public void setPassword(String password){
this.password=password;
}
public void setTime(long time){
this.time=time;
}
public int getSno(){
return sno;
}
public void setSno(int sno){
this.sno=sno;
}
public String getLogName(){
return logName;
}
public String getPassword(){
return password;
}
public long getTime(){
return time;
}
}
/**
*
* @author chinedu
*/
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.util.*;
public class TablePanel extends JPanel {

public String[] COLUMNNAMES={"S/No","Login Name","Password","Time "};
// private AccountPerson accountPerson;
private JTable table;
private JScrollPane tablePane;
AccountPeople people;
/** Creates a new instance of TablePanel */
public TablePanel() {
setLayout(new BorderLayout());
// accountPerson=new accountPerson();
people=new AccountPeople();
table=new JTable(new TabModel());
table.setAutoCreateColumnsFromModel(true);
tablePane=new JScrollPane(table);
tablePane.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
tablePane.setToolTipText("scroll");
tablePane.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

add(tablePane,BorderLayout.CENTER);
}
/** the Panel containing the JTable and the TableModel**/
class TabModel extends AbstractTableModel{


Vector dataRows;

public void setAccount(AccountPeople people) {

if(people==null)
{ //accountPeople.clear();
fireTableChanged(null);
return;
}

int columns=COLUMNNAMES.length;//get number of columns

dataRows=new Vector();
Integer sno;
Long time;
Iterator iterator=people.iterator();
Vector rowData;

while(iterator.hasNext()){

Person obj=(Person)iterator.next();
System.out.println(obj.getSno()+obj.getLogName()+" "+obj.getPassword()+obj.getTime());
System.out.println("people size= "+people.getPeople().size());
sno=new Integer(obj.getSno());
time=new Long(obj.getTime());
Object[] data={sno,obj.getLogName(),obj.getPassword(),time};
rowData=new Vector(columns);

for(int i=0;i<columns;i++) {
rowData.addElement(data[i]);
System.out.println("row data in the for loop for looop"+rowData.size());
}


System.out.println("row data after for looop"+rowData.size());
dataRows.addElement(rowData);
System.out.println("after loop get row count"+getRowCount());
System.out.println("after loop tObject getValueAt(int row, int column)"+ getValueAt(0, 1));
}

System.out.println("data rows is"+dataRows.size());
fireTableChanged(null);
}


public String getColumnName(int column){
return COLUMNNAMES[column]==null?"No Name":COLUMNNAMES[column];

}
public int getColumnCount() {
return COLUMNNAMES.length;
}

public int getRowCount() {

if(dataRows==null)
return 0;

return dataRows.size();

}

public Object getValueAt(int row, int column) {

Vector tmprow = (Vector)dataRows.elementAt(row);
return tmprow.elementAt(column);


}
public boolean isCellEditable(int row, int column){
return false;
}

public Class getColumnClass(int c) {

return getValueAt(0, c).getClass();
}
}
}

Thanks please help me fix it thanks
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the UBB Code "CODE" tag to preserve your code formatting. It is painful to look at unformatted code.
And look into AbstractTableModel.fireRowsInserted(firstRow, lastRow). When you change the data model you have to let the view know it changed.
[ December 10, 2003: Message edited by: Joe Ess ]
[ December 10, 2003: Message edited by: Joe Ess ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!