All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mdlaf.components.tree.MaterialTreeCellEditor Maven / Gradle / Ivy

There is a newer version: 1.1.4
Show newest version
package mdlaf.components.tree;

import mdlaf.components.textfield.MaterialTextFieldUI;

import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
import java.awt.Component;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @author https://github.com/vincenzopalazzo
 */
public class MaterialTreeCellEditor extends DefaultTreeCellEditor {

	private JTextField textField;

	public MaterialTreeCellEditor (JTree tree, DefaultTreeCellRenderer renderer) {
		super (tree, renderer);
		init ();
	}

	public MaterialTreeCellEditor (JTree tree, DefaultTreeCellRenderer renderer, TreeCellEditor editor) {
		super (tree, renderer, editor);
		init ();
	}

	private void init () {
		textField = new JTextField ();
		textField.setUI (new MaterialTextFieldUI());

		textField.addKeyListener (new KeyAdapter () {
			@Override
			public void keyTyped (KeyEvent e) {
				if (e.getKeyChar () == KeyEvent.VK_ENTER)
					stopCellEditing ();
			}
		});
	}

	@Override
	public Component getTreeCellEditorComponent (JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
		textField.setText (value.toString ());
		return textField;
	}

	@Override
	public Object getCellEditorValue () {
		return textField.getText ();
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy