How to get the table whose cell is currently being clicked? - java

I've two tables in panels. When I click on first table on some cell, its row is getting selected. And when I click on the second table on some cell, its row is also getting selected.
Now, How will I come to know, which table is last clicked. I tried with isRowSelected on both the tables, both are returning, so I'm not able to find the last clicked table?
Can somebody help me?

you have to read how
JTable
ListSelectionListener
works,
you have to understand both concepts, simple example here, another here

I don't know if this must be determined from a MouseListener or from a ListSelectionListener, but the simplest solution is similar: use a different listener for each table:
table1.addXxxListener(new XxxListener() {
// here, you know it's table 1
}
table2.addXxxListener(new XxxListener() {
// here, you know it's table 2
}

Another way to do it is to check the source of the event:
new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if (e.getSource()==table1.getSelectionModel()) {
// Event comes from table1
} else if (e.getSource()==table2.getSelectionModel()) {
// Event comes from table2
}
}
}
Of course this is true if and only if the selection model is used by a single table (which is the case if you have not set your own ListSelectionModel)

Related

Wicket: highte all the rows in a dataview

I implemented the function of highlite here
It work fine, but now I need to highlite all the rows.
I did that:
private void seleccionarTodos(AjaxRequestTarget target) {
Iterator<Component> it = datosListView.iterator();
while (it.hasNext()){
HighlitableDataItem<Historial> item = (HighlitableDataItem<Historial> )it.next();
if(!item.highlite){
item.toggleHighlite();
target.add(item);
}
}
inicializarSelected(true);
container.add(datosListView);
target.add(form);
}
It doesn't throw any errors but no highlite any row, if there was some, they came swich off.
I though that mayby this actions can't be actioned from a button event, but i put the same code in the action of the event that hilight one row and it doesnt work. Some one has implemented the functionality of highlite all the rows?
Thanks
I solved my problem. The mistake was add:
container.add(datosListView);
target.add(form);
Should add only the item!

Receiving incorrect values from a JTable

I am trying to retrieve and use values from a JTable after a row has been selected using this code:
#Override
public void valueChanged(ListSelectionEvent e){
TableModel tableModel = this.mainFrame.getViewersTableModel();
this.mainFrame.setViewerButtonsEnabled(
!((Boolean)(tableModel.getValueAt(e.getLastIndex(), 1)))
);
}
In my scenario, the table has one row with a value of true, and another of false.
The very strange thing is that the first time I select a row, the value given is correct, whether it be the row holding true, or the one with false, but any subsequent selections result in getting true, no matter which row I pick.
Well, I fixed it.
Instead of getting the required boolean in my event handler class, I just made a method in the mainFrame class that retrieves the boolean from the table. The issue is now resolved.
Thank you to all who helped.

Null error when attempting to add custom row to CellTable in gwt

I have a Cell Table that I am using to output some search results. The cell table uses a list data provider to update info. I want to separate different sections so I am attempting to add a custom row in between different sections that has one cell that spans all of the columns. I am extending AbstractCellTableBuilder to do this, but my issue comes when I use TableRowBuilder and startRow(), calling startRow() returns a null pointer exception, to AbstractCellTableBuilder.java:243, which refers to tbody. So this is leading me to believe that my cell table is not getting passed into AbstractCellTableBuilder properly. My understanding of gwt and java is pretty basic, so I might just not be understanding how exactly this is supposed to work, and the showcase example is pretty complicated for me to understand. If anyone can tell where I'm messing up or has any simpler examples of this that might help me I would appreciate it!
I had found a similar answer and tried to implement it, and that is how I came up with what I have, but it answer wasn't quite detailed enough for me to fully understand how it works. Here is what I referenced:
Building a custom row on demand with GWT CellTableBuilder
EDITED:
Basic format of how I add normal rows to the cell table
searchProvider = new ListDataProvider<SearchColumn>();
cellTable_2 = new CellTable<SearchColumn>();
//Add columns to the cellTable
searchProvider.addDataDisplay(cellTable_2);
//What I call when adding a row to the cellTable using the ListDataProvider
searchProvider.getList().add(new SearchColumn("",label,"","","","","","",""));
Adding the CustomCellTableBuilder to the cell table:
//Passing the CustomCellTableBuilder to the cell table
CustomCellTableBuilder buildRow = new CustomCellTableBuilder();
cellTable_2.setTableBuilder(buildRow);
The CustomCellTableBuilder for adding custom rows:
public class CustomCellTableBuilder extends AbstractCellTableBuilder<SearchColumn>{
public CustomCellTableBuilder() {
super(cellTable_2);
}
#Override
protected void buildRowImpl(SearchColumn rowValue, int absRowIndex){
//building main rows logic
if (labelrow == 1){
System.out.println("Going to build extra row if");
buildExtraRow(absRowIndex, rowValue);
}
else {
System.out.println("Getting into normal buildRow");
buildRow(rowValue,absRowIndex);
}
}
private void buildExtraRow(int absRowIndex, SearchColumn rowValue){
start(true);
TableRowBuilder row = startRow();
TableCellBuilder td = row.startTD().colSpan(getColumns().size());
td.text("Testing this out").endTD();
row.endTR();
}}
I think you should call start(true) before calling startRow() because tbody is initialized to null. Start() call will initialize tbody to HtmlBuilderFactory.get().createTBodyBuilder().
The source doesn't lie.
Just like that:
private void buildExtraRow(int absRowIndex, SearchColumn rowValue) {
start(true); // true makes builder to rebuild all rows
TableRowBuilder row = startRow();
// whatever
}

