Java JTable With JTextArea Column

How to Create JTextArea Inside JTable Cells In Java Netbeans

Create a JTable with a JTextArea Column in Java Swing




In this Java Tutorial we will see How To Create a JTable with a JTextArea column to display multiline text in the table cell.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.





Project Source Code:



package new_tutorials;

import com.formdev.flatlaf.FlatIntelliJLaf;
import java.awt.Component;
import java.util.List;
import java.util.ArrayList;
import javax.swing.AbstractCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;

/**
 *
 * @author 1BestCsharp
 */
public class JtableWithJtextAreaColumn extends JFrame{

    public JtableWithJtextAreaColumn()
    {
        setTitle("JTable With JtextArea Column");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 400);
        setLocationRelativeTo(null);
        /*
        try{
            UIManager.setLookAndFeel(new FlatIntelliJLaf());
        }catch(UnsupportedLookAndFeelException ex){}
        */
        
        initializeGUI();
    }
    
    
    private void initializeGUI()
    {
        JTable table = new JTable(new CustomTableModel());
        table.setRowHeight(60);
        table.setShowHorizontalLines(true);
        table.setShowVerticalLines(true);
        table.getColumnModel().getColumn(3).setCellRenderer(new JTextAreaCellRenderer());
        table.getColumnModel().getColumn(3).setCellEditor(new JTextAreaCellEditor());
        
        JScrollPane scrollpane = new JScrollPane(table);
        add(scrollpane);
        
        setVisible(true);
    }
    
    
    public static void main(String[] args) {
        
        new JtableWithJtextAreaColumn();
        
    }
    
    
    // Custom Table Model for the JTable
    private class CustomTableModel extends AbstractTableModel
    {
        private final String[] columnNames = {"First Name","Last Name","Age","Address"};
        private final List<Person> data = new ArrayList<>();
        
        public CustomTableModel()
        {
            data.add(new Person("John", "Silva", 30, "123 Main St, City A, Country X, Postal Code: 12345, 456 Elm St, City B, Country Y, Postal Code: 56789 - 123 Main St, City A, Country X, Postal Code: 12345, 456 Elm St, City B, Country Y, Postal Code: 56789 - 123 Main St, City A, Country X, Postal Code: 12345, 456 Elm St, City B, Country Y, Postal Code: 56789 - 123 Main St, City A, Country X, Postal Code: 12345, 456 Elm St, City B, Country Y, Postal Code: 56789 - 123 Main St, City A, Country X, Postal Code: 12345, 456 Elm St, City B, Country Y, Postal Code: 56789 - 123 Main St, City A, Country X, Postal Code: 12345, 456 Elm St, City B, Country Y, Postal Code: 56789"));
            data.add(new Person("Jane", "Doe", 25, "456 Elm St, City B, Country Y, Postal Code: 56789"));
            data.add(new Person("Michael", "Johnson", 40, "789 Oak St, City C, Country Z, Postal Code: 10111"));
            data.add(new Person("Emily", "Smith", 28, "321 Pine St, City D, Country X, Postal Code: 67890"));
            data.add(new Person("Daniel", "Brown", 35, "567 Cedar St, City E, Country Y, Postal Code: 11223"));
            data.add(new Person("Olivia", "Jones", 22, "876 Maple St, City F, Country Z, Postal Code: 44556"));
            data.add(new Person("David", "Martinez", 33, "234 Birch St, City G, Country X, Postal Code: 78901"));
            data.add(new Person("Sophia", "Taylor", 29, "987 Willow St, City H, Country Y, Postal Code: 23456"));
            data.add(new Person("William", "Clark", 45, "543 Rose St, City I, Country Z, Postal Code: 11234"));
            data.add(new Person("Emma", "Anderson", 27, "210 Sunflower St, City J, Country X, Postal Code: 56789"));
        }

        @Override
        public int getRowCount() { return data.size();}

        @Override
        public int getColumnCount() { return columnNames.length;}
        
        @Override
        public String getColumnName(int columnIndex){ return columnNames[columnIndex];}

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
           
            Person person = data.get(rowIndex);
            return switch(columnIndex){
                case 0 -> person.getFirstName();
                    case 1 -> person.getLastName();
                        case 2 -> person.getAge();
                            case 3 -> person.getAddress();
                            default -> null;
            };
            
        }
        
        
        public Class<?> getClassName(int columnIndex){
            return columnIndex == 3 ? JTextArea.class:Object.class;
        }
        
        
        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex){
            return columnIndex == 3;
        }
        
        
        @Override
        public void setValueAt(Object value, int rowIndex, int columnIndex){
            
            if(columnIndex == 2 && value instanceof String)
            {
                data.get(rowIndex).setAddress((String)value);
                fireTableCellUpdated(rowIndex, columnIndex);
            }
            
        }
        
    }
    
    
    // Custom cell renderer for JTextArea column
    private class JTextAreaCellRenderer extends DefaultTableCellRenderer
    {
        private final JTextArea textArea;
        
        // Constructor to initialize JTextArea properties
        public JTextAreaCellRenderer()
        {
            textArea = new JTextArea();
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            textArea.setOpaque(true);
        }
        
        // Method to render JTextArea component in table cell
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            textArea.setText(value != null ? value.toString() : "");
            return textArea;
        }
        
    }  
    
    // Custom cell editor for JTextArea column
    private class JTextAreaCellEditor extends AbstractCellEditor implements TableCellEditor
    {
        private final JTextArea textArea;
        private final JScrollPane scrollPane;
        
        public JTextAreaCellEditor()
        {
            textArea = new JTextArea();
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            scrollPane = new JScrollPane(textArea);
            scrollPane.setBorder(null);
        }

        // Method to retrieve the edited cell value
        @Override
        public Object getCellEditorValue() {
           return textArea.getText();
        }

        // Method to return the JTextArea component for editing
        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            
            textArea.setText(value != null ? value.toString() : "");
            return scrollPane;
            
        }
    }
    
    
// Model class representing a person's information
    private class Person 
    {
        private String firstName;
        private String lastName;
        private int age;
        private String address;
        
        public Person(String fname, String lname, int age, String adrs){
            this.firstName = fname;
            this.lastName = lname;
            this.age = age;
            this.address = adrs;
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public int getAge() {
            return age;
        }

        public String getAddress() {
            return address;
        }
        
        public void setAddress(String address){ this.address = address; }
        
        
    }
    
}


The Final Result:

JTable with JTextArea Cells in Java





Share this

Related Posts

Latest
Previous
Next Post »