nl.cloudfarming.client.util.swing.InlineEditLabel Maven / Gradle / Ivy
/**
* Copyright (C) 2010-2012 Agrosense [email protected]
*
* Licensed under the Eclipse Public License - v 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.eclipse.org/legal/epl-v10.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.cloudfarming.client.util.swing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.io.Serializable;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author Timon Veenstra
*/
public class InlineEditLabel extends JPanel implements Serializable{
public enum State {
LABEL, EDIT
}
private final JLabel label;
private final JTextField textField = new JTextField();
private State state = State.LABEL;
protected JLabel getLabel() {
return label;
}
public InlineEditLabel() {
super(null);
setPreferredSize(new Dimension(220, 26));
//
// label
//
label = new JLabel();
label.setPreferredSize(new Dimension(220, 20));
label.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
nameLabelMouseClicked(evt);
}
});
label.setVisible(true);
add(label);
//
// textfield
//
textField.setPreferredSize(getPreferredSize());
textField.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
nameTextFocusLost(evt);
}
});
textField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
nameTextKeyPressed(evt);
}
});
add(textField);
textField.setVisible(false);
addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent e) {
Dimension newSize = getSize();
label.setSize(new Dimension(newSize.width, newSize.height-4));
textField.setSize(newSize);
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
});
}
private void nameLabelMouseClicked(java.awt.event.MouseEvent evt) {
setState(State.EDIT);
}
public void setValue(String newValue) {
label.setText(newValue);
textField.setText(newValue);
}
public String getValue() {
return label.getText();
}
private void processNameChange() {
setState(State.LABEL);
}
private void nameTextFocusLost(java.awt.event.FocusEvent evt) {
processNameChange();
}
private void nameTextKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
processNameChange();
}
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
switch (state) {
case LABEL:
label.setText(textField.getText());
label.setVisible(true);
textField.setVisible(false);
setValue(textField.getText());
break;
case EDIT:
label.setVisible(false);
textField.setVisible(true);
textField.setSelectionStart(0);
textField.setSelectionEnd(textField.getText().length());
textField.requestFocus();
break;
default:
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy