package jtable01;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;



public class code {

	private static JComboBox cmb1;
	
   public static void main(String[] args){
       
       // create JFrame and JTable
       JFrame frame = new JFrame();
       JTable table = new JTable(); 
       
       // create a table model and set a Column Identifiers to this model 
       Object[] columns = {"Id","First Name","Last Name","Age"};
       DefaultTableModel model = new DefaultTableModel();
       model.setColumnIdentifiers(columns);
       
       // set the model to the table
       table.setModel(model);
       
       // Change A JTable Background Color, Font Size, Font Color, Row Height
       table.setBackground(Color.LIGHT_GRAY);
       table.setForeground(Color.black);
       Font font = new Font("",1,22);
       table.setFont(font);
       table.setRowHeight(30);
       
       // create JTextFields
       JTextField textId = new JTextField();
       JTextField textFname = new JTextField();
       JTextField textLname = new JTextField();
       JTextField textAge = new JTextField();
       
       // create JButtons
       JButton btnAdd = new JButton("Add");
       JButton btnDelete = new JButton("Delete");
       JButton btnUpdate = new JButton("Update");     
       
       textId.setBounds(20, 220, 100, 25);
       textFname.setBounds(20, 250, 100, 25);
       textLname.setBounds(20, 280, 100, 25);
       textAge.setBounds(20, 310, 100, 25);
       
       btnAdd.setBounds(150, 220, 100, 25);
       btnUpdate.setBounds(150, 265, 100, 25);
       btnDelete.setBounds(150, 310, 100, 25);
       
       // create JScrollPane
       JScrollPane pane = new JScrollPane(table);
       pane.setBounds(0, 0, 880, 200);
       
       frame.setLayout(null);
       
       frame.add(pane);
       
       // add JTextFields to the jframe
       frame.add(textId);
       frame.add(textFname);
       frame.add(textLname);
       frame.add(textAge);
   
       // add JButtons to the jframe
       frame.add(btnAdd);
       frame.add(btnDelete);
       frame.add(btnUpdate);
       
       // create an array of objects to set the row data
       Object[] row = new Object[4];
       
       
       JComboBox comboBox = new JComboBox();
       comboBox.addItem("Snowboarding");
       comboBox.addItem("Rowing");
       comboBox.addItem("Knitting");
       comboBox.addItem("Speed reading");
       comboBox.addItem("Pool");
       comboBox.addItem("None of the above");
       
       cmb1 = new JComboBox(new Object[]{"1.1", "1.0"});
       
       //use dropdown boxes
       String[] options = new String[]{"No", "Yes", "Only non-default"};
      // TableColumn column = TPTable.getColumnModel().getColumn(2);
      // column.setCellEditor(new MyJComboBoxEditor(options));

       //BorderPanel borderPanel3 =   new  BorderPanel("Panel 3");
       
//       JComboBox<String> ppComboBox;
//       DefaultComboBoxModel<String> cbm = new DefaultComboBoxModel<String>( new String[] 
//              { "person", "student", "employee" });
//       ppComboBox = new JComboBox<String>(cbm);

       
     //  table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(comboBox));
       
       JComboBox combo = new JComboBox();
       combo.addItem("Names");
       combo.addItem("Antony");
       combo.addItem("Victor");
       combo.addItem("Ramkumar");
       TableColumn gradeColumn = table.getColumnModel().getColumn(2);
       
       gradeColumn.setCellEditor(new DefaultCellEditor(combo));
       // button add row
       btnAdd.addActionListener(new ActionListener(){

           @Override
           public void actionPerformed(ActionEvent e) {
            
               row[0] = textId.getText();
               row[1] = textFname.getText();
               //row[2] = textLname.getText();
               row[3] = textAge.getText();
               
               // add row to the model
               model.addRow(row);
           }
       });
       
       // button remove row
       btnDelete.addActionListener(new ActionListener(){

           @Override
           public void actionPerformed(ActionEvent e) {
           
               // i = the index of the selected row
               int i = table.getSelectedRow();
               if(i >= 0){
                   // remove a row from jtable
                   model.removeRow(i);
               }
               else{
                   System.out.println("Delete Error");
               }
           }
       });
       
       // get selected row data From table to textfields 
       table.addMouseListener(new MouseAdapter(){
       
       @Override
       public void mouseClicked(MouseEvent e){
           
           // i = the index of the selected row
           int i = table.getSelectedRow();
           
           textId.setText(model.getValueAt(i, 0).toString());
           textFname.setText(model.getValueAt(i, 1).toString());
           textLname.setText(model.getValueAt(i, 2).toString());
           textAge.setText(model.getValueAt(i, 3).toString());
       }
       });
       
       // button update row
       btnUpdate.addActionListener(new ActionListener(){

           @Override
           public void actionPerformed(ActionEvent e) {
            
               // i = the index of the selected row
               int i = table.getSelectedRow();
               
               if(i >= 0) 
               {
                  model.setValueAt(textId.getText(), i, 0);
                  model.setValueAt(textFname.getText(), i, 1);
                  model.setValueAt(textLname.getText(), i, 2);
                  model.setValueAt(textAge.getText(), i, 3);
               }
               else{
                   System.out.println("Update Error");
               }
           }
       });
       
       frame.setSize(900,400);
       frame.setLocationRelativeTo(null);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
       
   }
   
   class MyJComboBoxEditor extends DefaultCellEditor {
	    public MyJComboBoxEditor(String[] items) {
	        super(new JComboBox(items));
	    }
	}

}