- 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
How to create a Default Cell Editor that uses a JComboBox in Java?
Create a combo box first and set some values −
JComboBox comboBox = new JComboBox(new String[]{"Product1","Product2","Product3","Product4"});
Seth the JComboBox for the editor so that the editor uses the combo box −
TreeCellEditor editor = new DefaultCellEditor(comboBox); tree.setEditable(true); tree.setCellEditor(editor);
The following is an example to create a Default Cell Editor that uses a JComboBox −
Example
package my; import javax.swing.DefaultCellEditor; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeCellEditor; public class SwingDemo { public static void main(String[] args) throws Exception { JFrame frame = new JFrame("Demo"); DefaultMutableTreeNode node = new DefaultMutableTreeNode("Products"); DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Clothing"); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Electronics"); DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("Home Decor"); DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Furniture"); node.add(node1); node.add(node2); node.add(node3); node.add(node4); DefaultMutableTreeNode one = new DefaultMutableTreeNode("Shirt"); DefaultMutableTreeNode two = new DefaultMutableTreeNode("Trousers"); DefaultMutableTreeNode three = new DefaultMutableTreeNode("Jeans"); DefaultMutableTreeNode four = new DefaultMutableTreeNode("Mobiles"); DefaultMutableTreeNode five = new DefaultMutableTreeNode("Camera"); DefaultMutableTreeNode six = new DefaultMutableTreeNode("Tablet"); DefaultMutableTreeNode seven = new DefaultMutableTreeNode("Paintings"); DefaultMutableTreeNode eight = new DefaultMutableTreeNode("Showpieces"); DefaultMutableTreeNode nine = new DefaultMutableTreeNode("Wardrobes"); DefaultMutableTreeNode ten = new DefaultMutableTreeNode("Bean Bags"); node1.add(one); node1.add(two); node1.add(three); node2.add(four); node2.add(five); node2.add(six); node3.add(seven); node3.add(eight); node4.add(nine); node4.add(ten); JTree tree = new JTree(node); for (int i = 0; i < tree.getRowCount(); i++) { tree.expandRow(i); } tree.putClientProperty("JTree.lineStyle", "Angled"); JComboBox comboBox = new JComboBox(new String[]{"Product1","Product2","Product3", "Product4"}); TreeCellEditor editor = new DefaultCellEditor(comboBox); tree.setEditable(true); tree.setCellEditor(editor); tree.setRowHeight(25); frame.add(tree); frame.setSize(600,450); frame.setVisible(true); } }
Output
- Related Articles
- How to create a Default Cell Editor that uses a JCheckBox in Java?
- How to create a Default Cell Editor with textbox in Java?
- How to create a default constructor in Java?
- How to modify the default editor of JShell in Java 9?
- How to display the items in a JComboBox in Java
- How to display the first element in a JComboBox in Java
- How to add items in a JComboBox on runtime in Java
- How to hide and display JCombobox with a JCheckBox in Java?
- How to center align the items of a JComboBox in Java?
- How to display the different font items inside a JComboBox in Java?
- How can we sort the items of a JComboBox in Java?
- How to handle action event for JComboBox in Java?
- How to create a header cell in a table using HTML5
- Create a form that uses the horizontal layout with Bootstrap
- Java Program to set JComboBox in JOptionPane

Advertisements