ねごとめも背景

    ねごとめも



    作成したプログラムは、こちらでも公開しています。
    よかったら覗いてみてください。_(┐「ε:)_
    → javascript,SVG で作成したプログラム置き場
    → プログラム一覧 - (2)

    Java JTableクラス、複数行セル( JTextArea )の実装(2) [ キーアクション制御 ]

    前回の JTableクラス、複数行セル( JTextArea )の実装(1)[ キーアクション制御 ] を修正。
    テストした範囲では動作は前回と同じ。
    前回作成したプログラムは JTable と JTextArea 間でフォーカスのやり取りをしながら値を変更してた。
    今回はテキストエリアにフォーカスを当てたままの状態でテーブルの値を変更するように修正。
    修正してて気づいたのは、前回のは、望む値が出力されないと、その値を出力するためにコード追加。
    追加してもまだ望む値が出力されないとコード追加。
    その繰り返しでファイルサイズは大きくなるわ、ソースコードが汚くなるわの悪循環。
    今回は、 ”テキストエリアへフォーカスを当てたままの状態での値の変更” を頭に置いといて書いたためか
    少しだけファイルサイズも小さく、ソースコードも少しだけシンプルになった。
    コードが汚く処理速度が遅くても望む結果が出力できればっ!\(^o^)/

    FocusTest.java

    
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.InputEvent;
    import java.awt.event.InputMethodEvent;
    import java.awt.event.InputMethodListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.EventObject;
    import javax.swing.AbstractAction;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.KeyStroke;
    import javax.swing.ListSelectionModel;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.border.LineBorder;
    import javax.swing.event.CellEditorListener;
    import javax.swing.event.ChangeEvent;
    import javax.swing.table.TableCellEditor;
    import javax.swing.table.TableCellRenderer;
    
    class FocusTest extends JFrame
    {
    	UndoRedoTable undoTable;
    	static JTextArea textArea;
    	FocusTest()
    	{
    		setTitle("Focus Test");
    		setLookAndFeel();
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		String[] header={"A","B","C"};
    		String[][] tbody=
    		{
    			{"a-0","b-0","c-0"},
    			{"a-1","b-1","c-1"},
    			{"a-2","b-2","c-2"},
    			{"a-3","b-3","c-3"}
    		};
    		getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.PAGE_AXIS));
    		undoTable=new UndoRedoTable(tbody,header);
    		JScrollPane scrollPane=new JScrollPane(undoTable);
    		add(scrollPane);
    		textArea=new JTextArea(50,100);
    		textArea.setBorder(new LineBorder(Color.white,8,false));
    		textArea.setText((String)undoTable.getValueAt(0,0));
    
    		undoTable.setTextArea(textArea);
    		JPanel menu=new JPanel();
    		JLabel label=new JLabel("行:");
    		Font font=new Font("Meiryo",Font.PLAIN,12);
    		label.setFont(font);
    		menu.add(label);
    		JTextField[] textField=new JTextField[2];
    		textField[0]=new JTextField("0",10);
    		textField[0].setHorizontalAlignment(JTextField.RIGHT);
    		textField[0].setEditable(false);
    		menu.add(textField[0]);
    		menu.add(Box.createRigidArea(new Dimension(30,1)));
    
    		label=new JLabel("列:");
    		label.setFont(font);
    		menu.add(label);
    		textField[1]=new JTextField("0",10);
    		textField[1].setHorizontalAlignment(JTextField.RIGHT);
    		textField[1].setEditable(false);
    		menu.add(textField[1]);
    		undoTable.setTextField(textField);
    		add(menu);
    
    		scrollPane=new JScrollPane(textArea);
    		add(scrollPane);
    		Dimension dimension=Toolkit.getDefaultToolkit().getScreenSize();
    		int width=(int)dimension.getWidth();
    		int height=(int)dimension.getHeight();
    		setBounds((width-600)/2,(height-400)/2,600,400);
    	}
    	public static void main(String[] args)
    	{
    		new FocusTest().setVisible(true);
    		textArea.requestFocus();
    		String str=textArea.getText();
    		textArea.setCaretPosition(str.length());
    	}
    	private void setLookAndFeel()
    	{
    		try
    		{
    			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    			SwingUtilities.updateComponentTreeUI(this);
    		}
    		catch(Exception ex){}
    	}
    }
    
    class UndoRedoTable extends JTable
    {
    	private JTextArea textArea;
    	private JTextField[] textField;
    	private boolean flg=true;
    	UndoRedoTable(String[][] tbody,String[] header)
    	{
    		super(tbody,header);
    		getTableHeader().setReorderingAllowed(false);
    		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		//setDefaultEditor(Object.class,null);
    		setColumnSelectionAllowed(true);
    		setRowSelectionAllowed(true);
    		//setSurrendersFocusOnKeystroke(true);
    		//setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    		//setRowHeight(getRowHeight()*2);
    		for(int i=0;i<getColumnCount();i++)
    		{
    			getColumn(getColumnName(i)).setCellEditor(new TextAreaCellEditor());
    		}
    		setSelectionBackground(Color.red);
    		//setFocusTraversalKeysEnabled(false);
    		//KeyStroke tab=KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0);
    		//getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(tab,"none");
    		//KeyStroke ctrl=KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK,0);
    		//getInputMap(WHEN_FOCUSED).put(ctrl,"none");
    		changeSelection(0,0,false,false);
    		addMouseListener(new MouseAdapter()
    		{
    			public void mouseReleased(MouseEvent e)
    			{
    				UndoRedoTable tb=(UndoRedoTable)(e.getSource());
    				int row=tb.getSelectedRow();
    				int col=tb.getSelectedColumn();
    				textField[0].setText(String.valueOf(row+1));
    				textField[1].setText(String.valueOf(col+1));
    				textArea.setText((String)tb.getValueAt(row,col));
    				textArea.requestFocus();
    			}
    		});
    		addKeyListener(new KeyAdapter()
    		{
    			public void Typed(KeyEvent e){}
    			public void keyPressed(KeyEvent e){}
    			public void keyReleased(KeyEvent e){}
    		});
    	}
    	protected void setTextArea(JTextArea text)
    	{
    		textArea=text;
    		textArea.setFocusTraversalKeysEnabled(false);
    		//KeyStroke tab=KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0);
    		//textArea.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(tab,"none");
    		textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)//enter
    			{
    				int row=getSelectedRow();
    				int col=getSelectedColumn();
    				row++;
    				if(row==getRowCount()){ row=0; col++; }
    				if(col==getColumnCount()){ col=0; }
    				changeSelection(row,col,false,false);
    				textArea.setText((String)getValueAt(row,col));
    				System.out.println("enter");
    			}
    		});
    		textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,InputEvent.SHIFT_DOWN_MASK),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)//tab + shift
    			{
    				int row=getSelectedRow();
    				int col=getSelectedColumn();
    				col--;
    				if(col==-1){ col=getColumnCount()-1; row--; }
    				if(row==-1){ row=getRowCount()-1; }
    				changeSelection(row,col,false,false);
    				textArea.setText((String)getValueAt(row,col));
    				System.out.println("shift + tab");
    			}
    		});
    		textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,InputEvent.CTRL_DOWN_MASK),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)//tab + ctrl
    			{
    				int row=getSelectedRow();
    				int col=getSelectedColumn();
    				col++;
    				if(col==getColumnCount()){ col=0; row++; }
    				if(row==getRowCount()){ row=0; }
    				changeSelection(row,col,false,false);
    				textArea.setText((String)getValueAt(row,col));
    				System.out.println("ctrl + tab");
    			}
    		});
    		textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,InputEvent.SHIFT_DOWN_MASK),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)//enter + shift
    			{
    				int row=getSelectedRow();
    				int col=getSelectedColumn();
    				row--;
    				if(row==-1){ row=getRowCount()-1; col--; }
    				if(col==-1){ col=getColumnCount()-1; }
    				changeSelection(row,col,false,false);
    				textArea.setText((String)getValueAt(row,col));
    				System.out.println("shift + enter");
    			}
    		});
    		textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,InputEvent.CTRL_MASK),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)//enter + ctrl
    			{
    				JTextArea target=(JTextArea)e.getSource();
    				target.insert("\n",target.getCaretPosition());
    			}
    		});
    		textArea.addKeyListener(new KeyAdapter()
    		{
    			public void keyTyped(KeyEvent e){}
    			public void keyPressed(KeyEvent e){}
    			public void keyReleased(KeyEvent e)
    			{
    
    				int row=getSelectedRow();
    				int col=getSelectedColumn();
    				textField[0].setText(String.valueOf(row+1));
    				textField[1].setText(String.valueOf(col+1));
    				setValueAt(textArea.getText(),row,col);
    			}
    		});
    		textArea.addInputMethodListener(new InputMethodListener()
    		{
    			@Override
    			public void inputMethodTextChanged(InputMethodEvent event)
    			{
    				flg=(event.getText()==null)? true: false;
    			}
    
    			@Override
    			public void caretPositionChanged(InputMethodEvent event){}
    		});
    	}
    	public void setTextField(JTextField[] f)
    	{
    		textField=f;
    	}
    	@Override
    	public Component prepareRenderer(TableCellRenderer tcr,int row,int column)
    	{
    		Component cmp=super.prepareRenderer(tcr,row,column);
    		if(isCellSelected(row,column))
    		{
    			cmp.setForeground(getSelectionForeground());
    			cmp.setBackground(getSelectionBackground());
    		}
    		else
    		{
    			cmp.setForeground(getForeground());
    			cmp.setBackground(getBackground());
    		}
    		return cmp;
    	}
    }
    
    class TextAreaCellEditor extends JTextArea implements TableCellEditor
    {
    	private final JScrollPane scroll;
    	public TextAreaCellEditor()
    	{
    		scroll=new JScrollPane(this);
    		setLineWrap(true);
    		getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)
    			{
    				stopCellEditing();
    			}
    		});
    		getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,InputEvent.CTRL_MASK),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)
    			{
    				insert("\n",getCaretPosition());
    			}
    		});
    		addKeyListener(new KeyAdapter()
    		{
    			public void keyTyped(KeyEvent e){}
    			public void keyPressed(KeyEvent e){}
    			public void keyReleased(KeyEvent e){}
    		});
    	}
    	@Override
    	public Object getCellEditorValue()
    	{
    		return getText();
    	}
    	@Override
    	public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int column)
    	{
    		setFont(table.getFont());
    		setText((value!=null)?value.toString():"");
    		EventQueue.invokeLater(new Runnable()
    		{
    			@Override
    			public void run()
    			{
    				setCaretPosition(getText().length());
    				requestFocusInWindow();
    			}
    		});
    		return scroll;
    	}
    	@Override
    	public boolean isCellEditable(final EventObject e)
    	{
    		return false;
    	}
    	transient protected ChangeEvent changeEvent=null;
    	@Override
    	public boolean shouldSelectCell(EventObject e)
    	{
    		return true;
    	}
    	@Override
    	public boolean stopCellEditing()
    	{
    		fireEditingStopped();
    		return true;
    	}
    	@Override
    	public void cancelCellEditing()
    	{
    		fireEditingCanceled();
    	}
    	@Override
    	public void addCellEditorListener(CellEditorListener l)
    	{
    		listenerList.add(CellEditorListener.class,l);
    	}
    	@Override
    	public void removeCellEditorListener(CellEditorListener l)
    	{
    		listenerList.remove(CellEditorListener.class,l);
    	}
    	protected void fireEditingStopped()
    	{
    		Object[] listeners=listenerList.getListenerList();
    		for(int i=listeners.length-2;i>=0;i-=2)
    		{
    			if(listeners[i]==CellEditorListener.class)
    			{
    				if(changeEvent == null){ changeEvent=new ChangeEvent(this); }
    				((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);
    			}
    		}
    	}
    	protected void fireEditingCanceled()
    	{
    		Object[] listeners=listenerList.getListenerList();
    		for(int i=listeners.length-2;i>=0;i-=2)
    		{
    			if(listeners[i]==CellEditorListener.class)
    			{
    				if(changeEvent == null){ changeEvent=new ChangeEvent(this); }
    				((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
    			}
    		}
    	}
    }
    

    2013/02/25 [00:30]
    • category : java,
    • comment : 0

    Java JTableクラス、複数行セル( JTextArea )の実装(1)[ キーアクション制御 ]

    JTableクラス、複数行セル( JTextArea )の実装(2)[ キーアクション制御 ]

    参考にさせて頂いたサイト →  TableCellEditor をスクロール可能にする
    上記サイトのサンプルは一つのサンプル中に色々な要素がてんこもり。
    プログラムを読んで、実行して、クラスの作り方、メソッドの動作確認等ができるので勉強になります。

    上記のサンプルを参考にさせて頂いて、JTableへ複数行セルの実装と、キーアクション制御テスト。
    望む動作は、テキストエリア内でデータを編集、編集データを選択セルへ出力。
    選択セルの値をテキストエリアへ出力。
    テキストエリア内、 ctrl + enter キーで改行の挿入。
    enter, shift + enter, shift + tab, ctrl + tab キーで の選択セルの移動。
    マウス使用無しで、スムーズな編集ができるようにフォーカスイベントの処理。

    ハマった場所は、 ctrl + tab キーの処理。
    初期設定でキーが割り当ててあるのかな?
    フォーカストラバーサルキーなるものの無効化と tab キー を無効化しないと、うまく動作してくれなかった。

    
    setFocusTraversalKeysEnabled(false);//トラバーサルキー?の無効化
    KeyStroke tab=KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0);//tab キーの無効化
    textArea.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(tab,"none");
    

    んん??テキストエリアにフォーカスを当てたままの状態で選択セル移動、選択セルの内容変更が出来るのかな!?
    もう少しやってみよう。(;´Д`)

    ■ 実行結果 ( テキストエリアの値はセルの値 )

    JTableへの複数行セル実装

    ■ alt + tab キー実行結果 (初見)

    alt + tab キー実行結果

    ■ FocusTest.java

    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.InputEvent;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.EventObject;
    import javax.swing.AbstractAction;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.KeyStroke;
    import javax.swing.ListSelectionModel;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.border.LineBorder;
    import javax.swing.event.CellEditorListener;
    import javax.swing.event.ChangeEvent;
    import javax.swing.table.TableCellEditor;
    import javax.swing.table.TableCellRenderer;
    
    class FocusTest extends JFrame
    {
    	UndoRedoTable undoTable;
    	static JTextArea textArea;
    	FocusTest()
    	{
    		setTitle("Focus Test");
    		setLookAndFeel();
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		String[] header={"A","B","C"};
    		String[][] tbody=
    		{
    			{"a-0","b-0","c-0"},
    			{"a-1","b-1","c-1"},
    			{"a-2","b-2","c-2"},
    			{"a-3","b-3","c-3"}
    		};
    		getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.PAGE_AXIS));
    		undoTable=new UndoRedoTable(tbody,header);
    		JScrollPane scrollPane=new JScrollPane(undoTable);
    		add(scrollPane);
    		textArea=new JTextArea(50,100);
    		textArea.setBorder(new LineBorder(Color.white,8,false));
    		textArea.setText((String)undoTable.getValueAt(0,0));
    		undoTable.setTextArea(textArea);
    		JPanel menu=new JPanel();
    		JLabel label=new JLabel("行:");
    		Font font=new Font("Meiryo",Font.PLAIN,12);
    		label.setFont(font);
    		menu.add(label);
    		JTextField[] textField=new JTextField[2];
    		textField[0]=new JTextField("0",10);
    		textField[0].setHorizontalAlignment(JTextField.RIGHT);
    		textField[0].setEditable(false);
    		menu.add(textField[0]);
    		menu.add(Box.createRigidArea(new Dimension(30,1)));
    		label=new JLabel("列:");
    		label.setFont(font);
    		menu.add(label);
    		textField[1]=new JTextField("0",10);
    		textField[1].setHorizontalAlignment(JTextField.RIGHT);
    		textField[1].setEditable(false);
    		menu.add(textField[1]);
    		undoTable.setTextField(textField);
    		add(menu);
    		scrollPane=new JScrollPane(textArea);
    		add(scrollPane);
    		Dimension dimension=Toolkit.getDefaultToolkit().getScreenSize();
    		int width=(int)dimension.getWidth();
    		int height=(int)dimension.getHeight();
    		setBounds((width-500)/2,(height-350)/2,500,350);
    
    	}
    	public static void main(String[] args)
    	{
    		new FocusTest().setVisible(true);
    		textArea.requestFocus();
    		String str=textArea.getText();
    		textArea.setCaretPosition(str.length());
    	}
    	private void setLookAndFeel()
    	{
    		try
    		{
    			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    			SwingUtilities.updateComponentTreeUI(this);
    		}
    		catch(Exception ex){}
    
    	}
    }
    
    class UndoRedoTable extends JTable
    {
    	private JTextArea textArea;
    	private JTextField[] textField;
    	UndoRedoTable(String[][] tbody,String[] header)
    	{
    		super(tbody,header);
    		getTableHeader().setReorderingAllowed(false);
    		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		setDefaultEditor(Object.class,null);
    		setColumnSelectionAllowed(true);
    		setRowSelectionAllowed(true);
    		//setSurrendersFocusOnKeystroke(true);
    		//setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    		//setRowHeight(getRowHeight()*2);
    		for(int i=0;i<getColumnCount();i++)
    		{
    			getColumn(getColumnName(i)).setCellEditor(new TextAreaCellEditor(this));
    		}
    		setSelectionBackground(Color.red);
    		setFocusTraversalKeysEnabled(false);//フォーカストラバーサルキーの無効化
    		KeyStroke tab=KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0);//tabキーの無効化
    		getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(tab,"none");
    		changeSelection(0,0,false,false);
    		addMouseListener(new MouseAdapter()
    		{
    			public void mouseReleased(MouseEvent e)
    			{
    				UndoRedoTable tb=(UndoRedoTable)(e.getSource());
    				int row=tb.getSelectedRow();
    				int col=tb.getSelectedColumn();
    				textField[0].setText(String.valueOf(row));
    				textField[1].setText(String.valueOf(col));
    				textArea.setText((String)tb.getValueAt(row,col));
    				textArea.requestFocus();
    			}
    		});
    		addKeyListener(new KeyAdapter()
    		{
    			public void Typed(KeyEvent e){}
    			public void keyPressed(KeyEvent e)
    			{
    				int keycode=e.getKeyCode();
    				int mod=e.getModifiersEx();
    				if((mod & InputEvent.SHIFT_DOWN_MASK)!=0&&keycode==KeyEvent.VK_ENTER)
    				{
    					System.out.println("shift + enter");
    				}
    				else if((mod & InputEvent.SHIFT_DOWN_MASK)!=0&&keycode==KeyEvent.VK_TAB)
    				{
    					System.out.println("shitf + tab");
    				}
    				else if((mod & InputEvent.CTRL_DOWN_MASK)!=0&&keycode==KeyEvent.VK_ENTER)
    				{
    					System.out.println("ctrl + enter");
    				}
    				else if((mod & KeyEvent.CTRL_DOWN_MASK)!=0&&keycode==KeyEvent.VK_TAB)
    				{
    					int row=getSelectedRow();
    					int col=getSelectedColumn();
    					col++;
    					if(col==getColumnCount()){ col=0; row++; }
    					if(row==getRowCount()){ row=0; }
    					changeSelection(row,col,false,false);
    					textArea.setText((String)getValueAt(row,col));
    					System.out.println("ctrl + tab");
    				}
    				else if((keycode==KeyEvent.VK_ENTER))
    				{
    					System.out.println("enter");
    				}
    				else
    				{
    					textArea.requestFocus();
    					System.out.println("other");
    				}
    			}
    			public void keyReleased(KeyEvent e)
    			{
    				int keycode=e.getKeyCode();
    				int mod=e.getModifiersEx();
    				UndoRedoTable tb=(UndoRedoTable)(e.getSource());
    				int row=tb.getSelectedRow();
    				int col=tb.getSelectedColumn();
    				textField[0].setText(String.valueOf(row));
    				textField[1].setText(String.valueOf(col));
    				textArea.setText((String)tb.getValueAt(row,col));
    				if((mod & InputEvent.SHIFT_DOWN_MASK)!=0&&keycode==KeyEvent.VK_ENTER)
    				{
    					System.out.println("shift + enter");
    				}
    				else if((mod & InputEvent.SHIFT_DOWN_MASK)!=0&&keycode==KeyEvent.VK_TAB)
    				{
    					System.out.println("shitf + tab");
    				}
    				else if((mod & InputEvent.CTRL_DOWN_MASK)!=0&&keycode==KeyEvent.VK_ENTER)
    				{
    					System.out.println("ctrl + enter");
    				}
    				else if((mod & KeyEvent.CTRL_DOWN_MASK)!=0&&keycode==KeyEvent.VK_TAB)
    				{
    					System.out.println("ctrl + tab");
    				}
    				else if((keycode==KeyEvent.VK_ENTER))
    				{
    					System.out.println("enter");
    					textArea.requestFocus();
    				}
    				else
    				{
    					textArea.requestFocus();
    					System.out.println("other");
    				}
    			}
    		});
    
    	}
    	void setTextArea(JTextArea text)
    	{
    		textArea=text;
    		textArea.setFocusTraversalKeysEnabled(false);
    		KeyStroke tab=KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0);
    		textArea.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(tab,"none");
    		textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)//enter
    			{
    				int row=getSelectedRow();
    				int col=getSelectedColumn();
    				row++;
    				if(row==getRowCount()){ row=0; col++; }
    				if(col==getColumnCount()){ col=0; }
    				changeSelection(row,col,false,false);
    				requestFocus();
    			}
    		});
    		textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,InputEvent.SHIFT_DOWN_MASK),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)//tab + shift
    			{
    				int row=getSelectedRow();
    				int col=getSelectedColumn();
    				col--;
    				if(col==-1){ col=getColumnCount()-1; row--; }
    				if(row==-1){ row=getRowCount()-1; }
    				changeSelection(row,col,false,false);
    				requestFocus();
    			}
    		});
    		textArea.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,InputEvent.CTRL_DOWN_MASK),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)//tab + ctrl
    			{
    				int row=getSelectedRow();
    				int col=getSelectedColumn();
    				col++;
    				if(col==getColumnCount()){ col=0; row++; }
    				if(row==getRowCount()){ row=0; }
    				changeSelection(row,col,false,false);
    				textArea.setText((String)getValueAt(row,col));
    				requestFocus();
    			}
    		});
    		textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,InputEvent.SHIFT_DOWN_MASK),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)//enter + shift
    			{
    				int row=getSelectedRow();
    				int col=getSelectedColumn();
    				row--;
    				if(row==-1){ row=getRowCount()-1; col--; }
    				if(col==-1){ col=getColumnCount()-1; }
    				changeSelection(row,col,false,false);
    				requestFocus();
    			}
    		});
    		textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,InputEvent.CTRL_MASK),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)//enter + ctrl
    			{
    				JTextArea target=(JTextArea)e.getSource();
    				target.insert("\n",target.getCaretPosition());
    			}
    		});
    
    		textArea.addKeyListener(new KeyAdapter()
    		{
    			public void keyTyped(KeyEvent e){}
    			public void keyPressed(KeyEvent e)
    			{
    				int mod=e.getModifiersEx();
    				if(((mod & InputEvent.CTRL_DOWN_MASK)!=0)&&e.getKeyCode()==KeyEvent.VK_TAB){}
    			}
    			public void keyReleased(KeyEvent e)
    			{
    				int row=getSelectedRow();
    				int col=getSelectedColumn();
    				setValueAt(getTextArea().getText(),row,col);
    			}
    		});
    	}
    	public JTextArea getTextArea()
    	{
    		return textArea;
    	}
    
    	public void setTextField(JTextField[] f)
    	{
    		textField=f;
    	}
    	public JTextField[] getTextField()
    	{
    		return textField;
    	}
    	@Override
    	public Component prepareRenderer(TableCellRenderer tcr,int row,int column)
    	{
    		Component cmp=super.prepareRenderer(tcr,row,column);
    		if(isCellSelected(row,column))
    		{
    			cmp.setForeground(getSelectionForeground());
    			cmp.setBackground(getSelectionBackground());
    		}
    		else
    		{
    			cmp.setForeground(getForeground());
    			cmp.setBackground(getBackground());
    		}
    		return cmp;
    	}
    }
    
    class TextAreaCellEditor extends JTextArea implements TableCellEditor
    {
    	private final JScrollPane scroll;
    	public TextAreaCellEditor(final UndoRedoTable table)
    	{
    		scroll=new JScrollPane(this);
    		setLineWrap(true);
    		getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)
    			{
    				stopCellEditing();
    			}
    		});
    		getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,InputEvent.CTRL_MASK),new AbstractAction()
    		{
    			@Override
    			public void actionPerformed(ActionEvent e)
    			{
    				JTextArea target=(JTextArea)e.getSource();
    				target.insert("\n",target.getCaretPosition());
    			}
    		});
    		addKeyListener(new KeyAdapter()
    		{
    			public void keyTyped(KeyEvent e){}
    			public void keyPressed(KeyEvent e){}
    			public void keyReleased(KeyEvent e){}
    		});
    	}
    	@Override
    	public Object getCellEditorValue()
    	{
    		return getText();
    	}
    	@Override
    	public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int column)
    	{
    		setFont(table.getFont());
    		setText((value!=null)?value.toString():"");
    		EventQueue.invokeLater(new Runnable()
    		{
    			@Override
    			public void run()
    			{
    				setCaretPosition(getText().length());
    				requestFocusInWindow();
    			}
    		});
    		return scroll;
    	}
    	@Override
    	public boolean isCellEditable(final EventObject e)
    	{
    		return false;
    	}
    
    	transient protected ChangeEvent changeEvent=null;
    	@Override
    	public boolean shouldSelectCell(EventObject e)
    	{
    		return true;
    	}
    	@Override
    	public boolean stopCellEditing()
    	{
    		fireEditingStopped();
    		return true;
    	}
    	@Override
    	public void cancelCellEditing()
    	{
    		fireEditingCanceled();
    	}
    	@Override
    	public void addCellEditorListener(CellEditorListener l)
    	{
    		listenerList.add(CellEditorListener.class,l);
    	}
    	@Override
    	public void removeCellEditorListener(CellEditorListener l)
    	{
    		listenerList.remove(CellEditorListener.class,l);
    	}
    
    	protected void fireEditingStopped()
    	{
    		Object[] listeners=listenerList.getListenerList();
    		for(int i=listeners.length-2;i>=0;i-=2)
    		{
    			if(listeners[i]==CellEditorListener.class)
    			{
    				if(changeEvent == null){ changeEvent=new ChangeEvent(this); }
    				((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);
    			}
    		}
    	}
    	protected void fireEditingCanceled()
    	{
    		Object[] listeners=listenerList.getListenerList();
    		for(int i=listeners.length-2;i>=0;i-=2)
    		{
    			if(listeners[i]==CellEditorListener.class)
    			{
    				if(changeEvent == null){ changeEvent=new ChangeEvent(this); }
    				((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
    			}
    		}
    	}
    }
    

    2013/02/21 [02:41]
    • category : java,
    • comment : 0

    カウンター

    プロフィール

    negotoy

    Author:negotoy
    好きな選手権 :chara選手権
    好きなコーナー :リスナーが凸して
    椎名林檎の丸の内を歌うコーナー

    PC ゲーム歴 :
    モバゲーのタイピングゲームで
    タイピングの練習(サービス終了)
    ニックネーム : クッキー
    以降ゲーム歴なし

    [ YouTube ]

    chara : あいのうた
    椎名林檎 : ありあまる富
    かせきさいだぁ : じゃっ 夏なんで
    阿部真央 : 貴方の恋人になりたいのです

    最新記事

    カテゴリ

    カレンダー

    01 | 2013/02 | 03
    - - - - - 1 2
    3 4 5 6 7 8 9
    10 11 12 13 14 15 16
    17 18 19 20 21 22 23
    24 25 26 27 28 - -


    PAGE TOP
    広告: