Beh... anche così non è male:
codice:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class EditLabel extends JFrame implements ActionListener {
private class DoubleClick extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent me) {
if (me.getClickCount() == 2) {
switchLabelTextField( true );
}
}
}
private JPanel jpLabel;
private JLabel lbl;
private JTextField txt;
public EditLabel() {
Container c = getContentPane();
c.setLayout( new BorderLayout() );
jpLabel = new JPanel( new BorderLayout() );
lbl = new JLabel("Fai Doppio Clic:");
lbl.addMouseListener( new DoubleClick() );
txt = new JTextField();
txt.addActionListener( this );
jpLabel.add(lbl, BorderLayout.WEST);
c.add(jpLabel, BorderLayout.NORTH);
setTitle("Edit JLabel with Double-Click");
setSize(800, 600);
setLocationRelativeTo( null );
setDefaultCloseOperation( EXIT_ON_CLOSE );
setVisible( true );
}
private void switchLabelTextField(boolean lblToTxt) {
if ( lblToTxt ) {
txt.setText( lbl.getText() );
jpLabel.remove( lbl );
jpLabel.add(txt, BorderLayout.WEST);
txt.requestFocusInWindow();
} else {
jpLabel.remove( txt );
jpLabel.add(lbl, BorderLayout.WEST);
}
jpLabel.doLayout();
doLayout();
repaint();
}
@Override
public void actionPerformed(ActionEvent ae) {
lbl.setText( txt.getText() );
switchLabelTextField( false );
}
public static void main(String[] args) {
EditLabel e = new EditLabel();
}
}
Ciao.