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

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.

Related

Entire row is not getting selected in JTable

I am trying to select an entire row from the jtable. The first time, entire row is getting selected, but from the next time, only few cells are getting selected though entire row data is obtained.
Code:
jDelete.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(!jtable.getSelectionModel().getValueIsAdjusting())
deleteRow(jtable.getSelectedRow());
}
});
jtable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()){
jtable.setRowSelectionAllowed(true);
String[] arr = new String[9];
int row = jtable.getSelectedRow();
if(row!=-1){
for(int i=0;i<9;i++){
arr[i] = (String) jtable.getValueAt(row, i);
}
jId.setText(arr[0]);
jName.setText(arr[1]);
jTime.setSelectedItem(arr[2]);
jMail.setText(arr[3]);
jMobile.setText(arr[4]);
jCourse.setSelectedItem(arr[5]);
jFee.setText(arr[6]);
jPaid.setText(arr[7]);
jBalance.setText(arr[8]);
}
}
}
});
When I try to select the row and delete it, first time it is deleting properly. From the next time, when I click on a row, few cells are shown as selected but the entire row is obtained. How to make it to display as the entire row selected?
The first time : The entire is row selected properly.
[![enter image description here][1]][1]
The second time : The entire row is not selected entirely.
[![enter image description here][1]][1]
The entire code is posted here :
https://ideone.com/7EJiRQ
jtable.setRowSelectionAllowed(true);
Don't set this property in the selection listener. This is the default behaviour when the table is created. So the above code is not needed.
public void actionPerformed(ActionEvent e) {
if(!jtable.getSelectionModel().getValueIsAdjusting())
deleteRow(jtable.getSelectedRow());
Why are you checking the selection model in the ActionListener. There is no need to do this. The row will already be selected when the button is clicked.
However the code should be something like:
int row = jtable.getSelectedRow();
if (row != -1)
deleteRow( row );
This will make sure a row is selected before attempting to delete it.

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.

Questions about Jtable. Click within particular column perform event, is this possible?

I've added several rows to a Jtable, and I don't know if it's possible but I'd like if you click on any cell within a particular column then the connecting row is removed.
Are functions like this possible?
(I'm not asking anyone to do all the work for me. Just asking for information or a tutorial link) thanks :-)
attach a mouse listener to the table and when mouse click event occurs if column matches your particular column then remove that row.
tbl.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int row = tbl.rowAtPoint(e.getPoint());
int col = tbl.columnAtPoint(e.getPoint());
if(col == SPECIFIC_COLUMN_INDEX){
((DefaultTableModel)tbl.getModel()).removeRow(row);
}
}
});

How to auto select all rows in a JTable and freeze the selection?

I would like to auto select all rows in a JTable and make it impossible to manually change the number of selected rows.
All rows are selected automatically and it should not be possible to alter the selection
Selecting all rows is pretty straight forward
//Auto select all rows in the table
SwingUtilities.invokeLater(new Runnable(){
public void run(){
queueTable.selectAll();
}
});
Next step is to remove any possibilities to set focus on the table
queueTable.setFocusable(false);
Here it stops, how to freeze the selection at this point?
queueTable.setRowSelectionAllowed(false); // unfortunately this will clear the current selection.
You can try to hardcode that in next way:
youeTable.setSelectionModel(new DefaultListSelectionModel(){
#Override
public void setSelectionInterval(int arg0, int arg1) {
super.setSelectionInterval(0, t.getRowCount());
}
});
in that case in your table always be selected rows from 0 to the end of the table.

Categories