remove focus from JTable first row - java

I have JTable and rows in it. By default first row is selected and focus is in it. How can I deselect first row and change focus to somewhere else that .addListener(new RowSetListener() will work in first row too.
I already try:
tableZaposlenciView1.setRowSelectionAllowed(true);
//tableZaposlenciView1.getSelectionModel().clearSelection();
//tableZaposlenciView1.setColumnSelectionInterval(0,0);
//tableZaposlenciView1.setRowSelectionInterval(false,false);
tableZaposlenciView1.changeSelection(0,0,false,false);
tableZaposlenciView1.requestFocus();
but it is not working.

Have a look at JTable's changeSelection() method. I believe it does what you want.
EDIT: If you want to clear the selection:
JTable table = ...;
table.getSelectionModel().clearSelection();

if someone else have similar problem with ADF and JTable here is solution. I achieve that by overriding first() on the VO impl. my problem can be solved.
That comes in handy in many situations:
no selection after refresh (just return null from first())
reselect a particular row after refresh (before refresh, store a row key, after refresh in first() if stored key is found, navigate to that row and return that from first())
find next matching row for user to work on after refresh of a worklist
avoid costly detail executions in a VL situation

Related

Adding row to a ListGrid at a specified index Smart GWT

Currently I'm working on a listgrid which is editable and my requirement is to create a a new row every time a user selects a row and presses the "Create" Button. The new row needs to be added immediately below the selected row. I tried using the below method
ListGrid.getRecordList().addAt(ListGridRecord rec, Index index)
However I got the warning message
15:48:04.373:MUP3:WARN:Log:ResultSets are readonly. This operation (addAt) will be ignored.
I've searched the smartgwt showcase to look for ways to edit a grid , so that new row is added at a specified index, however I wasn't able to find anything suitable.
I got to know that ResultSets is getting created because I'm using the statement
ListGrid.fetchData() Is there any way to solve this issue? Any suggestion is highly appreciated!
Muchas Gracias.
Figured it out, quite a weird trick...but works for me...
what you need to do is use grid.setRecords(grid.getRecords()); just before grid.getRecordList().addAt(rec, index); I think doing this makes the ResultSet editable.

JTable if any row is selected

I am trying to figure out how to add logic to a JTable via an "if" statement that checks to see if ANY row is selected. I know how to check if a specific row is selected but I can't seem to figure out how to check all rows.
if(tbl.isRowSelected(0)){
Obviously that is checking for a specific row.
I've also tried something like
if(tbl.isRowSelected(0-2000)){
This did not work nor did I expect it to work.
The reason for this is that I'm setting up the table so that when the user clicks a row and then hits an "edit" button a second table will appear with more data related to the row they selected. (Gets complicated here with 2D arrays inside of a hash map but I first need to get by this simple problem).
Thanks for the help in advance!
This should be possible with a ListSelectionModel, which can be retrieved using JTable::getSelectionModel()
So you can call table.getSelectionModel().isSelectionEmpty() to find out if any row is selected.
Use :
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
// do some actions here, for example
// print first column value from selected row
System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString());
}
});
use table.getSelectedRow() it returns the index of the selected row
and getSelectedColumn() return the selected columns
and there is getSelectedRows() it return index arrays of selected rows
int[] indexs=table_name.getSelectedRows();
//all the selected row are here no need to go throw
//all your rows to see if they are selected or not
for(int index_row:rows){
//you code here
}

How can I render a JList inside a JTable cell?

In three of the columns of my JTable that I have set up I would like to have a list in each cell of the column. I'm not sure where to start on possibly creating a custom cell renderer class if that's the best option? My goal is to list the group names each on their own line in each cell, and expand the cell height as new lines are added. Each group will have an AdmType and Admitted entry, so I will also need to figure out how to add another checkbox to the Admitted column cell for every new Group entry.
This below solution did not work for me, but it clearly worked for the person who posted a screen shot of their success. My problems may be due to how I have my TableModel set up.
https://stackoverflow.com/a/32793088/6867420

How can delete/hide the mid row(5)th row before it listed in jtable

Here i have an 1-10 row is listed in jtable i want to delete/hide the 5th row before it listed in jtable.
i set the rowheight but it affected the cellselection.Is there any way to hide/delete the row without affected the normal flow code?
If i remove the row it will throws ArrayIndexoutofBoundException.
in my project executed means one gui open in that gui listed the some string. In here we can add the more string via Add Button on popup Button
Here what i need is i have to hide the particular string. That string is placed on 1st row.
i need to hide the string from end user.
now u hope understand.
You can use the JTable row filtering support in order to hide certain rows without deleting them from the model. Also see this: How can I filter rows in a JTable?
You can eliminate rows in the table by calling the removeRow() method. If you want to just hide it instead of elimintaing it you need to customize the JTable's model to meet your specs on what to display.
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
using DefaultTableModel with JTable you should be able to use model.removeRow(int row) function to remove A row from JTable. There is no way to hide a row based on index as much as i know. However, If you need to hide and re-show mechanism you need to save the row prior to delete it and Save the removedRow in a ArrayList to re-use them.. Something as follows:
List<Vector>deletedRows = new ArrayList<>();
Vector removingRow = (Vector) model.getDataVector().get(5);
deletedRows.add(removingRow);
model.removeRow(5);

How to get JTable selectedRow and selectedColumn during FocusLost event

Whenever there is a lostFocus inside a JTable, i need to capture the existing cell's row and column.
However, the condition below is always false because the source is always either a JTextField or a JComboBox.
public void focusLost(FocusEvent e) {
int row, col;
Object source = e.getSource();
if(((Component) source).getParent() instanceof JTable_Ext){ //<-- always false
table = (JTable_Ext) ((Component) source).getParent();
row = table.getSelectedRow();
col = table.getSelectedColumn();
}
To mitigate the above, i remember the row and col during FocusGained (as class level variable). The problem is, if the user click very fast all over the place within the JTable, somehow the row and column information will be out of sync.
Is there a way to get the Row and Col during FocusLost? if not, is there a better way of doing this?
Well, there is the oppositeComponent. The weird thing is, if this listener is attached to the table, the documentation tells that the table should be the "source" component (because it is a FocusLost event and the component that lost the focus is the table itself).
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/event/FocusEvent.html#getOppositeComponent%28%29
Could you just keep a record of row & column using
table.getSelectionModel().addListSelectionListener(...);
table.getColumnModel().getSelectionModel().addListSelectionListener(...);
So every time the use clicks update it, not just on focus events?
If you just want to save the data that was entered in the cell (without hitting return), then you don't need to do anything. The updated information is contained within TableModel of the JTable.
Otherwise you can take a look at .tableChanged() and the associated TableModelEvent, which gives you the last row/column modified. You could keep a variable that is always updated to the latest event row/column. I guess that if you change a cell number without hitting return, it nonetheless registers as an event.
However, the condition below is always false because the source is always either a JTextField or a JComboBox.
This implies that the focusLost event is being generated when you begin editing a cell. So the question is why are you doing this? I think you need to state your actual requirement, because you attempted solution does not seem appropriate.

Categories