Jump to content

Ajuda na detecção de coluna numa tabela SWT.


Recommended Posts

Posted

Estou com um problema que não consigo resolver... Pelo que investiguei, parece que não tem solução, mas mesmo assim estou a dar o benficio da dúvida...

Estou a fazer o desenvolvimento de uma aplicação desktop, acente em SWT, e quero ter uma tabela com duas colunas: chave e valor. O número de chaves é variável, ou seja, não existe um conjunto fixo de chaves e como tal tenho dois botões para efectuar essa gestão: adicionar e remover.

Para editar, nada melhor que o clique de rato ou o clássico F2 sobre o texto que se pretende alterar. E aqui é que as coisas se complicam.

Detectar a linha onde o utilizador se encontra é simples, já descobrir a coluna e colocá-la editável parece impossível...

Eis o método que tenho para fazer a edição:

/**

* Edit the selected cell value

* @param e is the selection event

*/

private void editCellValue(SelectionEvent e) {

// Clean up any previous editor control

Control oldEditor = pEditor.getEditor();

if (oldEditor != null) oldEditor.dispose();

// Identify the selected row

TableItem item = (TableItem)e.item;

if (item == null) return;

int column = 1;

// The control that will be the editor must be a child of the Table

Text newEditor = new Text(tProperties, SWT.NONE);

newEditor.setText(item.getText(column));

newEditor.addModifyListener(new ModifyListener() {

public void modifyText(ModifyEvent e) {

Text text = (Text)pEditor.getEditor();

pEditor.getItem().setText(1, text.getText());

}

});

newEditor.selectAll();

newEditor.setFocus();

pEditor.setEditor(newEditor, item, column);

}

Estão a ver a linha int column = 1;? Isso define qual a coluna que vai ser editada e tem de ser um valor constante, caso contrário pEditor.getItem().setText e pEditor.setEditor não funcionam...

Ora tenho assim dois problemas:

1. Descobrir qual a coluna em que o utilizador está posicionado.

2. Saber como é possível usar a informação do ponto acima em métodos que me permitam colocar a célula editável.

Se alguém tiver mais informações e me possa ajudar, agradeço.

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Posted
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;

public class Snippet3 {

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
final Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL
		| SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
final int rowCount = 64, columnCount = 4;
for (int i = 0; i < columnCount; i++) {
	TableColumn column = new TableColumn(table, SWT.NONE);
	column.setText("Column " + i);
}
for (int i = 0; i < rowCount; i++) {
	TableItem item = new TableItem(table, SWT.NONE);
	for (int j = 0; j < columnCount; j++) {
		item.setText(j, "Item " + i + "-" + j);
	}
}
for (int i = 0; i < columnCount; i++) {
	table.getColumn(i).pack();
}
Point size = table.computeSize(SWT.DEFAULT, 200);
table.setSize(size);
shell.pack();
table.addListener(SWT.MouseDown, new Listener() {
	public void handleEvent(Event event) {
		Point pt = new Point(event.x, event.y);
		TableItem item = table.getItem(pt);
		if (item == null)
			return;
		for (int i = 0; i < columnCount; i++) {
			Rectangle rect = item.getBounds(i);
			if (rect.contains(pt)) {
				int index = table.indexOf(item);
				System.out.println("Item " + index + "-" + i);
			}
		}
	}
});
shell.open();
while (!shell.isDisposed()) {
	if (!display.readAndDispatch())
		display.sleep();
}
display.dispose();
}

}

in: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet3.java?rev=HEAD&content-type=text/vnd.viewcvs-markup

Como podes ver pelo exemplo tens de la ir pelos pontos nao da pelas celulas.

Hope it helped

EDIT: Btw o tipo de evento gerado tb e importante tu precisas de apanhar o evento SWT.MouseDown e nao SWT.Selection,  so digo isto porque ja me aconteceu...

Posted

Vou dar uma olhadela nisso quando puder...

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Posted

Dabubble, obrigado pela ajuda, mas não era isso que pretendia.

Isso só me permite usar o rato e o uso do teclado é imprescindível...

Vou assumir que não dá e partir para outra solução menos bonita.

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.