2 Replies Latest reply: Oct 7, 2004 4:31 PM by 807587 RSS

    Abstract Table Model

    807587
      I am developing a monthly GUI calendar interface, which talks to the database to set appointments on particular date. Interface can navigate through to show dates, month and year. When the user clicks on the date button an other Table Model of AbstractTableModel 's interface opens up. For now, it is retrieving records from the database. But I have other buttons like Add and Save along with cancel. The 'cancel' button is working fine. I am concerned about 'Add' and 'Save' buttons. Here are the 4 classes that I use.
      * CalLatest.java - is the main class(works for the first interface). Once a date button is clicked thats when CalendarEvent.java gets triggered.
      * CalDataModel.java - is the second interface which deals with database to the list events for that date
      * CalendarEvent.java- also works with the second interface.
      * CalendarDB.java - deals with database connectivity and for now, it is retrieving records. I just need to add two of these features and I will be done. Please Help

      /*CalLatest.java - Main File */


      // Month Display: standard set-up

      import java.awt.*;//Container,Font,GridBagConstraints,GridBagLayout
      import java.awt.event.*;//ActionEvent,ActionListener,WindowAdapter,WindowEvent
      import java.util.*;//ArrayList,Calendar,GregorianCalendar,HashMap

      import javax.swing.*; //JButton,JFrame,JLabel,UIManager;


      //begin of CalLatest class
      public class CalLatest extends JFrame
      {
      //begin private data members
      private JFrame aFrame = null;
      protected JButton _dayButtons[] = new JButton[42];
      protected Calendar _calendar = new GregorianCalendar();
      protected HashMap _calendarTableList = null;
      private JLabel _dayLabels[] = new JLabel[7];
      private JLabel _monthLabel = null;
      private JLabel _yearLabel = null;
      protected JButton _scrollButtons[] = new JButton[2];
      protected int currentMonth = calendar.get(Calendar.MONTH), currentYear = calendar.get(Calendar.YEAR);

      private static final String[] MONTHS = { "January", "February", "March","April", "May", "June", "July", "August", "September",
      "October","November", "December" };

      private void initializeFrame()
           {
      aFrame = new JFrame("Calendar");
      aFrame.setTitle("Calendar Graphics");
      aFrame.setSize(600, 400);
      aFrame.setVisible(true);
      aFrame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
      System.exit(0);
      }
      });
      }

      private void initializePanels()
           {
      Container myContentPane = aFrame.getContentPane();
      GridBagLayout gbl = new GridBagLayout();
      gbl.columnWidths = new int [] {80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80};
      gbl.rowHeights = new int [] {35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35};
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.BOTH;
      myContentPane.setLayout(gbl);

      _scrollButtons[0] = new JButton("<<");
      _scrollButtons[1] = new JButton(">>");

      _dayButtons[0] = new JButton("1");
      _dayButtons[1] = new JButton("2");
      _dayButtons[2] = new JButton("3");
      _dayButtons[3] = new JButton("4");
      _dayButtons[4] = new JButton("5");
      _dayButtons[5] = new JButton("6");
      _dayButtons[6] = new JButton("7");
      _dayButtons[7] = new JButton("8");
      _dayButtons[8] = new JButton("9");
      _dayButtons[9] = new JButton("10");
      _dayButtons[10] = new JButton("11");
      _dayButtons[11] = new JButton("12");
      _dayButtons[12] = new JButton("13");
      _dayButtons[13] = new JButton("14");
      _dayButtons[14] = new JButton("15");
      _dayButtons[15] = new JButton("16");
      _dayButtons[16] = new JButton("17");
      _dayButtons[17] = new JButton("18");
      _dayButtons[18] = new JButton("19");
      _dayButtons[19] = new JButton("20");
      _dayButtons[20] = new JButton("21");
      _dayButtons[21] = new JButton("22");
      _dayButtons[22] = new JButton("23");
      _dayButtons[23] = new JButton("24");
      _dayButtons[24] = new JButton("25");
      _dayButtons[25] = new JButton("26");
      _dayButtons[26] = new JButton("27");
      _dayButtons[27] = new JButton("28");
      _dayButtons[28] = new JButton("29");
      _dayButtons[29] = new JButton("30");
      _dayButtons[30] = new JButton("31");
      _dayButtons[31] = new JButton("32");
      _dayButtons[32] = new JButton("33");
      _dayButtons[33] = new JButton("34");
      _dayButtons[34] = new JButton("35");
      _dayButtons[35] = new JButton("36");
      _dayButtons[36] = new JButton("37");
      _dayButtons[37] = new JButton("38");
      _dayButtons[38] = new JButton("39");
      _dayButtons[39] = new JButton("40");
      _dayButtons[40] = new JButton("41");
      _dayButtons[41] = new JButton("42");

      _monthLabel = new JLabel(MONTHS[_calendar.get(Calendar.MONTH)]);
      yearLabel = new JLabel(String.valueOf(calendar.get(Calendar.YEAR)));

      _dayLabels[0] = new JLabel("Sunday");
      _dayLabels[1] = new JLabel("Monday");
      _dayLabels[2] = new JLabel("Tuesday");
      _dayLabels[3] = new JLabel("Wednesday");
      _dayLabels[4] = new JLabel("Thursday");
      _dayLabels[5] = new JLabel("Friday");
      _dayLabels[6] = new JLabel("Saturday");

      myContentPane.add(_scrollButtons[0]);
      myContentPane.add(_monthLabel);
      myContentPane.add(_yearLabel);
      myContentPane.add(_scrollButtons[1]);

      //Labels that are related to Bottom Panel.
      myContentPane.add(_dayLabels[0]);
      myContentPane.add(_dayLabels[1]);
      myContentPane.add(_dayLabels[2]);
      myContentPane.add(_dayLabels[3]);
      myContentPane.add(_dayLabels[4]);
      myContentPane.add(_dayLabels[5]);
      myContentPane.add(_dayLabels[6]);

      for (int i = 0; i < _dayButtons.length; i++) {
      _dayButtons.addActionListener(new DayButtonListener());
      myContentPane.add(_dayButtons[i]);
      }

      gbc.gridy = 2; gbc.gridx = 2;
      gbl.setConstraints(_scrollButtons[0], gbc);
      gbc.gridy = 2; gbc.gridx = 4;
      gbl.setConstraints(_monthLabel, gbc);
      gbc.gridy = 2; gbc.gridx = 6;
      gbl.setConstraints(_yearLabel, gbc);
      gbc.gridy = 2; gbc.gridx = 8;
      gbl.setConstraints(_scrollButtons[1], gbc);


      gbc.gridy = 4; gbc.gridx = 2;
      gbl.setConstraints(_dayLabels[0], gbc);
      gbc.gridy = 4; gbc.gridx = 3;
      gbl.setConstraints(_dayLabels[1], gbc);
      gbc.gridy = 4; gbc.gridx = 4;
      gbl.setConstraints(_dayLabels[2], gbc);
      gbc.gridy = 4; gbc.gridx = 5;
      gbl.setConstraints(_dayLabels[3], gbc);
      gbc.gridy = 4; gbc.gridx = 6;
      gbl.setConstraints(_dayLabels[4], gbc);
      gbc.gridy = 4; gbc.gridx = 7;
      gbl.setConstraints(_dayLabels[5], gbc);
      gbc.gridy = 4; gbc.gridx = 8;
      gbl.setConstraints(_dayLabels[6], gbc);

      int rowCounter = 0;
      int columnCounter = 0;
      for (int i = 0; i < _dayButtons.length; i++) {

      gbc.gridy = rowCounter + 5; gbc.gridx = columnCounter + 2;
      gbl.setConstraints(_dayButtons[i], gbc);
      columnCounter++;
      if (columnCounter >= 7) {
      rowCounter++;
      columnCounter = 0;
      }
      }

      _scrollButtons[0].setSize(300,300);
      _monthLabel.setSize(300,300);
      _yearLabel.setSize(300,300);
      _scrollButtons[1].setSize(300,300);

      _dayLabels[0].setSize(300,300);
      _dayLabels[1].setSize(300,300);
      _dayLabels[2].setSize(300,300);
      _dayLabels[3].setSize(300,300);
      _dayLabels[4].setSize(300,300);
      _dayLabels[5].setSize(300,300);
      _dayLabels[6].setSize(300,300);

      aFrame.show(); //show the frame

      }

      protected void setupCalendarDayButtons() {

      populateCalendarTableList();

      Calendar cal = Calendar.getInstance();
      cal.setTime(_calendar.getTime());
      cal.set(Calendar.DATE, 1);

      int emptyLabelCount = cal.get(Calendar.DAY_OF_WEEK) - 1;
      int lastDayOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
      cal.set(Calendar.DATE, lastDayOfMonth);


      int dayCount = 0;
      for (int count = 0; count < emptyLabelCount; count++) {
      _dayButtons[count].setText("");
      }

      for (int i = 0; i < lastDayOfMonth; i++) {
      dayCount++;
      String key = "" + calendar.get(Calendar.MONTH) + dayCount + calendar.get(Calendar.YEAR);
      Object obj = _calendarTableList.get(key);
      int eventCount = 0;
      if (obj != null) {
      ArrayList dayList = (ArrayList)obj;
      eventCount = dayList.size();
      }
      _dayButtons[i + emptyLabelCount].setText("" + dayCount);
      if (eventCount > 1) {
      _dayButtons[i + emptyLabelCount].setFont(new Font("Serif", Font.BOLD, 14));
      } else if (eventCount == 1){
      _dayButtons[i + emptyLabelCount].setFont(new Font("Serif", Font.ITALIC, 14));
      } else {
      _dayButtons[i + emptyLabelCount].setFont(new Font("Serif", Font.PLAIN, 12));
      }

      }

      for (int i = (dayCount + emptyLabelCount); i < _dayButtons.length; i++) {
      _dayButtons[i].setText("");
      }

      _monthLabel.setText(MONTHS[_calendar.get(Calendar.MONTH)]);
      yearLabel.setText(String.valueOf(calendar.get(Calendar.YEAR)));

      }

      private void populateCalendarTableList() {
      CalendarDB caldb = new CalendarDB();
      _calendarTableList = caldb.findAll();
      }

      private void initializeScrollButtonListeners() {
      _scrollButtons[0].addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      if (e.getSource() == _scrollButtons[0])
      currentMonth = currentMonth - 1;
      if (_currentMonth == -1) {
      _currentMonth = 11;
      currentYear = currentYear - 1;
      }
      calendar.set(Calendar.MONTH, currentMonth);
      calendar.set(Calendar.YEAR, currentYear);
      setupCalendarDayButtons();
      }
      });
      _scrollButtons[1].addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      if (e.getSource() == _scrollButtons[1])
      currentMonth = currentMonth + 1;
      if (_currentMonth == 12) {
      _currentMonth = 0;
      currentYear = currentYear + 1;
      }
      calendar.set(Calendar.MONTH, currentMonth);
      calendar.set(Calendar.YEAR, currentYear);
      setupCalendarDayButtons();
      }
      });
      }

      //begin constructor
      public CalLatest() {
      try {
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
      } catch (Exception e) {
      e.printStackTrace();
      }

      initializeFrame();
      initializePanels();
      initializeScrollButtonListeners();
      setupCalendarDayButtons();
      } //end constructor

      //begin Action Listener class
      public class DayButtonListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
      JButton clickedButton = (JButton)(e.getSource());
      if (clickedButton.getText() != null && clickedButton.getText() != "")
                     {
                          String key = "" + calendar.get(Calendar.MONTH) + clickedButton.getText() + calendar.get(Calendar.YEAR);
                          CalendarEvent calEvent = new CalendarEvent(key,(ArrayList)_calendarTableList.get(key));
      }
      }
      }

      //begin main
      public static void main(String[] args) {
      CalLatest myCal = new CalLatest();
      } //end main

      } //end of Cal class






      /*CalendarEvent.java*/



      import java.awt.*;//GridLayout;
      import java.awt.event.*;//WindowAdapter,WindowEvent
      import java.sql.Timestamp;
      import java.text.SimpleDateFormat;
      import java.util.List;
      import java.util.*;//ArrayList,HashMap,List
      import javax.swing.*;//JButton,JFrame,JPanel,JScrollPane,JTable,ListSelectionModel
      import javax.swing.event.*;//ListSelectionEvent,ListSelectionListener
      import javax.swing.table.AbstractTableModel;
      import javax.swing.table.TableColumn;




      //begin the CalendarEvent class
      public class CalendarEvent extends JFrame implements ActionListener
      {
           //begin private data members
           private JButton myAddButton,mySaveButton,myCancelButton;
           private CalDataModel myCalDataModel;
           private JFrame myJFrame;
      private JTable table;
           private int i;

      //begin the CalendarEvent constructor
      public CalendarEvent (String date, List dayEvents)
           {
                JFrame frame = new JFrame("Calendar Events");
      frame.setTitle("Calendar Events for '" + date + "'");
      frame.setSize(900, 600);
      frame.setVisible(true);
      frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { } } );

      JTable table = new JTable(new CalDataModel(dayEvents));
      table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

      TableColumn column = null;
      for (int i = 0; i < 11; i++)
                {
      column = table.getColumnModel().getColumn(i);
      switch (i)
                     {
      case 0: column.setPreferredWidth(200); break;
      case 1: column.setPreferredWidth(200); break;
      case 2: column.setPreferredWidth(200); break;
      case 3: column.setPreferredWidth(200); break;
      case 4: column.setPreferredWidth(200); break;
      case 5: column.setPreferredWidth(200); break;
      case 6: column.setPreferredWidth(200); break;
      case 7: column.setPreferredWidth(400); break;
      case 8: column.setPreferredWidth(400); break;
      case 9: column.setPreferredWidth(300); break;
      case 10: column.setPreferredWidth(30); break;
      default: break;

      } //end switch

                } //end for

      table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      ListSelectionModel rowSM = table.getSelectionModel();
                //Row Selection
                rowSM.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
      if (e.getValueIsAdjusting()) return;

      ListSelectionModel rlsm =
      (ListSelectionModel)e.getSource();
      if (rlsm.isSelectionEmpty()) {
      } else {
      int selectedRow = rlsm.getMinSelectionIndex();
      }
      }
      });


                
                //Enable the ability to select single cell
                table.setColumnSelectionAllowed(true);
                table.setRowSelectionAllowed(true);


           
      JScrollPane scrollPane = new JScrollPane(table);
      scrollPane.setAutoscrolls(true);
      JPanel buttonPanel = new JPanel();
                

                //Adding ActionListener to Add button
                this.myAddButton = new JButton("Add");
                this.myAddButton.addActionListener(this);

                //Adding ActionListener to Save button
                this.mySaveButton = new JButton("Save");
                this.mySaveButton.addActionListener(this);

                //Adding ActionListener to Cancel button
                this.myCancelButton = new JButton("Cancel");
      this.myCancelButton.addActionListener(this);

                
      //Adding 'Add Button','Save Button' & 'Cancel Button to panel
      buttonPanel.add(this.myAddButton);
      buttonPanel.add(this.mySaveButton);
                buttonPanel.add(this.myCancelButton);

                

      frame.getContentPane().setLayout(new GridLayout(2, 1));
      frame.getContentPane().add(scrollPane);
      frame.getContentPane().add(buttonPanel);

      frame.setVisible(true);

      } //end the CalendarEvent constructor

           //Calling the method from CalDataModel to add
           //new row to JTable
      public void actionPerformed(ActionEvent e)
           {
                if (e.getSource()== CalendarEvent.this.myAddButton)
                {JOptionPane.showMessageDialog(null,"Hello");
                int row = this.table.getRowCount();
                int col = this.table.getColumnCount();
                for (i = 1; i<col; i++ )
                {
                     row = row + 1;
                }
                }

                else if (e.getSource() == CalendarEvent.this.mySaveButton)
                {JOptionPane.showMessageDialog(null,"This is save");}

                else if(e.getSource() == CalendarEvent.this.myCancelButton)
                {int choice = JOptionPane.showConfirmDialog(this,"Do you really want to quit",
                "Confirm quit",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);

                     if (choice == JOptionPane.YES_OPTION)
                  System.exit(0);
                }
      }


      } //end the CalendarEvent class



      /*CalDataModel.java*/


      import java.awt.*;//GridLayout;
      import java.awt.event.*;//WindowAdapter,WindowEvent
      import java.sql.Timestamp;
      import java.text.SimpleDateFormat;
      import java.util.List;
      import java.util.*;//ArrayList,HashMap,List
      import javax.swing.*;//JButton,JFrame,JPanel,JScrollPane,JTable,ListSelectionModel
      import javax.swing.event.*;//ListSelectionEvent,ListSelectionListener
      import javax.swing.table.AbstractTableModel;
      import javax.swing.table.TableColumn;




      //begin the CalendarEvent class
      public class CalendarEvent extends JFrame implements ActionListener
      {
           //begin private data members
           private JButton myAddButton,mySaveButton,myCancelButton;
           private CalDataModel myCalDataModel;
           private JFrame myJFrame;
      private JTable table;
           private int i;

      //begin the CalendarEvent constructor
      public CalendarEvent (String date, List dayEvents)
           {
                JFrame frame = new JFrame("Calendar Events");
      frame.setTitle("Calendar Events for '" + date + "'");
      frame.setSize(900, 600);
      frame.setVisible(true);
      frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { } } );

      JTable table = new JTable(new CalDataModel(dayEvents));
      table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

      TableColumn column = null;
      for (int i = 0; i < 11; i++)
                {
      column = table.getColumnModel().getColumn(i);
      switch (i)
                     {
      case 0: column.setPreferredWidth(200); break;
      case 1: column.setPreferredWidth(200); break;
      case 2: column.setPreferredWidth(200); break;
      case 3: column.setPreferredWidth(200); break;
      case 4: column.setPreferredWidth(200); break;
      case 5: column.setPreferredWidth(200); break;
      case 6: column.setPreferredWidth(200); break;
      case 7: column.setPreferredWidth(400); break;
      case 8: column.setPreferredWidth(400); break;
      case 9: column.setPreferredWidth(300); break;
      case 10: column.setPreferredWidth(30); break;
      default: break;

      } //end switch

                } //end for

      table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      ListSelectionModel rowSM = table.getSelectionModel();
                //Row Selection
                rowSM.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
      if (e.getValueIsAdjusting()) return;

      ListSelectionModel rlsm =
      (ListSelectionModel)e.getSource();
      if (rlsm.isSelectionEmpty()) {
      } else {
      int selectedRow = rlsm.getMinSelectionIndex();
      }
      }
      });


                
                //Enable the ability to select single cell
                table.setColumnSelectionAllowed(true);
                table.setRowSelectionAllowed(true);


           
      JScrollPane scrollPane = new JScrollPane(table);
      scrollPane.setAutoscrolls(true);
      JPanel buttonPanel = new JPanel();
                

                //Adding ActionListener to Add button
                this.myAddButton = new JButton("Add");
                this.myAddButton.addActionListener(this);

                //Adding ActionListener to Save button
                this.mySaveButton = new JButton("Save");
                this.mySaveButton.addActionListener(this);

                //Adding ActionListener to Cancel button
                this.myCancelButton = new JButton("Cancel");
      this.myCancelButton.addActionListener(this);

                
      //Adding 'Add Button','Save Button' & 'Cancel Button to panel
      buttonPanel.add(this.myAddButton);
      buttonPanel.add(this.mySaveButton);
                buttonPanel.add(this.myCancelButton);

                

      frame.getContentPane().setLayout(new GridLayout(2, 1));
      frame.getContentPane().add(scrollPane);
      frame.getContentPane().add(buttonPanel);

      frame.setVisible(true);

      } //end the CalendarEvent constructor

           //Calling the method from CalDataModel to add
           //new row to JTable
      public void actionPerformed(ActionEvent e)
           {
                if (e.getSource()== CalendarEvent.this.myAddButton)
                {JOptionPane.showMessageDialog(null,"Hello");
                int row = this.table.getRowCount();
                int col = this.table.getColumnCount();
                for (i = 1; i<col; i++ )
                {
                     row = row + 1;
                }
                }

                else if (e.getSource() == CalendarEvent.this.mySaveButton)
                {JOptionPane.showMessageDialog(null,"This is save");}

                else if(e.getSource() == CalendarEvent.this.myCancelButton)
                {int choice = JOptionPane.showConfirmDialog(this,"Do you really want to quit",
                "Confirm quit",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);

                     if (choice == JOptionPane.YES_OPTION)
                  System.exit(0);
                }
      }


      } //end the CalendarEvent class



      /*CalendarDB.java*/



      import java.sql.Connection;
      import java.sql.Date;
      import java.sql.DriverManager;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.util.ArrayList;
      import java.util.Calendar;
      import java.util.HashMap;

      public class CalendarDB {

      private static final String INSERT =
           "insert into calendar (event_type, startdate, starttime,enddate, endtime, summary, description, " +
      "link, location, originator) values ('E', ?, ?, ?, ?, ?, ?, ?, ?, ?)";

      private static final String SELECT_ALL =
      "select eventid, event_type, startdate, starttime, enddate, endtime, summary, description, " +
      "link, location, originator from calendar";


      private Connection getConnection() {
      Connection conn = null;
      try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      String filename = "D:/Calendar.mdb";
      String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
      database += filename.trim() + ";DriverID=22;READONLY=true}";
      conn = DriverManager.getConnection(database, "", "");
      } catch (Exception e) {
      throw new RuntimeException("Exception while getting a connection : " + e + " : " + e.getMessage());
      }
      return conn;
      }

      public HashMap findAll() {
      Connection conn = null;
      PreparedStatement stmt = null;
      ResultSet rs = null;
      HashMap calendarMap = new HashMap();

      try {
      conn = getConnection();
      stmt = conn.prepareStatement(SELECT_ALL);
      rs = stmt.executeQuery();
      while(rs.next()) {
      HashMap map = new HashMap();
      map.put("eventid", new Long(rs.getLong("eventid")));
      map.put("event_type", rs.getString("event_type"));
      Date startDate = rs.getDate("startdate");
      map.put("startdate", startDate);
      map.put("enddate", rs.getDate("enddate"));
      map.put("starttime", rs.getTimestamp("starttime"));
      map.put("endtime", rs.getTimestamp("endtime"));
      map.put("summary", rs.getString("summary"));
      map.put("description", rs.getString("description"));
      map.put("link", rs.getString("link"));
      map.put("location", rs.getString("location"));
      map.put("originator", rs.getString("originator"));

      Calendar cal = Calendar.getInstance();
      cal.setTime(startDate);
      String key = "" + cal.get(Calendar.MONTH) +
      cal.get(Calendar.DATE) + cal.get(Calendar.YEAR);
      if (calendarMap.containsKey(key)) {
      ArrayList list = (ArrayList)calendarMap.get(key);
      list.add(map);
      } else {
      ArrayList list = new ArrayList();
      list.add(map);
      calendarMap.put(key, list);
      }
      }
      } catch (SQLException e) {
      throw new RuntimeException("Exception while inserting record : " + e + " : " + e.getMessage());
      } finally {
      try {
      if (rs != null) {
      rs.close();
      }
      } catch (SQLException e1) {
      }
      try {
      if (stmt != null) {
      stmt.close();
      }
      } catch (SQLException e1) {
      }
      try {
      if (conn != null) {
      conn.close();
      }
      } catch (SQLException e2) {
      }
      stmt = null;
      conn = null;
      rs = null;
      }
      return calendarMap;
      }

      public void create(Date startdate, Date starttime, Date enddate, Date endtime,String summary, String description, String link,
                String location, String originator)
      {
      Connection conn = null;
      PreparedStatement stmt = null;
      try {
      conn = getConnection();
      stmt = conn.prepareStatement(INSERT);
      int ix = 1;
      stmt.setDate(ix++, startdate);
      stmt.setDate(ix++, starttime);
      stmt.setDate(ix++, enddate);
      stmt.setDate(ix++, endtime);
      stmt.setString(ix++, summary);
      stmt.setString(ix++, description);
      stmt.setString(ix++, link);
      stmt.setString(ix++, location);
      stmt.setString(ix++, originator);
      int rowcount = stmt.executeUpdate();
      if (rowcount != 1) {
      throw new RuntimeException("Expected to inserted 1 row but inserted " + rowcount + " row(s)");
      }
      } catch (SQLException e) {
      throw new RuntimeException("Exception while inserting record : " + e + " : " + e.getMessage());
      } finally {
      try {
      stmt.close();
      } catch (SQLException e1) {
      }
      try {
      conn.close();
      } catch (SQLException e2) {
      }
      stmt = null;
      conn = null;
      }
      }
      }

        • 1. Re: Abstract Table Model
          807587
          Thanks for sharing you code listing, however, it would be nice to know what your "concerns" are with the add and save buttons. May you could share what is happening now or what errors you see.

          Being specific is a key to getting useful help in this forum.

          DB
          • 2. Re: Abstract Table Model
            807587
            Let me explain. I need to add a new row to JTable at the click of 'Add' button. To achieve this, I need deal with two classes.
            * CalDataModel, which extends AbstractTableModel
            * CalendarEvent, which extends JFrames and have buttons 'Add', 'Save' and 'Cancel'

            What is happening is, Monthly GUI interface is getting displayed with dates, month and year. User can navigate through "<<" and ">>" buttons, and interface displays just like calendar. For example, you can navigate and see January 2005 or 2006 etc..

            But when the user clicks on one of the days, an other interface is displayed, which is a JTable interface with rows and columns( actually you can browse backwards September 2004, and click on date 26, you will see that there are some appointments) with buttons 'ADD', 'SAVE' and 'CANCEL'.

            When the user clicks on add it should add a new row to JTable interface and when the user should be able to add new information to the added row and use 'SAVE' button to save it in the database. There is also other feature which indicates that this particular date has some appointments saved in it. (for example, if you look at September 26th, 2004 the date is made bold and the rest of the dates are made normal, which will act as a reminder to the user that there are some appointments saved on that date).

            My question is, my buttons on the JTable interface are in class CalendarEvent.java and to access rows and columns and also eventlist array is in class CalDataModel.java. I tried different ways of accessing outer class methods and functions. For some reason, I cannot figure out how to add a new row to JTable and save it to the database. This is part of school work. A quick response would be greatly appreciated. I hope I made sense in my explanation.
            Thanks