get name of checkbox in gwt java - java

When I create a table from an array, I fill ithe first column with the values present in the array, with the second column being ckeckBoxes with the values of the index of the array. I would need to retrieve the name of the selected checkBox. Below is my code, could anyone help me please?
Thanks for your help!
public class AddDoodlePart3 extends Composite {
MainView main = new MainView();
FlexTable table= new FlexTable();
VerticalPanel ab = new VerticalPanel();
HorizontalPanel hor = new HorizontalPanel();
InlineLabel lb = new InlineLabel("tette");
CheckBox ck ;
TextBox orario = new TextBox();
Button btn = new Button("Inserisci");
int culo;
public AddDoodlePart3(String det, ArrayList<String> listDate){
initWidget(this.ab);
this.ab.add(lb);
System.out.println(det+listDate.size());
table.setText(0, 0, " ");
table.setText(0, 1, "Opzione");
table.setText(0, 2, " ");
System.out.println("1");
for(int i=0;i<listDate.size();i++){
System.out.println(i);
this.ck = new CheckBox(""+i);
table.setWidget(i, 0, ck);
table.setText(i, 1, listDate.get(i));
ck.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
boolean checked = ((CheckBox) event.getSource()).getValue();
Window.alert("It is " + (checked ? "" : "not ") + "checked "+ culo);
}
});
}
this.ab.add(table);
this.hor.add(orario);
this.hor.add(btn);
this.ab.add(hor);
btn.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// System.out.println(culo);
}
});
}
}

You should use a DataGrid widget for these purposes. See GWT Showcase for an example. Click on a Source Code link to see how to implement this DataGrid.
You can set a SelectionModel on a DataGrid - in your case you need a MultiSelectionModel. Then, getting the list of selected items is as easy as:
selectionModel.getSelectedSet();

Related

click handler on bottom vaadin

I need to add a click Handler for a column in a table, so if the table has 3 row I need to add a clickhandeler for every row;
But I need also to add a value (to allow me to distinguish between rows) to the click handeler;
In other words:
I have this clickHandler:
btnElimina.addClickListener(new Button.ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
System.out.println( "nell handler " );
}
});
and I should want some thing, like this:
btnElimina.addClickListener(new Button.ClickListener(String Val) {
#Override
public void buttonClick(ClickEvent event) {
System.out.println( "nell handler con "+ val );
} });
Is that possible? I've googled on it but didn't find a solution;
Also If I try to use the ClickEvent there seems nothing that I can use to distinguish between rows;
thanks for help
For Vaadin 7, you can find an example in the official documentation in the "Components Inside a Table" section. TLDR: you can use setData() / getData() from the Component to pass identifying information.
The relevant pieces are here:
Table table = new Table();
table.addStyleName("components-inside");
table.addContainerProperty("Details", Button.class, null);
/* Add a few items in the table. */
for (int i=0; i<100; i++) {
// The Table item identifier for the row.
Integer itemId = new Integer(i);
// Create a button and handle its click. A Button does not
// know the item it is contained in, so we have to store the
// item ID as user-defined data.
Button detailsField = new Button("show details");
detailsField.setData(itemId);
detailsField.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
// Get the item identifier from the user-defined data.
Integer iid = (Integer)event.getButton().getData();
Notification.show("Link " +
iid.intValue() + " clicked.");
}
});
// Create the table row.
table.addItem(new Object[] {detailsField},
itemId);
}
I resolved the issue in this way:
public class TableListe extends CustomComponent implements Button.ClickListener{
private String istituto;
public TableListe(String ista) {
//super();
this.istituto = ista;
}
#Override
public void buttonClick(ClickEvent event) {
System.out.println( " nell handler con this.istituto " + this.istituto);
}
}
It seems to work for now;

Check/Uncheck CheckboxCell, onclick of a row - GWT