How can I (un)hide a SWT TableItem?

I am trying to allow my user to search through a table of information, dynamically hiding/showing results that contain the search. I have the hiding part down, and it works well, but I'm having trouble showing the table item again once the search criteria is changed.
Here is my hide code:
searchField.addModifyListener(new ModifyListener() {
#Override
public void modifyText(ModifyEvent arg0) {
modified = true;
for (int i = 0; i < table.getItems().length; i++) {
if (!(table.getItem(i).getText(2)
.contains(searchField.getText()))) {
table.getItem(i).dispose();
}
}
if ("".equals(searchField.getText())) {
modified = false;
//where I would want to un-hide items
}
}
});
Looking at your code, it seems you try to hide the item by calling dispose(). If you dispose a widget, it is gone for good. You cannot get it back.
If you want to unhide it again, will have to create a new item at the position of the previously hidden one with the same content.
Isn't it better to actually operate with some kind of a table model and JFace bindings, rather, then do it like that? And yes, disposing is not hiding. You should probably remove the item from the table.
You have probably to save the data from TableItem into collection before you call dispose. Then when you search again you could check that collection and if matches are found, then insert back into Table by creating new TableItem.

problem when implementing a selection listener in a JTable

I am developing a JTable with different rows. I would like to associate an event to the selection of a row in this table. I have used the following selection class to provide behaviour to the table selection:
public class TableSelectionListener implements ListSelectionListener{
public Integer item;
public TableSelectionListener(Integer item){
this.dialog = item;
}
public void valueChanged(ListSelectionEvent e) {
System.out.println("The row clicked is "+item);
}
}
When I create an instance of this table, sai tabletest, I have added the following piece of code:
tabletest.getSelectionModel().addListSelectionListener(new TableSelectionListener(tabletest.getSelectedRow());
The problem is that when I click on one row once, instead of retrieving the related message once, I retrieve the same message several times, suggesting that the actions repeated several times. For example:
The row clicked is 0
The row clicked is 0
The row clicked is 0
The row clicked is 0
Does anyone know where the problem may be?
Well, that's just normal.
Your selection listener is created with the value of tabletest.getSelectedRow() at its creation table (which is zero). And, as you never change the value of item in your listener, this listener fcan only display 0as a result.
If I were you, I wouold replace the valueChanged() method by something like (although it's untested and I remember strange things happens sometimes when mixing view and model row values) this :
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()) // added as sometimes, multiple events are fired while selection changes
System.out.println("The row clicked is "+e.getFirstIndex());
}
Firstly, it's perfectly normal to get multiple ListSelectionEvents, while the selection is being changed. You can use the getValueIsAdjusting method to determine when selection has ended (it will return false).
Secondly, there's no need to construct your TableSelectionListener with a row number. When your valueChanged method is called, you can get the index of the first/last selected row (remember it's possibly to select multiple rows in the table, unless you disable that) using e.getFirstIndex() and e.getLastIndex() respectively.
An easier way of doing it, is as follows:
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
System.out.println("e...."+table.getSelectedRow());
}
});

Categories

Resources