Richfaces 3.3 extendedDataTable onRowClick is setting data from previous row click - java

I have this datatable with an a4j:support tag so taht when someone clicks on a row, i want to display or hide certain buttons based on the status of the row selected.
The problem is that the data in my backing bean is always one click behind.
<rich:extendedDataTable
id="formSummaryTableId"
var="dataSummary"
value="#{FormSearch.summaries}"
binding="#{FormBacking.table}"
selection="#{FormSearch.selection}"
rowKeyVar="rkv"
frozenColCount="0"
sortMode="single" height="500px" width="795px"
cellpadding="0" cellspacing="0" border="0" rowClasses="even, odd"
selectedClass="itemSelected">
<a4j:support event="onRowClick" actionListener="#{FormBacking.onClickForm}"
reRender="DesignerForm:formEditToolbar" />
....
FormBacking.onClickForm
public void onClickForm(ActionEvent e)
{
Iterator<Object> selectionKeys = getFormSearch().getSelection().getKeys();
LOG.info("selectionKeys = "+selectionKeys.hasNext());
if (selectionKeys.hasNext())
{
LOG.info("selectionKeys.next()");
Integer lookupKey = (Integer) selectionKeys.next();
LOG.info("lookupKey = "+lookupKey);
} else {
LOG.info("THERE is no keys");
}
}
EDIT
I've figured out that for some reason, when i click a row, it's not getting set correctly. It's like it's 1 step behind my clicks. For example if i click row 1 of my datatable, my method gets into "THERE is no keys", but if i click row 2 now, my method has the keys for the first row I clicked.
Why is it 1 click behind?

Use onselectionchange event:
<a4j:support event="onselectionchange" ...
I guess the problem with onRowClick is that it gets fired before selection change happen, so it submits previous selection value.

Related

jtable.setModel gives java.lang.ArrayIndexOutOfBoundsException: -1

I have a jbutton which loads data from a DB, and then populates a jtable (using a DefaultTableModel)
Then, I have this event on the row selection of the table:
jTableDettagliFattura.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent event) {
int selected= jTableDettagliFattura.getSelectedRow();
String id = jTableDettagliFattura.getModel().getValueAt(selected, 0).toString();
System.out.println(id);
}
});
When I load the table for the first time (using the button), everything works fine. But if I select one of the table rows, and then reload the table with the button, I get the "java.lang.ArrayIndexOutOfBoundsException: -1", at the command "jTableDettagliFattura.setModel(model);" (that was perfectly working the first time).
What could be the problem?
Is the selection event somehow "ruining" my model?
But if I select one of the table rows, and then reload the table with the button, I get the "java.lang.ArrayIndexOutOfBoundsException: -1"
There is no row selected when the model is reloaded. The listener probably fires an event to indicate the selection was removed.
Try:
int selected = jTableDettagliFattura.getSelectedRow();
if (selected == -1) return;
The main point is don't assume a row is selected. Validate the index before doing your processing.

Setting tab cursor position in TableView JavaFX

I have a javafx tableview and I'm trying to write a keylistener so the user can quickly select rows and edit their contents.
So far I can focus the table with a key press and select/focus a row with a digit key press. However I then want the user to be able to tab through the row to select the cell he wants to edit.
My problem is whenever I press the tab when a row is selected, it always starts selecting cells from the first row.
Here is the code from my key listener:
....
else if(code.isDigitKey()){
TableView tv = scene.getSelectedTable();
if(tv != null){
tv.getSelectionModel().select(code.ordinal()-24);
tv.getFocusModel().focus(code.ordinal()-24);
//Something required here to set tab position to start of this row
}else{
System.out.println("null");
}
}
Is there a way to set a tab starting position?
SOLUTION:
Tab simply goes to the next JavaFX control element that it knows about. The TableView had focus so when I pressed tab it went to the next control element which just so happened to be inside the table. Tab does not traverse through table cells.
So the work around I came to was to call the edit method on the first cell in the selected row:
....
else if(code.isDigitKey()){
TableView tv = scene.getSelectedTable();
if(tv != null){
tv.getSelectionModel().select(code.ordinal()-24);
tv.getFocusModel().focus(code.ordinal()-24);
tv.edit(code.ordinal()-24, ((TableColumn)(tv.getColumns().get(0))));
}else{
System.out.println("null");
}
}
Then in my CellFactory for that column (It happens to be a Spinner), I override startEdit:
#Override
public void startEdit(){
this.spinner.requestFocus();
}
Note the column and table must be editable.

Deselect selected row in Vaadin Grid

Can somebody give me a hint because I am stucked. And I haven't found the appropriate solution for my problem.
I have a Grid, with 1-3 rows. I click on the row -> the row is selected.
I want to have this row to be deselected after I click somewhere else (outside this row) but inside the grid.
Here is simple screenshot, to help you visialize it better.
What kind of listener should I use for this case? I have tried ItemClickListener -> haven't helped.
Try putting your grid in a separate layout and add LayoutClickListener to it:
gridLayout.addLayoutClickListener(new LayoutEvents.LayoutClickListener() {
#Override
public void layoutClick(LayoutEvents.LayoutClickEvent event) {
if(grid.getSelectedRow() != null) {
grid.deselectAll();
}
}
});
gridLayout.asSingleSelect().addSelectionListener(e->{
if(e.getFirstSelectedItem().isPresent()) {
system.out.println("selected");
}else {
// when deselected make some actions here
system.out.println("deSelected");
}
});
Selection:
If you click on the same row of data for the first time , it will print selected ,since there are values present on what you clicked .
Deselection:
Now try to click on the same selected row for the second time , now it will go to deselection mode. since there are no values when you deselected.Now no need to click anywhere on the grid ,you can click on the same row of data for two times for the selection and deselection.

how to clear the <select> tag before filling the another value through <option> tag

On clicking these radio button it just call servlet and fill the Dropdown list to its content.
but the problem is on clicking the 1st time it is filling as expected but if I click againg the radio button then it again call the servlet and fill the same thing again(for eg. in the dropdown list matrix and matrix two times).
Q1 : How do i prevent this to fill the same thing again ????
second thing is, after clicking the 1st radio button it will fill the dropdown let suppose "matrix" value and then if click the another radio button the it will fill the another value let suppose "jack" (as expected) but the problem is,
In the dropdown list both the value is contained "matrix" and "jack" but I just want if I click 1st radio button then it fill only "matrix" and again if I click 2nd radio button the it fill only "jack" the 1st radio button's value should be removed before filling the another radio button's value Q2: how to do this ??
jsp page:-
<b>Select Language :</b>
<input type="radio" onclick="callServlet();" id="lang1" name="lang" value="c">C
<input type="radio" onclick="callServlet();" id="lang2" name="lang" value="cpp">C++
<input type="radio" onclick="callServlet();" id="lang3" name="lang" value="java">Java
<b>Select Program :</b>
<select id="combo">
<option>-Select Program-</option>
</select>
Here is my java script function which will called on clicking the radio button function:-
<script>
function callServlet()
{
var d;
$(function() {
d = $('input[name=lang]:checked').val();
});
console.log("value = " + d);
$.ajax({
url: "AllProgramNameServlet",
type: "post",
data: {
language: d
},
success: function(msg)
{
for (i = 0; i < msg.length; i++)
{
var combo = document.getElementById("combo");
var option = document.createElement("option");
option.text = msg[i].programName;
option.value = msg[i].programName;
try {
combo.add(option, null); //Standard
} catch (error) {
combo.add(option); // IE only
}
}
}
});
}
</script>
You can first clear the dropdownlist before appending options using:
$('#combo').empty(); //empty the list
or for keeping first option and removing rest using:
$('#combo option:gt(0)').remove();

Dynamically show a panelGroup when an event happens (JSF/ICEFACES)

I want to show a panelGroup dynamically.
In this code, I have boxes which can be filled up. When the last available box is filled up with data, a panelGroup with a new set of boxes should show up.
<ice:panelGroup binding="#{myPage.boxes0to9}" />
<ice:panelGroup binding="#{myPage.boxes10to19}" />
How do I let the second panelGroup display only when the last box in #{myPage.boxes0to9} has been filled in?
Thanks!
You can achieve it by wrapping the <ice:panelGroup> with a div and show/hide it by a javascript check:
<script type="text/javascript">
var arrInputs = new Array(9);
function checkInputsFulfilled(value, index) {
arrInputs[index] = (value == "");
var filled = true;
for(var i = 0; i < arrInputs.length; i++) {
if (arrInputs[i]) {
filled = false;
break;
}
}
document.getElementById("myDiv").style.display = filled ? "block" : "none";
}
</script>
<ice:panelGroup binding="#{myPage.boxes0to9}">
<!-- your 9 inputs (or more) here, I'll write 1 as a sample -->
<h:inputText value="#{myBean.attribute1}" onchange="checkInputsFulfilled(this.value, 0);" />
</ice:panelGroup>
<div id="myDiv" style="display:none">
<ice:panelGroup binding="#{myPage.boxes10to19}">
</ice:panelGroup>
</div>
You can also do it using an ajax support and rendered a JSF component that wraps the second <ice:panelGroup> but this will have a big impact when you just empty one of yout starting inputs and the group should hidden. Of course, this last behavior is controlled in the javascript, if you dont want it just modify it :).

Categories