In Java, Cell Editor Interface can be used with the JTree and JTable Classes to edit their values in a specific cell, That would not be easy without using this interface. We can still do that by using the JTextFeild or Other Java Swing Component but will be hard to manage it. 

Using Cell Editor with JTable

We can use a cell Editor Interface to edit the value of the JTree or JTable without using the other components. The CellEditor Interface his two other subinterfaces which are TableCellEditor and TreeCellEditor. The TableCellEditor Interface is suitable to use when you are working with the JTable and the TreeCellEditor is suitable when you are working with the JTree. They are specific Interfaces and the Cell Editor is the General Interface. 

Methods In the CellEditor Interface

There are about 7 methods that we need to override in the cellEditor. Below is the List with the Description. We will use them in our test program. 

  1. Adding Cell Editor Listener

  2. In the Cell Editor Listener Interface, we have a method  addCellEditorListener(CellEditorListenere listener) , which helps us to register our editor to the CellEdiorListener. 

  3. Method cancelCellEditing()

  4. This method is a void method and tells the Editor not to accept when there is a value that is partial and not complete. 

  5. Method getCellEditorValue()

  6. This method returns the Object Type. That is it returns Whatever the Editor has. 

  7. Method isCellEditable()

  8. This method checks if the current cell that we are in is editable or no. it accepts one parameter which is an EventObject and returns a boolean value. 

  9. Method removeCellEditorListener()

  10. This method is used to remove the CellEditor Listener that listens to the events occurring there on the CellEditor. 

  11. Method shouldSelectedCell()

  12. This method checks whether the cell you want to edit is selected or not. returns a boolean value true if it is selected otherwise return false.

  13. Method stopCellEditing()

  14. This method returns the value of a boolean value. and is used to tell the cell editor to accept the partially edited value and stop the editing. 

Example for the Cell Editor Interface

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package AlixaProDev.EventListeners;
import javax.swing.*;
import javax.swing.event.CellEditorListener;
import java.awt.*;
import java.util.EventObject;

public class TableDemo {
    Font myFavFont;
    JFrame frame=new JFrame();
    TableDemo(){
        myFavFont = new Font("Mv Boli",Font.ITALIC,34);
        String [] [] rowsData = {{"1","Hazrat ALi"},{"2","ALixaDev"}};
        String [] colName = {"RollNo","Name"};


        JTable table = new JTable(rowsData,colName);
        table.setBounds(50,50,500,300);
        table.setBackground(Color.MAGENTA);
        table.setRowHeight(45);
        table.setFont(myFavFont);
        JScrollPane scrollPane = new JScrollPane(table);
        frame.add(scrollPane);
        frame.setTitle("AlixaProDev");
        frame.setSize(500,400);
        frame.setVisible(true);
    }
    // we need to create our cell Own Cell Editor as follows 
    private class MyCellEditor implements CellEditor{

        // this method will gives us the value of the SetEditor 
        // as we have explained we will implement this in the next stage
        @Override
        public Object getCellEditorValue () {
            return null;
        }

        // to check whether the cell is Editable
        @Override
        public boolean isCellEditable (EventObject anEvent) {
            return false;
        }
        // thie returns a boolean value of the cell is selcted
        @Override
        public boolean shouldSelectCell (EventObject anEvent) {
            return false;
        }

        // This method is called when we want t stop the editing
        @Override
        public boolean stopCellEditing () {
            return false;
        }

        // We want to accept the partial values 
        @Override
        public void cancelCellEditing () {

        }

        // add the CellEditorListener
        @Override
        public void addCellEditorListener (CellEditorListener l) {

        }

        // remove the cell editor listener
        @Override
        public void removeCellEditorListener (CellEditorListener l) {

        }
    }
    public static void main(String[] args) {
        new TableDemo();
    }
}  
Cell Editor in java


 I know this topic is a little complicated. we will look into this when we are working with the cell editor listener interface in our next article

Post a Comment

Previous Post Next Post