Making an Editable JLabel in Swing You'd think this would be included by default

One of the things I’ve found myself wanting to do fairly often is make an editable JLabel in Swing. Unfortunately there isn’t a default component for doing this, you have to make your own…

…so I did, and you can find the code on GitHub.

The EditableJLabel actually extends JPanel, not JLabel, and switches between the two different states (Normal and Hover) using a CardLayout. Of course it’s not quite as simple as detecting mouse enters and exits, there are a bunch of times when you don’t want the panel to change back (such as the TextField having focus) and vica versa. The source could should explain the behaviour however I’ve modelled it to what feels right for me, so if it feels odd to use, I apologize.

// Create the EditableJLabel
EditableJLabel label = new EditableJLabel("Meow");

label.setPreferredSize(new Dimension(150,24));

// Create and add a listener for changes in the value of EditableJLabel
ValueChangedListener valueListener = new ValueChangedListener() {
	public void valueChanged(String value, JComponent source) {
		// Do something with the new value

// Add the EditableJLabel to the content pane

I hope someone else finds this useful. I’d love to hear what you think about its behaviour and any extensions you’ve made to it (An EditableJLabelComboBox perhaps?).


A. J. McMinn


8th of January, 2011

  • Karen Urate

    Continue doing what you’re doing. Good stuff. Thanks.

  • Rashmi

    thanx for making my work easy . i was looking out for exactly the same thing

  • suneel

    i am not understanding clearly,please explain with an example clearly .