How to Add TextArea to a Combobox In Java Netbeans
In this Java Tutorial we will see How To Create a JCombobox 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.FlowLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
/**
*
* @author 1BestCsharp
*/
public class TextAreaComboboxFrame extends JFrame{
private JComboBox<TextAreaComboboxElement> comboBox;
private JTextArea displayTextArea;
public TextAreaComboboxFrame(){
// Set up the JFrame
setTitle("TextArea Combobox");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the layout manager for the JFrame
setLayout(new FlowLayout());
// Create a JComboBox and set its renderer
comboBox = new JComboBox<>();
comboBox.setRenderer(new TextAreaComboboxRenderer());
// Add three items with multiline text to the combobox
comboBox.addItem(new TextAreaComboboxElement("Item 1", "This is the first item.\nIt has multiple lines."));
comboBox.addItem(new TextAreaComboboxElement("Item 2", "This is the second item.\nIt also has multiple lines."));
comboBox.addItem(new TextAreaComboboxElement("Item 3", "This is the third item.\nIt's multiline as well."));
// Add an item listener to handle item selection
comboBox.addItemListener((e) -> {
TextAreaComboboxElement selectedItem = (TextAreaComboboxElement)comboBox.getSelectedItem();
if(selectedItem != null){
displayTextArea.setText(selectedItem.getText());
}
});
add(comboBox);
displayTextArea = new JTextArea(5,20);
displayTextArea.setEditable(false);
add(new JScrollPane(displayTextArea));
setSize(400,400);
setLocationRelativeTo(null);
// Set the initial text in the display area
TextAreaComboboxElement initialItem = comboBox.getItemAt(0);
displayTextArea.setText(initialItem.getText());
}
// Create an Inner class representing elements in the combobox
public static class TextAreaComboboxElement{
private final String label;
private final String text;
// constructor
public TextAreaComboboxElement(String label, String text){
this.label = label;
this.text = text;
}
// Getters
public String getLabel(){ return label; }
public String getText(){ return text; }
}
// Create an Inner class representing the renderer for the combobox
private class TextAreaComboboxRenderer extends JPanel implements ListCellRenderer<TextAreaComboboxElement>{
private final JLabel label;
private final JTextArea textArea;
public TextAreaComboboxRenderer(){
setLayout(new BorderLayout());
label = new JLabel();
add(label, BorderLayout.NORTH);
textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
add(textArea, BorderLayout.CENTER);
}
// Implement the rendering of combobox elements
@Override
public Component getListCellRendererComponent(JList<? extends TextAreaComboboxElement> list, TextAreaComboboxElement value, int index, boolean isSelected, boolean cellHasFocus) {
// Set the label and text of the renderer components
label.setText(value.getLabel());
textArea.setText(value.getText());
// Set the background and foreground based on selection
if(isSelected){
setBackground(Color.BLACK);
getComponent(0).setForeground(Color.YELLOW);
}
else
{
setBackground(list.getBackground());
getComponent(0).setForeground(list.getForeground());
//setForeground(list.getForeground());
}
return this;
}
}
public static void main(String[] args) {
TextAreaComboboxFrame frame = new TextAreaComboboxFrame();
frame.setVisible(true);
}
}
The Final Result:
More Java Projects:
Download Projects Source Code