본문 바로가기

JAVA/SWT/JFACE

TableEditor를 이용한 Table 에디터 구성

TableEditor 란?

테이블 과 에디터 (Text, ComboBox, Checkbox ..) 등의 에디터컨트롤을 연결 시켜 주는 기능을 수행하는 것으로 다음의 함수로 테이블에 설정할 에디터와 TableItem(row) 그리고 컬럼번호를 넘겨 테이블의 TableItem 의 컬럼에 Editor 를 설정하게한다. 

public void setEditor(Control editor,

                      TableItem item,

                      int column) 


전시 구조는 TableEditor가 TableItem표면을 덮어 표시되는 형태로 되어 있다. 아래의 그림에서 TableEditor가 설정되지 않은 첫번째 checkbox 컬럼을 선택하면 TableItem이 선택 처리되는데 TableEditor가 설정된 두번재 컬럼 이후로는 선택 처리가 잘리듯 보이지 않게되는 것을 확인할 수 있다.


"+" 버트으로 데이터를 추가할 때 다음과 같은 코드로 테이블 에디터를 구현할 수 있다.
btnAddRow.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				TableItem item = new TableItem(table, SWT.NONE);
				
				int nRowCnt = table.getItemCount();
							
				TableEditorInfo editorInfo = new TableEditorInfo(table, nRowCnt);
				
				Text text = new Text(table, SWT.NONE);
				editorInfo.setColtrol(text, 1, item);
				text.addFocusListener(textFocusListener);
				
				text = new Text(table, SWT.NONE);
				editorInfo.setColtrol(text, 2, item);
				text.addFocusListener(textFocusListener);
				
				text = new Text(table, SWT.NONE);
				editorInfo.setColtrol(text, 3, item);	
				text.addFocusListener(textFocusListener);
				
				CCombo combo = new CCombo(table, SWT.READ_ONLY);
				combo.add("true");
				combo.add("false");
				combo.select(1);
				editorInfo.setColtrol(combo, 4, item);		
				
				listRow.add(editorInfo);
			}
		});


"-" 버튼으로 데이터를 삭제할 때에는 다음과 같은 문제가 발생 하는데

이는 두번째 TableItem 이 삭제 될때 세번째 TableItem과 연결된 TableEditor가 삭제된 두번째 TableItem의 자리로 이동되지 않기 때문이다.

이를 방지 하기 위해서는 TableItem 삭제 코드 아래에 다음의 코드를 추가하여 TableEditor를 Refresh해줘야 한다.

table.pack();

table.getParent().layout(false);


TableEditorSample.java