Getting checkbox state from a table [message #636135] |
Fri, 29 October 2010 12:58 |
JavaGalaxy Messages: 6 Registered: November 2009 |
Junior Member |
|
|
Hi,
I have a table with 3 columns, the first column contains checkbox. Within a given table I would be multiple such rows.
On clicking of a button, I am trying to get the selected rows in the table. Can anyone help me this please?
Below is my code snippet.
Thanks
TableItem tableItem = new TableItem(table, SWT.NONE);
TableEditor editor = new TableEditor (table);
Button checkButton = new Button(table, SWT.CHECK);
checkButton.pack();
editor.minimumWidth = checkButton.getSize().x;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor(checkButton, tableItem, 0);
editor = new TableEditor(table);
tableItem.setText(1, "Name");
tableItem.setText(2, "State");
checkButton.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
table.showSelection();
}
});
..................
...............
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
TableItem[] items = table.getItems();
int length = items.length;
for(int i=0; i < length; i++) {
TableItem item = items[i];
System.out.println("Checked::"+item.getChecked());
System.out.println("Text:"+item.getText());
System.out.println("Display:"+item.getDisplay());
}
}
});
|
|
|
|
|
|
|
Re: Getting checkbox state from a table [message #638737 is a reply to message #638460] |
Fri, 12 November 2010 12:02 |
Daniel Krügler Messages: 853 Registered: July 2009 |
Senior Member |
|
|
On 11.11.2010 12:44, JavaGalaxy wrote:
> Sorry for late reply. I did not get what you meant by SWT-like snippet
>
> The code is similar to what is in my original post
>
>
> TableItem tableItem = new TableItem(table, SWT.NONE);
> TableEditor editor = new TableEditor (table);
> Button checkButton = new Button(table, SWT.CHECK);
> checkButton.pack();
> editor.minimumWidth = checkButton.getSize().x;
> editor.horizontalAlignment = SWT.CENTER;
> editor.setEditor(checkButton, tableItem, 0);
> editor = new TableEditor(table);
> tableItem.setText(1, "Name");
> tableItem.setText(2, "State");
> checkButton.addFocusListener(new FocusAdapter() {
> public void focusGained(FocusEvent e) {
> table.showSelection();
> }
> });
>
>
> The table would have 3 or 4 items (rows) and I wanted to identify which
> item in the table was selected by the user.
>
> if I addSelectionListener to the checkbox button, I get the state but
> again I dont know which checkbox button is selected, like the index or
> some name to identify it.
Above is no complete snippet to argue about. It is very easy to get such
snippet from the SWT snippets home page:
http://www.eclipse.org/swt/snippets/
Here I copied Snippet114 linked from that page and modified it a bit to
demonstrate how to get the index of an item via tree.indexOf(), see
below. Similar examples are possible for the Table widget. If you have a
problem, you should not unnecessarily impose the burden to understand
your problem to those who are willing to help.
HTH & Greetings from Bremen,
Daniel Krügler
public class Snippet114 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
final Tree tree = new Tree(shell, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL
| SWT.H_SCROLL);
for (int i = 0; i < 12; i++) {
TreeItem item = new TreeItem(tree, SWT.NONE);
item.setText("Item " + i);
}
tree.setSize(100, 100);
tree.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
String string = event.detail == SWT.CHECK ? "Checked"
: "Selected";
System.out.println(event.item + " " + string);
TreeItem ti = (TreeItem) event.item;
System.out.format("Index: %s\n", tree.indexOf(ti));
}
});
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.04731 seconds