After getting the knowledge about the JTable components and creation with column header, you will be able to create a custom cell renderer in a JTable component.After getting the knowledge about the JTable components and creation with column header, you will be able to create a custom cell renderer in a JTable component.
After getting the knowledge about the JTable components and creation with column header, you will be able to create a custom cell renderer in a JTable component. Here, first of all you will know about the cell renderer in JTable. The cell renderer is the component of JTable that can be used to display the cells in a column. The JTable describes a specific renderer to the particular column. For this the JTable invokes the table model to use the getColumnClass method that takes the data types of the column's cells.
The custom cell renderer means user created renderer according to requirement. The given code with description is to provide the facility for creating a custom cell renderer in a JTable to use some Java methods and APIs.
Description of program:
In this program, you will see how to create a custom cell renderer. To create a table cell renderer you need a JTable, so that this program can create a table containing the data and column with column header in the very first step. After that you will create a custom cell renderer to apply the CustomTableCellRenderer() method by using the setCellRenderer method that specify the cells in a particular column to use renderer. The CustomTableCellRenderer method extends the DefaultTableCellRenderer that helps you to create a default table cell renderer. Finally, it will create a custom cell renderer that will display in given output of table. The even rows of a JTable contains cyan color and odd rows contains lightGray color but when you select it, the selected row or rows will look like green color.
Description of code:
This is the constructor of DefaultTableCellRenderer class that can be used for displaying the individual cells in a JTable. It constructs a default table cell renderer.
getTableCellRendererComponent( JTable table, Object
obj, boolean isSelected, boolean hasFocus, int row, int column):
This is the method that returns default table cell renderer. It takes the following agruments:
This is a JTable.
obj: This is the value that is assigning in the cells at the specified positions (rows and columns).
isSelected: When any cell is selected then it becomesl true.
hasFocus: This the the specific cell that is true when you focus any cell of a JTable.
row: This is the row of cell renderer.
column: This is the column of cell renderer.
This method returns the state of column either true or false. If any cell is selected then it will return true otherwise false.
Here is the code of program:
Output of program: