Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTable AddRow Not Working

843806Jul 16 2009 — edited Jul 23 2009
I've searched through the archives and did actually find a couple of things I was doing wrong, but still no-go :( :(

I am using the default table model to create the table:
  private static JPanel pnl_CourseList;
  public static JTable tbl_CourseList;
  private static JScrollPane sp_CourseList;

  public Course_ListPanel(String[][] CourseData)
  {
    DefaultTableCellRenderer dtcr;

    pnl_CourseList=new JPanel();
    tbl_CourseList=new JTable();
    sp_CourseList=new JScrollPane();

    tbl_CourseList_Heading=get_tbl_CourseList_Heading();
    DefaultTableModel dtm=new DefaultTableModel(CourseData, tbl_CourseList_Heading);
    tbl_CourseList.setModel(dtm);

    DefaultTableHeaderCellRenderer dthcr=new DefaultTableHeaderCellRenderer();
    dthcr.setVerticalAlignment(SwingConstants.BOTTOM);
    dthcr.setVerticalTextPosition(SwingConstants.BOTTOM);
    tbl_CourseList.getTableHeader().setDefaultRenderer(dthcr);

    tbl_CourseList.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    tbl_CourseList.setMaximumSize(THIS_SIZE);
    tbl_CourseList.setMinimumSize(THIS_SIZE);
    tbl_CourseList.setPreferredSize(THIS_SIZE);
    tbl_CourseList.setRowHeight(20);
    tbl_CourseList.setRowMargin(2);
    tbl_CourseList.getTableHeader().setResizingAllowed(false);
    tbl_CourseList.getTableHeader().setReorderingAllowed(false);

    for(CourseList ClmnInfo : CourseList.values()) //EnumSet.complementOf(EnumSet.of(CourseList.ZIP, CourseList.HOLEPAR)))
    {
      tbl_CourseList.getColumnModel().getColumn(ClmnInfo.ordinal()).setMaxWidth(ClmnInfo.get_ColumnWidth());
      tbl_CourseList.getColumnModel().getColumn(ClmnInfo.ordinal()).setMinWidth(ClmnInfo.get_ColumnWidth());
      tbl_CourseList.getColumnModel().getColumn(ClmnInfo.ordinal()).setPreferredWidth(ClmnInfo.get_ColumnWidth());

      if(tbl_CourseList.getColumn(ClmnInfo.get_HeadingText()).getCellRenderer()==null)
        dtcr=new DefaultTableCellRenderer();
      else
        dtcr=(DefaultTableCellRenderer)tbl_CourseList.getColumn(ClmnInfo.get_HeadingText()).getCellRenderer();

      dtcr.setHorizontalAlignment(ClmnInfo.get_ColumnAlignment());
      dtcr.setVerticalAlignment(SwingConstants.BOTTOM);
      dtcr.setVerticalTextPosition(SwingConstants.BOTTOM);
      tbl_CourseList.getColumn(ClmnInfo.get_HeadingText()).setCellRenderer(dtcr);
    }

    sp_CourseList.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    sp_CourseList.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); //VERTICAL_SCROLLBAR_AS_NEEDED);
    sp_CourseList.setViewportView(tbl_CourseList);

    pnl_CourseList.setMaximumSize(THIS_SIZE);
    pnl_CourseList.setMinimumSize(THIS_SIZE);
    pnl_CourseList.setPreferredSize(THIS_SIZE);
    pnl_CourseList.setLayout(new BoxLayout(pnl_CourseList, BoxLayout.PAGE_AXIS));
    pnl_CourseList.add(sp_CourseList);
  }
Once the user imputs all of the data on the "edit" screen, I capture that data and try to add it to the end of the table:
        if(usrOption==JOptionPane.YES_OPTION)
        {
          Course_EditPanel.Get_EditPanelData(updateCourse);
          int tmpID=Integer.valueOf(Collections.max(GUI_MainPanel.CSX).get_CourseID())+1;
          NumberFormat nf = new DecimalFormat("00000");
          Enable_CourseList();

          for(int lp=0; lp<4; lp=lp+1)
          {
            updateCourse.get(lp).set_CourseID(nf.format(tmpID+lp));
            GUI_MainPanel.CSX.add(updateCourse.get(lp));
            Object[] tmpCrse=updateCourse.get(lp).Get_Course();
            DefaultTableModel dtm=(DefaultTableModel) Course_ListPanel.tbl_CourseList.getModel();
            dtm.addRow(tmpCrse);
          }
        }
Debugging this, I have verified that the data is in fact being retrieved into updateCourse and that data is properly being added to the main array list (CSX). I have also verified that the dtm that I create within the loop is the same ID as the one that is created at initialization. I have also checked that prior to the loop, there are 16 rows (which it should be) in the dataVector property of the model. After the loop, there are now 20 rows (again, which there should be) in the dataVector.

I'm hoping it's something simple that I'm missing here. Thanks for looking!!!!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 20 2009
Added on Jul 16 2009
15 comments
201 views