Java JList With JTextArea

How to Add TextArea to a JList In Java Netbeans

How to Add TextArea to a JList In Java Netbeans


In this Java Tutorial we will see How To Create a JList With JTextArea Inside It In Java Using Netbeans.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.





Project Source Code:



package new_tutorials;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;

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

    private JList<TextAreaListElement> textAreaList;
    
    public TextAreaInJListFrame(){
        setTitle("TextArea In JList");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        
        List<TextAreaListElement> elements = new ArrayList<>();
        elements.add(new TextAreaListElement("Item 1", "Description for Item Line Description for Item 1 Line 2 Description for Item 1 Line 3"));
        elements.add(new TextAreaListElement("Item 2", "Description Description DescriptionDescription DescriptionDescription Description Description Description Description for Item 2"));
        elements.add(new TextAreaListElement("Item 3", "Description for Item 3"));
        elements.add(new TextAreaListElement("Item 11", "Description for Item Line Description for Item 1 Line 2 Description for Item 1 Line 3"));
        elements.add(new TextAreaListElement("Item 22", "Description Description DescriptionDescription DescriptionDescription Description Description Description Description for Item 2"));
        elements.add(new TextAreaListElement("Item 33", "Description for Item 3"));
        elements.add(new TextAreaListElement("Item 41", "Description for Item Line Description for Item 1 Line 2 Description for Item 1 Line 3"));
        elements.add(new TextAreaListElement("Item 52", "Description Description DescriptionDescription DescriptionDescription Description Description Description Description for Item 2"));
        elements.add(new TextAreaListElement("Item 63", "Description for Item 3"));
        elements.add(new TextAreaListElement("Item 71", "Description for Item Line Description for Item 1 Line 2 Description for Item 1 Line 3"));
        elements.add(new TextAreaListElement("Item 82", "Description Description DescriptionDescription DescriptionDescription Description Description Description Description for Item 2"));
        elements.add(new TextAreaListElement("Item 93", "Description for Item 3"));
        elements.add(new TextAreaListElement("Item 211", "Description for Item Line Description for Item 1 Line 2 Description for Item 1 Line 3"));
        elements.add(new TextAreaListElement("Item 992", "Description Description DescriptionDescription DescriptionDescription Description Description Description Description for Item 2"));
        elements.add(new TextAreaListElement("Item 543", "Description for Item 3"));

        textAreaList = new JList<>(elements.toArray(TextAreaListElement[] :: new));
        textAreaList.setCellRenderer(new TextAreListRenderer());
        
        JScrollPane scrollPane = new JScrollPane(textAreaList);
        add(scrollPane, BorderLayout.CENTER);
        setSize(400,300);
        setLocationRelativeTo(null);
        
    }
    
    
    public static class TextAreaListElement{
        private final String label;
        private final String description;
        
        public TextAreaListElement(String label, String description){
            this.label = label;
            this.description = description;
        }
        
        public String getLabel(){
            return label;
        }
        
        public String getDescription(){
            return description;
        }
        
    }
    
    
    // create a renderer to display the textArea
    private class TextAreListRenderer extends JTextArea implements ListCellRenderer<TextAreaListElement>{

        public TextAreListRenderer(){
            setLineWrap(true);
            setWrapStyleWord(true);
            setMargin(new Insets(5, 10, 5, 10));
            setFont(new Font("Arial", Font.PLAIN, 16));
            setPreferredSize(new Dimension(300,80));
        }
        
        @Override
        public Component getListCellRendererComponent(JList<? extends TextAreaListElement> list, TextAreaListElement value, int index, boolean isSelected, boolean cellHasFocus) {
         
            setEnabled(list.isEnabled());
            setText(value.getLabel()+ "\n" + value.getDescription());
            
            if(isSelected){
                setBackground(Color.BLACK);
                setForeground(Color.YELLOW);
            }
            else{
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
            
            return this;
        }
        
    }
    
    
    public static void main(String[] args) {
        
        TextAreaInJListFrame frame = new TextAreaInJListFrame();
        frame.setVisible(true);
        
    }
}


The Final Result:


Java JList With JTextArea

JList With JTextArea In Java









Share this

Related Posts

Latest
Previous
Next Post »