Something I Whipped Up... Advice for Improvements?
I made a combination of a JTextField and a JLabel, because I want something that allows text input but doesn't stay ugly-looking like a JTextField when not in use. So, I came up with this class...
class LabelField (LabelField.java)
Code Java:
package gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
/**
*
* @author Tommy
*/
public class LabelField extends JPanel {
private static GridBagConstraints C = new GridBagConstraints();
static {
C.gridx = 1;
C.gridy = 1;
C.ipadx = 10;
C.ipady = 10;
}
private static final Border DB = BorderFactory.createEmptyBorder(1, 1, 1, 1);
private static final Border HB = BorderFactory.createLineBorder(Color.GRAY, 1);
private static final Color DC = null;
private static final Color HC = Color.WHITE;
private final MouseAdapter MA = new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
l.setBackground(HC);
l.setBorder(HB);
}
public void mouseExited(MouseEvent e) {
showLabel();
l.setBackground(DC);
l.setBorder(DB);
}
public void mouseClicked(MouseEvent e) {
showTextField();
tf.requestFocusInWindow();
}
};
private final DocumentListener DA = new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
update();
}
public void insertUpdate(DocumentEvent e) {
update();
}
public void removeUpdate(DocumentEvent e) {
update();
}
private void update() {
l.setText(tf.getText());
}
};
private JLabel l;
private JTextField tf;
private int width;
private boolean labelIsShowing;
private boolean resetFieldTextOnFocus;
public LabelField() {
this("");
}
public LabelField(int width) {
this("", width);
}
public LabelField(String text) {
this(text, true);
}
public LabelField(String text, int width) {
this(text, true, width);
}
public LabelField(String text, boolean resetFieldTextOnFocus) {
this(text, resetFieldTextOnFocus, -1);
}
public LabelField(String text, boolean resetFieldTextOnFocus, int width) {
l = new JLabel(text, SwingConstants.CENTER);
l.setBorder(DB);
l.addMouseListener(MA);
l.setOpaque(true);
tf = new JTextField(text);
tf.addMouseListener(MA);
tf.getDocument().addDocumentListener(DA);
tf.getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
tf.getActionMap().put("enter", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
showLabel();
l.setBackground(DC);
}
});
if(width > 0) {
l.setMinimumSize(new Dimension(width, l.getHeight()));
tf.setMinimumSize(new Dimension(width, l.getHeight()));
}
labelIsShowing = true;
this.resetFieldTextOnFocus = resetFieldTextOnFocus;
setLayout(new GridBagLayout());
add(l, C);
}
public LabelField setText(String text) {
l.setText(text);
tf.setText(text);
return this;
}
public String getText() {
return l.getText();
}
public LabelField setResetFieldTextOnFocus(boolean reset) {
resetFieldTextOnFocus = reset;
return this;
}
public boolean isResettingFieldTextOnFocus() {
return resetFieldTextOnFocus;
}
public LabelField showLabel() {
if(!labelIsShowing) {
remove(tf);
add(l, C);
labelIsShowing = true;
revalidate();
repaint();
}
return this;
}
public LabelField showTextField() {
if(labelIsShowing) {
remove(l);
add(tf, C);
labelIsShowing = false;
revalidate();
repaint();
}
return this;
}
}
Test class (LabelFieldTest.java)
Code Java:
package gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
/**
*
* @author Tommy
*/
public class LabelFieldTest extends JFrame {
public static void main(String[] cake) {
new LabelFieldTest();
}
public LabelFieldTest() {
super();
setSize(400, 400);
setResizable(false);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
c.ipadx = 10;
c.ipady = 10;
add(new LabelField("Well...", 200), c);
/*c.gridy = 2;
add(new LabelButton("Tests!"), c);*/
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
If anyone would be willing, could he/she check out the LabelField and its test class and offer suggestions for improvement (either on my coding or on the idea)?