Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Getting checkbox state from a table(Getting checkbox state from a table)
Getting checkbox state from a table [message #636135] Fri, 29 October 2010 12:58 Go to next message
JavaGalaxy  is currently offline JavaGalaxy Friend
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 #636141 is a reply to message #636135] Fri, 29 October 2010 13:05 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 29.10.2010 14:58, JavaGalaxy wrote:
> 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?

I assume you mean "checked" instead, of "selected", right? I suggest
that you query all items (#getItems()) and construct a new TableItem[]
that contains only those where #getChecked() returns true. In fact,
the jface component CheckboxTableViewer does so similarly.

HTH & Greetings from Bremen,

Daniel Krügler
Re: Getting checkbox state from a table [message #636151 is a reply to message #636135] Fri, 29 October 2010 13:36 Go to previous messageGo to next message
JavaGalaxy  is currently offline JavaGalaxy Friend
Messages: 6
Registered: November 2009
Junior Member
Hi,

Thanks for your response. Yes, I meant checked checkbox.

I did the same but item.getChecked() returns false for me. Any ideas please?

Thanks
Re: Getting checkbox state from a table [message #636427 is a reply to message #636151] Mon, 01 November 2010 09:00 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 29.10.2010 15:36, JavaGalaxy wrote:
> Hi,
>
> Thanks for your response. Yes, I meant checked checkbox.
>
> I did the same but item.getChecked() returns false for me. Any ideas
> please?

Please send us an SWT-like snippet that shows then problem.

- Daniel
Re: Getting checkbox state from a table [message #638460 is a reply to message #636427] Thu, 11 November 2010 11:44 Go to previous messageGo to next message
JavaGalaxy  is currently offline JavaGalaxy Friend
Messages: 6
Registered: November 2009
Junior Member
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.

Any help please?
Re: Getting checkbox state from a table [message #638737 is a reply to message #638460] Fri, 12 November 2010 12:02 Go to previous message
Daniel Krügler is currently offline Daniel KrüglerFriend
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();
}

}
Previous Topic:TableEditor display performance
Next Topic:[Tree] unwanted automatic SelectionEvents
Goto Forum:
  


Current Time: Sat Dec 16 07:33:11 GMT 2017

Powered by FUDForum. Page generated in 0.01957 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software