| Author |
Mukesh
|
Mukesh Pandit
Greenhorn
Joined: Nov 19, 2002
Posts: 2
|
|
Hello! I have a table that contains the details of a patient. But when I restart the program next time, it does not store any information. Please tell me how to make the program to store the data next time I restart the same program. I am sending the code. import javax.swing.table.AbstractTableModel; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; class PATIENT { private String name, age, sex; public PATIENT(String name) { setName(name); age = ""; sex = ""; } public PATIENT() { this(""); } public void setName(String name) { this.name = name; } public void setAge(String age) { this.age = age; } public void setSex(String sex) { this.sex = sex; } public String getName() { return name; } public String getAge() { return age; } public String getSex() { return sex; } public String toString() { return name; } } class myModel extends AbstractTableModel { private PATIENT patient; private String row_names[] = { "NAME", "AGE", "SEX" }; private String column_names[] = {"PARAMETERS","VALUES"}; public myModel() { super(); patient = new PATIENT(""); } public void setPATIENT(PATIENT p) { patient = p; fireTableDataChanged(); } public PATIENT getPATIENT() { return patient; } public String getColumnName( int col ) { return column_names[col]; } public int getColumnCount( ) { return column_names.length; } public int getRowCount( ) { return row_names.length; } public void setValueAt(Object value, int row, int col) { if (col > 0) { String newValue = (String) value; switch (row) { case 0 : patient.setName(newValue); break; case 1 : patient.setAge(newValue); break; case 2 : patient.setSex(newValue); break; } } } public Object getValueAt(int row, int col) { if (col == 0) { return row_names[row]; } else { switch (row) { case 0 : return patient.getName(); case 1 : return patient.getAge(); case 2 : return patient.getSex(); default : return ""; } } } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public boolean isCellEditable(int row, int col) { if (col == 0) return false; else return true; } } public class PERSON_TABLE extends JFrame implements ActionListener { myModel tmodel = new myModel(); JTable table = new JTable( tmodel ); JButton enter_info = new JButton( "Enter info" ), update_info = new JButton( "Update info" ), save_info = new JButton( "save info" ), print_info = new JButton( "print info" ); JPanel panel = new JPanel( ); DefaultListModel m = new DefaultListModel( ); JList list = new JList( m ); PERSON_TABLE( ) { super( "table of patients" ); PATIENT mukesh = new PATIENT("Mukesh"); m.addElement(mukesh); panel .add( enter_info ); panel .add( update_info ); enter_info .addActionListener( this ); update_info .addActionListener( this ); getContentPane( ).add( new JScrollPane( list ), BorderLayout.CENTER); getContentPane( ).add( panel, BorderLayout.SOUTH ); setDefaultCloseOperation( EXIT_ON_CLOSE ); pack( ); show( ); } public void actionPerformed(ActionEvent e) { if( e.getSource( ) == enter_info ) { PATIENT p = new PATIENT(); m.addElement(p); tmodel.setPATIENT(p); JPanel option_panel = new JPanel( ); option_panel.add( save_info ); option_panel.add( print_info ); save_info.addActionListener( this ); print_info.addActionListener( this ); JFrame frame = new JFrame( m.getElementAt( list.getSelectedIndex( )).toString( )); frame . getContentPane( ).add( new JScrollPane( new JTable( tmodel )), BorderLayout.CENTER ); frame . getContentPane( ).add( option_panel, BorderLayout.SOUTH ); frame . setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); frame . pack( ); frame . show( ); } if( e.getSource( ) == save_info ) { PATIENT p = tmodel.getPATIENT(); int index = m.indexOf(p); m.setElementAt(p, index); } if( e.getSource( ) == update_info ) { if (list.getSelectedIndex() != -1) { PATIENT p = (PATIENT) m.getElementAt( list.getSelectedIndex() ); tmodel.setPATIENT(p); JPanel option_panel = new JPanel( ); option_panel.add( save_info ); option_panel.add( print_info ); save_info.addActionListener( this ); print_info.addActionListener( this ); JFrame frame = new JFrame( m.getElementAt( list.getSelectedIndex( )).toString( )); frame . getContentPane( ).add( new JScrollPane( new JTable( tmodel )), BorderLayout.CENTER ); frame . getContentPane( ).add( option_panel, BorderLayout.SOUTH ); frame . setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); frame . pack( ); frame . show( ); } } } public static void main( String s[] ) { new PERSON_TABLE( ); } } < end of code >
|
na
|
 |
Younes Essouabni
Ranch Hand
Joined: Jan 13, 2002
Posts: 479
|
|
If you need persistance information, you should serialize it. IMHO, the better way is to make a connection to a DB. You may create easily a db with access and then connect to it via trough ODBC. Hope it helps you!
|
Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
|
 |
 |
|
|
subject: Mukesh
|
|
|