I have a cell table with the first column as checkboxes. My checkboxes have to be checked or unchecked when there is any single click event on the entire row. This is the following code for creating a MultiSelectionModel, creating CheckboxCell and creating a column for cell table.
MultiSelectionModel<Object> selectionModel = new MultiSelectionModel<>();
table.setSelectionModel(selectionModel);
CheckboxCell selectedCell = new CheckboxCell();
Column<Object,Boolean> selectedCol = new Column<Object, Boolean>(selectedCell){
#Override
public Boolean getValue(Object object) {
return object.isSelected();
}
};
table.addColumn(selectedCol);
//Single Click Event to Enable/Disable checkbox.
table.addDomHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
Set<Object> selectedItems = selectionModel.getSelectedSet();
for (Object s : selectedItems) {
Window.alert(String.valueOf(s));
selectionModel.setSelected(s, true);
}
}
}, ClickEvent.getType());
I tried to mark a row as checked using "selectionModel.setSelected(s, true)". But it isn’t working, when I clicked on row, the corresponding checkbox is not being checked.
My question is how do I enable/disable checkboxes onclick of an entire row. Is my approach correct. Or Is there any other way to perform this action in GWT.
You are very close to the working solution.
In the selectedCell you should return the value depending on selectionModel:
return selectionModel.isSelected(object);
This way you are using default multi selection model that selects rows by clicking on them. And the checkbox value comes from the selection model. That's it.
See the working example below:
CellTable<String> table = new CellTable<String>();
final MultiSelectionModel<String> selectionModel = new MultiSelectionModel<>();
table.setSelectionModel(selectionModel);
CheckboxCell selectedCell = new CheckboxCell();
Column<String, Boolean> selectedCol = new Column<String, Boolean>(selectedCell) {
#Override
public Boolean getValue(String object) {
// return object.isSelected();
return selectionModel.isSelected(object);
}
};
table.addColumn(selectedCol);
table.addColumn(new TextColumn<String>() {
#Override
public String getValue(String object) {
return object;
}
});
List<String> values = new ArrayList<>();
for(int i = 0; i < 10; i++)
values.add("Line " + (i + 1));
table.setRowData(values);
You can use standard Ctrl and Shift keys to control selection.

Cannot display Dialog box after clicking the flex table cell in GWT

This this the class of my dialog box:
public class Question extends DialogBox {
public Question(int row, FlexTable flex) {
super (true);
setText( "Choose Action");
String outputString = flex.getWidget(row, 0).toString();
VerticalPanel panel = new VerticalPanel();
panel.add(new Label ("" + outputString));
panel.add(new Button( "edit"));
this.setWidget(panel);
}
}
And this is the way I create an instance of my box after clicking at some row in flex table, but the problem is that I get nothing after, no impact after clicking any row.
flex.addClickHandler(new ClickHandler() {
public void onClick (ClickEvent event){
int rowlndex = flex.getCellForEvent(event).getRowIndex();
// Window.alеrt("Row Clicked " + rowlndex); --- Works Properly
Question que = new Question(rowlndex, flex);
que.center();
que.show();
}
});

How to update table cells with new information using JavaFX?

Whenever I click a row, I want to update the table with new information. For some reason when I click it is not doing anything and errors out. Any idea why I can't do this?
public HierarchyItemTreeCellCallback(TreeView<Requirement> treeView, DataModel dataModelIn)
{
super();
treeViewParent = treeView;
dataModel = dataModelIn;
}
#Override
public TreeCell<Requirement> call(TreeView<Requirement> param)
{
TreeCell<Requirement> cell = new HierarchyItemTreeCell();
cell.addEventFilter(MouseEvent.MOUSE_PRESSED,new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent event)
{
HierarchyTabController hierarchyTabController = new HierarchyTabController(dataModel);
Requirement req1 = new Requirement("ID", "21", 1, REQUIREMENT_TYPE.SOFTWARE);
hierarchyTabController.updateTableRow(req1);
}
});
return cell;
}
Here is the controller logic:
public void updateTableRow(Requirement r)
{
REQUIREMENT_TYPE reqType = REQUIREMENT_TYPE.SOFTWARE;
Requirement req1 = new Requirement("ID", "21", 1, reqType);
ObservableList<Requirement> list = FXCollections.observableArrayList(req1);
name.setCellValueFactory(new PropertyValueFactory<>("name"));
description.setCellValueFactory(new PropertyValueFactory<>("description"));
tableView.getItems().setAll(list);
tableView.setItems(list);
}
This works whenever I do not call the updateTableRow function from the MouseEvent. However, it is not working when I try to call the controller and update the values from the MouseEvent. Any idea what I am doing wrong?

how to delete selected rows (multiple rows) from CheckboxTableViewer when button clicks? (table is connected to oracle database)

