- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the differences between the TableCellRenderer and TableCellEditor in Java?
TableCellRenderer
- A TableCellRenderer creates a component that displays the value of a JTable cell.
- The default renderer uses JLabel to display the value of each table cell.
- The TableCellRenderer interface can be specified in two ways : By class of the object to be rendered using table.setDefaultRenderer() method and by a column using tableColumn.setCellRenderer() method and tableColumn.setHeaderRenderer() method for a specific column headers.
- The TableCellRenderer interface has only one method getTableCellRendererComponent() and this method can return different rendering components based on the value, a cell has the focus or is selected, row and column that can contain the value.
TableCellEditor
- A TableCellEditor is an interface and by default, the cells can be editable.
- A TableCellEditor can be determined by calling isCellEditable() method of a TableModel.
- If the class of a cell value is Boolean, a JCheckBox can be used. If must double click to put in edit mode, a JTextField can be used.
- The TableCellEditor interface has only one method getTableCellEditorComponent() and this method can return different editing components based on the value, a cell is selected, row and column that can contain the value.
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class TableCellRendererEditorTest extends JFrame { private JTable table; public TableCellRendererEditorTest() { setTitle("TableCellRendererEditor Test"); DefaultTableModel dtm = new DefaultTableModel() { public boolean isCellEditable(int row, int column) { return !(column == 0); } }; dtm.setDataVector(new Object[][]{{"Table Cell Renderer", "Table Cell Editor"}, {"Table Cell Renderer","Table Cell Editor"}}, new Object[]{"Renderer","Editor"}); table = new JTable(dtm); table.getColumn("Editor").setCellRenderer(new TextAreaRenderer()); table.getColumn("Editor").setCellEditor(new TextAreaEditor()); table.setRowHeight(80); JScrollPane spane = new JScrollPane(table); add(spane); setSize(400, 275); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new TableCellRendererEditorTest(); } } class TextAreaRenderer extends JScrollPane implements TableCellRenderer { JTextArea textarea; public TextAreaRenderer() { textarea = new JTextArea(); textarea.setLineWrap(true); textarea.setWrapStyleWord(true); getViewport().add(textarea); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,int row, int column) { if (isSelected) { setForeground(table.getSelectionForeground()); setBackground(table.getSelectionBackground()); textarea.setForeground(table.getSelectionForeground()); textarea.setBackground(table.getSelectionBackground()); } else { setForeground(table.getForeground()); setBackground(table.getBackground()); textarea.setForeground(table.getForeground()); textarea.setBackground(table.getBackground()); } textarea.setText((String) value); textarea.setCaretPosition(0); return this; } } class TextAreaEditor extends DefaultCellEditor { protected JScrollPane scrollpane; protected JTextArea textarea; public TextAreaEditor() { super(new JCheckBox()); scrollpane = new JScrollPane(); textarea = new JTextArea(); textarea.setLineWrap(true); textarea.setWrapStyleWord(true); scrollpane.getViewport().add(textarea); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { textarea.setText((String) value); return scrollpane; } public Object getCellEditorValue() { return textarea.getText(); } }
Output
- Related Articles
- What are the differences between C++ and Java?
- What are the differences between C and Java?
- What are the differences between Java classes and Java objects?
- What are the differences between recursion and iteration in Java?
- What are the differences between GridLayout and GridBagLayout in Java?
- What are the differences between JTextField and JTextArea in Java?
- What are the differences between JRadioButton and JCheckBox in Java?
- What are the differences between StackOverflowError and OutOfMemoryError in Java?
- What are the differences between ClassNotFoundException and NoClassDefFoundError in Java?\n
- What are the differences between length and length () in Java?\n
- What are the differences between JFrame and JDialog in Java?\n
- What are the differences between a class and an interface in Java?
- What are the differences between default constructor and parameterized constructor in Java?
- What are the differences between tight coupling and loose coupling in Java?
- What are the differences between compareTo() and compare() methods in Java?\n

Advertisements