1 Reply Latest reply: May 2, 2013 2:47 PM by TPD-Opitz RSS

    JComponents in JTable not check boxes or strings

    993724
      in the following code the jtable I have done displaying everything as string except Boolean values which displays check boxes
      I wanna to display the components as I want like comboBoxes and textfields not strings in the cells of the table
      I have override getclass method but in vain
      so, pls, help me
      /*
       * To change this template, choose Tools | Templates
       * and open the template in the editor.
       */
      package JobsFollowUp;
      
      import java.awt.Dimension;
      import javax.swing.*;
      import javax.swing.table.*;
      
      /**
       *
       * @author COMPUTER SHOP
       */
      public class JobsFollowUp extends JFrame {
              private boolean DEBUG = false;
      
          //declare the buttons for adding the job and removing for the last row
          JButton addJobButton = new JButton("Add Job");
          JButton removeJobButton = new JButton("Remove Job");
          
      
          
          //declare the engineer names, attach to combo box
          private  final static String[] engineerNames = {"Mohamad Samy", "Ahmad El_Namoury", "Ahmad Ismail"
                                          ,"Mohamad El_Gazar", "Mohamad Tolba", "Mohamad El_Askary"};
          JComboBox engineerNamesComboBox = new JComboBox(engineerNames);
          
          //declare the plant names, attach to combo box
          private  final static String[] plantNames = {"Qar'a", "A.M LPG", "Shipping"};
          JComboBox plantNamesComboBox = new JComboBox(plantNames);
          
          //declare the progress, attach to combo box
          private  final static String[] progressCases = {"Finished", "In Progress", "Waiting SD"};
          JComboBox progressCasesComboBox = new JComboBox(progressCases);
          
          //declare the job description textField
          JTextField jobDescriptionTextField = new JTextField();
          
          //declare the job description textField
          JTextField jobNoTextField = new JTextField();    
          
      
          //declare the grouplayout as the layout manager for the panel
          GroupLayout layout;
          //declare the scroll pane for the table
          JScrollPane tableScrollPane;
      
          
      public JobsFollowUp(){
              
                //declare the jobs table
              JTable jobsTable = new JTable(new MyModel());
              jobsTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
              jobsTable.setFillsViewportHeight(true);
              layout = new GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              tableScrollPane = new JScrollPane(jobsTable);
              //arrange the icons in the program as needed
              layout.setHorizontalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(layout.createSequentialGroup()
                      .addContainerGap()
                      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                          .addGroup(layout.createSequentialGroup()
                              .addComponent(addJobButton)
                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                              .addComponent(removeJobButton))
                          .addComponent(tableScrollPane, 0, 375, Short.MAX_VALUE
                      
                      
                      ))
                      .addContainerGap(15, Short.MAX_VALUE))
              );
              
              //arrange the icons in the vertical grid
              layout.setVerticalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(layout.createSequentialGroup()
                      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                          .addComponent(addJobButton)
                          .addComponent(removeJobButton))
                      .addGap(5)
                      .addComponent(tableScrollPane, 0, 170, Short.MAX_VALUE)
                      .addGap(5, 5, 5))
              );        
                     
      }
      
      public class MyModel extends AbstractTableModel
      {
             //declare the table row that contains the editable cells
          private  Object [][] rowData ={{engineerNames,jobDescriptionTextField,
                                                      plantNamesComboBox, progressCases,
                                                      jobNoTextField}
                                              };
              //declare the data for the column header 
          private String[] columnNames = {"Engineer Name", "Job Description",
                                                       "Plant Name", "Progress", " Job No."};
          
              public int getColumnCount() {
                  return columnNames.length;
              }
      
              public int getRowCount() {
                  return rowData.length;
              }
      
              public String getColumnName(int col) {
                  return columnNames[col];
              }
      
              public Object getValueAt(int row, int col) {
                  return rowData[row][col];
              }
      
              /*
               * JTable uses this method to determine the default renderer/
               * editor for each cell.  If we didn't implement this method,
               * then the last column would contain text ("true"/"false"),
               * rather than a check box.
               */
              public Class getColumnClass(int c) {
                  return getValueAt(0, c).getClass();
              }
      
              /*
               * Don't need to implement this method unless your table's
               * editable.
               */
              public boolean isCellEditable(int row, int col) {
                  //Note that the data/cell address is constant,
                  //no matter where the cell appears onscreen.
                  if (col < 2) {
                      return false;
                  } else {
                      return true;
                  }
              }
      
              /*
               * Don't need to implement this method unless your table's
               * data can change.
               */
              public void setValueAt(Object value, int row, int col) {
                  if (DEBUG) {
                      System.out.println("Setting value at " + row + "," + col
                                         + " to " + value
                                         + " (an instance of "
                                         + value.getClass() + ")");
                  }
      
                  rowData[row][col] = value;
                  fireTableCellUpdated(row, col);
      
                  if (DEBUG) {
                      System.out.println("New value of data:");
                      printDebugData();
                  }
              }
      
              private void printDebugData() {
                  int numRows = getRowCount();
                  int numCols = getColumnCount();
      
                  for (int i=0; i < numRows; i++) {
                      System.out.print("    row " + i + ":");
                      for (int j=0; j < numCols; j++) {
                          System.out.print("  " + rowData[i][j]);
                      }
                      System.out.println();
                  }
                  System.out.println("--------------------------");
              }
      }
      
                                                    {       
          }
          
          public static void main(String[] args)
          {
              JobsFollowUp application = new JobsFollowUp();
              application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              application.setVisible(true);
              application.setSize(700, 500);
          }    
          
          
          
      }