i hava a CheckboxTableViewer which has 10 columns, and the table is filled from database,
and i have a button outside the table named as "Delete",
what i want to do is:-
when i select rows using check box (multiple selection also) and when i press the "delete" button , i want the selected rows should get deleted from the database, and the tableviewer shuold get refreshed.
am pasting my tableviewer code below:-
final CheckboxTableViewer dataTable = CheckboxTableViewer.newCheckList(TableComposite2, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.BORDER |SWT.DM_FILL_BACKGROUND|SWT.FULL_SELECTION);
dataTable .getTable().setHeaderVisible(true);
dataTable .getTable().setLinesVisible(true);
dataTable .setContentProvider(new ArrayContentProvider());
//Action Check box
TableColumn columnCHead=new TableColumn(dataTable .getTable(),SWT.NONE);
columnCHead.setText("Delete");
columnCHead.setWidth(50);
// setting column input
TableViewerColumn columnC=new TableViewerColumn(dataTable ,columnCHead);
columnC.setLabelProvider(new ColumnLabelProvider()
{
public String getText(Object Element)
{
return null;
}
});
TableColumn columnFS1Head=new TableColumn(dataTable .getTable(),SWT.NONE);
columnFS1Head.setText("SOURCE DIRECTORY");
columnFS1Head.setWidth(300);
TableViewerColumn columnFS1=new TableViewerColumn(dataTable ,columnFS1Head);
columnFS1.setLabelProvider(new ColumnLabelProvider()
{
public String getText(Object Element)
{
AgedFileMaster a=(AgedFileMaster)Element;
return a.getDIRECTORY_PATH();
}
enter code here});
......
and i have a button for delete operation,(outside the table),
when i press delete button, i want the selected rows to get deleted...
am beginner to SWT.
anyone please help......
Use addSelectionListener on your Button control to be notified when the button is pressed:
button.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent event)
{
// TODO handle delete here
}
});
You need to do two things to remove the data - first update your data model to remove the objects and secondly tell the table viewer that the model has changed.
You can do something like this:
dataTable.getTable().setRedraw(false); // Stop redraw during update
IStructuredSelection selection = (IStructuredSelection)dataTable.getSelection();
for (Iterator<?> iterator = selection.iterator(); iterator.hasNext(); )
{
Object selectedObject = iterator.next();
// TODO remove from data model array
// Tell table view the object has been removed
dataTable.remove(selectedObject);
}
dataTable.getTable().setRedraw(true); // Allow updates to be drawn
An alternative to calling dataTable.remove on each object is to call dataTable.refresh once at the end. There is also a variant of remove which accepts an array of objects.
TableViewerColumn actionsNameCol = new TableViewerColumn(viewer, column);
actionsNameCol.setLabelProvider(new ColumnLabelProvider(){
//make sure you dispose these buttons when viewer input changes
Map<Object, Button> buttons = new HashMap<Object, Button>();
#Override
public void update(ViewerCell cell) {
TableItem item = (TableItem) cell.getItem();
Button button;
if(buttons.containsKey(cell.getElement()))
{
button = buttons.get(cell.getElement());
}
else
{
button = new Button((Composite) cell.getViewerRow().getControl(),SWT.NONE);
button.setText("Remove");
buttons.put(cell.getElement(), button);
}
TableEditor editor = new TableEditor(item.getParent());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(button , item, cell.getColumnIndex());
editor.layout();
}
});
Delete selected rows (multiple rows) from Table when button clicks (Database Connectivity)
//Java ArrayList class uses a dynamic array for storing the elements
List<String> id_list=new ArrayList<String>();
//Button Text:Selected Row Delete
Button btnNewButton = new Button(parent, SWT.NONE);
btnNewButton.setText("Selected Row Delete");
btnNewButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
TableItem[] item=table.getItems();
for(int i=0;i<table.getItemCount();i++)
{
if(item[i].getChecked()&&!item[i].getText(1).equals(""))
{
String id=item[i].getText(1);
id_list.add(id);//Add ID into List
}
}
for(int j=0;j<id_list.size();j++)
{
//class:Test
//Method:DeleteData(String ID) pass id to delete rows
// type cast object into string
Test.DeleteData((String) id_list.get(j));
}
}
});
btnNewButton.setImage(ResourceManager.getPluginImage("RCP_Demo", "icons/delete.png"));
btnNewButton.setBounds(18, 370, 68, 23);
Test.java
public class Test {
static Connection conn = null;
static PreparedStatement presta=null;
public static void DeleteData(String ID)
{
String url = "jdbc:sqlite:Demo.db";
try{
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(url);
presta = conn.prepareStatement("delete from Student where sid=?");
presta.setString(1, ID);
presta.executeUpdate();
DisplayData();
}catch(Exception e)
{
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}//End DeleteData()
}//End Test class

Categories

Resources