I have a flextable, and in that table I have a column where each row has a button to remove that row. How can I get the index of that row so that I can remove it. I only need to get a index.
A solution:
FlexTable myTable = new FlexTable();
myTable.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Cell cell = myTable.getCellForEvent(event);
int receiverRowIndex = cell.getRowIndex();
}
});
Related
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
I am using Vaadin 7.1.7.
I have a Table which has a few TextFields and a Button called "delete".
On click of the delete button, that particular row is to be deleted.
As i understand, I could remove table item as follows:
table.removeItem(itemID);
Unfortunately, I am unable to fetch the itemID of the row to remove it from the table.
Since, I used table.addItem(o, null); to addItems to it, how could I get the rowID/itemID on the click of the button inside buttonClickListener?
My trys so far have been:
#Override
public void buttonClick(ClickEvent event) {
Table t = (Table) event.getButton().getParent();
}
This has got me to the parent table but not to that particular item.
Thanks in advance
.
You could for example use setData(rowID) when you create the buttons.
The onClick you retrieve the associated data of the button and have the correct row id.
Provide a row id, override Button.ClickListener, and use the id in the click listener.
Object rowId = new Object();
Button button = new Button("Delete");
button.addClickListener(new RowDeleteListener(rowId));
//populate cells in the row, add the button & whatever
table.addItem(row, rowId);
public class RowDeleteListener implements Button.ClickListener {
Object rowId;
public RowDeleteListener(Object rowId) {
this.rowId = rowId;
}
public void buttonClick(ClickEvent event) {
table.removeItem(rowId);
}
}
Or André Schild’s solution, which is to use setData(rowId) on the button.
Button button = new Button("Delete");
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
table.removeItem(getData());
}
});
//Populate row stuff.
button.setData(table.addItem(row, null));
I like the first solution slightly better because it's more obvious what's going on, and also because the button has the correct row id before it gets added to the table instead of after.
Or, if you feel like creating something obnoxious: you could use the Button object as the id for the row.
Button button = new Button("Delete");
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
table.removeItem(this);
}
});
//populate row stuff, including adding the button to the row.
table.addItem(row, button);
I didn't test or compile any of these so... you know...
Can someone help me with this listener?
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
if(e.getValueIsAdjusting()){
ListSelectionModel model = table.getSelectionModel();
int lead = model.getLeadSelectionIndex();
displayRowValues(lead);
}
}
private void displayRowValues(int rowIndex){
String country = "";
Object oCountry = table.getValueAt(rowIndex, 0);
country += oCountry.toString();
countryTxt.setText(country );
}
});
It's supposed to send data from cell in jtable (table) into a textfield (countryTxt) when one of the row's is selected, but it works only when I click on row and not when I'm cycling trough my table with arrow key's.
The problem is with this line:
if (e.getValueIsAdjusting()) {
Replace this with:
if (e.getValueIsAdjusting()) return;
This is a check for multiple selection events BTW.
If you comment out e.getValueIsAdjusting() it works.
http://docs.oracle.com/javase/6/docs/api/javax/swing/ListSelectionModel.html#setValueIsAdjusting(boolean)
I added a mouse clicked listner to my jtable, when i double click the row, will pop up an window accordingly.
jTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
double amount = Double.parseDouble(jTable.getValueAt(getSelectedRow(), 4).toString());
String remarks = jTable.getValueAt(getSelectedRow(), 3).toString();
String transactionID = jTable.getValueAt(getSelectedRow(), 1).toString();
new EditFrame(...)
}
});
This code I used to retrieve the row selected row.
public int getSelectedRow() {
jTable.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
int viewRow = jTable.getSelectedRow();
selectedRow = viewRow;
System.out.println(viewRow);
}
});
return selectedRow;
}
In my case, I realised when I clicked the second row in the first time, I get null for selectedRow, only when I select first row then second row, I then can get the correct data. And If I removed the mouse listener the problem then be solved. Is it because I doing something wrong at the mouse click listener?
If you just want to know what row was clicked then you don't need the selection listener. Just use:
table.rowAtPoint();
You're doing it the wrong way. Remove your current getSelectedRow() method completely and never try to code something similar. Here is a better version:
jTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
int selectedRow = jTable.getSelectedRow();
double amount = Double.parseDouble(jTable.getValueAt(selectedRow, 4).toString());
String remarks = jTable.getValueAt(selectedRow, 3).toString();
String transactionID = jTable.getValueAt(selectedRow, 1).toString();
new EditFrame(...)
}
});
How can I find out which row in a JTable the user just clicked?
Try this:
aJTable.rowAtPoint(evt.getPoint());
If you only ever care about listening to selections on the JTable:
jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int sel = jTable.getSelectedRow();
}
});