com.darwinsys.swingui.ClickToActivateTextField Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of darwinsys-api Show documentation
Show all versions of darwinsys-api Show documentation
Ian Darwin's assorted Java stuff,
assembled as an API.
package com.darwinsys.swingui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextField;
/**
* A TextField that looks like a label until you click on it
*/
public class ClickToActivateTextField extends JTextField {
public ClickToActivateTextField() {
this(null);
}
public ClickToActivateTextField(String label) {
super(label);
setEnabled(false);
// clicking enables
addMouseListener(new MyMouseListener());
// hitting return disables
addActionListener(new MyActionListener());
}
class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
setEnabled(true);
}
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
setEnabled(false);
}
}
}