This discussion is archived
3 Replies Latest reply: May 14, 2013 8:06 AM by 993724 RSS

DefaultModel addRow method

993724 Newbie
Currently Being Moderated
I made the following JTable with default model and used the default editors to show my comboBoxes as I want. the program is ok but when I clicked the addRow button, the new row is added but rendererd as object so, it
give me the String abbreviation of this object. those objects in the new row are edited as I want but they show me the string on the textfields and the combo boxes, I don't want it.
Please compile this program and give me the right solution
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellEditor;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ep08363
 */
public class JobsFollowUp extends JPanel{

    TableColumn tCol;
    JButton addButton;
    JButton removeButton;
    JComboBox engineerNames;
    JComboBox plantNames;
    JComboBox progressValue;
                             String[] columnNames = {"Engineer Name", "Job Description", "Plant", "Progress",
                                "Job No."};
        
         final Object[][] rowData = {{"Mohamad Samy", "", "Qar'a", "Finished", ""},
                              {"Ahmad El_Namoury", "", "A.M LPG", "In Progress", ""},
                              {"Ahmad Ismail", "", "Shipping", "Wait SD", ""},
                              {"Mohamad El_Gazar", "", "", "", ""},
                              {"Mohamad El_Askary", "", "", "", ""},};

    public JobsFollowUp(){
        
        //declare the buttons for adding and removing jobs
        addButton = new JButton("Add Job");
        removeButton = new JButton("Remove Job");
        

     //declare the table model 
    final DefaultTableModel model = new DefaultTableModel(rowData, columnNames ){
        @Override
        public Class getColumnClass(int col){
            return (getValueAt(0,col).getClass());
        }
    };
    final JTable table = new JTable(model);
    table.setFillsViewportHeight(false);
    
    JScrollPane scrollPane = new JScrollPane(table);
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(removeButton))
                    .addComponent(scrollPane, 0, 375, Short.MAX_VALUE))
                )
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(removeButton)
                    .addComponent(addButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
                

    
    TableColumn column = null;
    for(int i=0; i<5; i++){
      column = table.getColumnModel().getColumn(i);
        if(i==0)
            column.setPreferredWidth(80);
        else if(i==1)          
            column.setPreferredWidth(500);
        else if(i==2)          
            column.setPreferredWidth(30);        
        else if(i==3)          
            column.setPreferredWidth(30);
        else if(i==4)          
            column.setPreferredWidth(30);
    }
    
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    table.setCellSelectionEnabled(true);
    //set the color for the table header
    JTableHeader header = table.getTableHeader();
    //paint the rows as per the renderers class
    header.setBackground(Color.CYAN);
    header.setToolTipText("brain out, figure out");
    

    
    
    for(int i=0;i<5;i++){
    tCol = table.getColumnModel().getColumn(i);
    tCol.setCellRenderer(new CustomTableCellRenderer() );
    }//end of for statment
        
    CustomTableCellRenderer renderer = new CustomTableCellRenderer();
    renderer.setToolTipText("my first tool tip ever in java");
    table.getColumnModel().getColumn(1).setCellRenderer(renderer);
    
    //sorter test
    table.setAutoCreateRowSorter(true);
    
    //make the combo for the first column for engineer names
     engineerNames = new JComboBox();
    engineerNames.addItem("Mohamad Samy");
    engineerNames.addItem("Ahmad El_Namoury");
    engineerNames.addItem("Ahmad Ismail");
    engineerNames.addItem("Mohamad El_Gazar");
    engineerNames.addItem("Mohamad Tolba");
    engineerNames.addItem("Mohamad El_Askary");    
    final TableColumn engineerNamesColumn = table.getColumnModel().getColumn(0);
    engineerNamesColumn.setCellEditor(new DefaultCellEditor(engineerNames));
    
    //make the combo for the first column for plant Names
    plantNames = new JComboBox();
    plantNames.addItem("Qar'a");
    plantNames.addItem("A.M LPG");
    plantNames.addItem("Shipping");    
    TableColumn plantNamesColumn = table.getColumnModel().getColumn(2);
    plantNamesColumn.setCellEditor(new DefaultCellEditor(plantNames));
    
    //make the combo for the first column for job situation
    progressValue = new JComboBox();
    progressValue.addItem("Finished");
    progressValue.addItem("In Progress");
    progressValue.addItem("Wait for SD");    
    TableColumn progressColumn = table.getColumnModel().getColumn(3);
    progressColumn.setCellEditor(new DefaultCellEditor(progressValue));
    
        addButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource()==addButton){
                        model.addRow(rowData);
                }
                
            }
        
    });
        
    }//end of constructor

   class CustomTableCellRenderer extends DefaultTableCellRenderer{
        @Override
        public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int col){
            Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, col);
                if(isSelected){
                    cell.setBackground(Color.WHITE);

             }
                else{
                    if(row %2 ==0){
                        cell.setBackground(Color.white);
                    }
                    else{
                        cell.setBackground(Color.LIGHT_GRAY);
                    }
                }
                    return cell;
                }
        }//End of the Custom table cell renderer
    

 


    private static void createAndShowGUI(){
    JFrame frame = new JFrame("1st Table in Oracle Documents");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JobsFollowUp tablePanel = new JobsFollowUp();
    tablePanel.setOpaque(true);
    
    frame.setContentPane(tablePanel);
    frame.setSize(1000, 340);
    frame.setVisible(true);
}// end of create GUI
    
    
    
    public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            createAndShowGUI();
        }
    });//end of main method
    
    
}
}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points