==[[JTextComponent 1]]==
[[Jython JTextComponent 1]] | [[Jython Swing]] | [[Swing]] | [[Jython]] | [[Python]] |
*メニューとアクションの割付
*キーボード押下との割付
*Undo、Redoの実装Undo、[[R]]edoの実装
*Documentの利用
*Documentのイベント感知
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.[[JFrame]]; import javax.swing.JMenuJ[[Menu]]; import javax.swing.JMenuBarJ[[Menu]]Bar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities[[Swing]]Utilities;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
*/
public class JTextTest {
private [[JFrame ]] frame;
private Map<Object, Action> actionMap = new HashMap<Object, Action>();
private UndoAction undoAction;
private RedoAction [[R]]edoAction redoAction;
private JMenu getMenuJ[[Menu]] get[[Menu]]() { JMenu J[[Menu]] menu = new JMenuJ[[Menu]]("Edit");
menu.add(actionMap.get(DefaultEditorKit.cutAction));
menu.add(undoAction);
redoAction = new RedoAction[[R]]edoAction();
menu.add(redoAction);
}
private void createUI() {
frame = new [[JFrame]]("TextTest"); frame.setDefaultCloseOperation([[JFrame]].EXIT_ON_CLOSE);
JTextArea txtArea = new JTextArea(10, 30);
// Documentのイベント感知(Undo Redo [[R]]edo 管理を行う)
Document doc = txtArea.getDocument();
doc.addUndoableEditListener(
);
frame.getContentPane().add(new JScrollPane(txtArea), BorderLayout.CENTERCENTE[[R]]);
// TextComponentが持つActionをMapに格納
createActionMap(txtArea);
// メニューを作成
JMenu J[[Menu]] menu = getMenuget[[Menu]](); JMenuBar J[[Menu]]Bar mb = new JMenuBarJ[[Menu]]Bar();
mb.add(menu);
frame.setJMenuBarsetJ[[Menu]]Bar(mb);
// テキストのUndo、Redoにキーを割り当てる テキストのUndo、[[R]]edoにキーを割り当てる (Ctrl+z,Ctrl+y)
InputMap inputMap = txtArea.getInputMap();
KeyStroke undoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.CTRL_MASKCT[[R]]L_MASK); KeyStroke redoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Y,Event.CTRL_MASKCT[[R]]L_MASK);
inputMap.put(undoKey, undoAction);
inputMap.put(redoKey, redoAction);
}
public static void main(String[] args) {
SwingUtilities[[Swing]]Utilities.invokeLater( new Runnable[[R]]unnable(){
public void run() {
JTextTest jft = new JTextTest();
}
updateState();
// Redo [[R]]edo を可能に
redoAction.updateState();
}
@SuppressWarnings("serial")
class RedoAction [[R]]edoAction extends AbstractAction { public RedoAction[[R]]edoAction() { super("Redo[[R]]edo");
setEnabled(false);
}
}
public void updateState() {
setEnabled(undo.canRedocan[[R]]edo());
}
}
}