せるえでぃた
JTableなどでおなじみのセル・エディタの尻尾をつかみました。
どうもSwingはL&Fの仕組みのせいでわかりづらい。
import java.awt.Rectangle; import java.awt.event.*; import javax.swing.*; /* セルエディタのひな形 * 作成日: 2006/02/01 */ public class CellEditorTest extends JPanel { JTextField editor = new JTextField("foo"); Rectangle bounds = new Rectangle(100,100,100,20); boolean atEdit = false; CellEditorTest() { this.addMouseListener(new ClickListener()); this.setLayout(null); } void startEdit() { System.out.println("startEdit()"); editor.setBounds(bounds); this.add(editor); editor.setEnabled(true); this.repaint(); atEdit = true; } void editingStopped() { System.out.println("editingStopped()"); this.remove(editor); this.repaint(bounds); atEdit = false; } class ClickListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { System.out.println("mouseClicked()"); if (atEdit) { editingStopped(); return; } startEdit(); } } public static void main(String[] args) { JFrame frame = new JFrame(); CellEditorTest test = new CellEditorTest(); frame.getContentPane().add(test); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(640, 480); frame.setVisible(true); } }
註:多分、サンプルとしても完成度が低いです。