JTable if any row is selected - java

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
}

Related

Filling up cell when clicked

First of all thanks for your support. I'm creating an application with MySQL database to store User details (Names, Age..). Just want to send the Name value to a JTable cell clicked by the user.
I'm able to store the value in a String, and show it in System.out.println, but cannot fill the cell up with the value.
Is this possible any how?
I'm able to store the value in a String, and show it in System.out.println, but cannot fill the cell up with the value.
It is unclear how do you get this String and what have you tried but you can use setValueAt(...) method to set this String as the selected cell's value. In order to get the selected cell row and column indexes you should use getSelectedRow() and getSelectedColumn() methods, respectively. Finally to detect user's "click" you have to attach a MouseListener to the table or a ListSelectionListener to the table's ListSelectionModel to listen for selection changes.
See the API:
JTable#setValueAt(Object value, int rowIndex, int columnIndex
JTable#getSelectedRow()
JTable#getSelectedColumn()
See also:
How to Use Tables tutorial.

How to hide the Jtable column data?

I want to hide data of a column of Jtable, but not hide the column view,just its data.
The column contain data about profit and the customer doesn't want to show the profit but in my code I use the values of this column and get it when the user select specified row.
How do I achieve the customer need and still be able to get the values of this column when selecting it after hiding its data(displaying column as empty but still has its values)?
You need to remove the TableColumn from the TableColumnModel of the JTable. For example:
table.removeColumn( table.getColumn(...) );
Now the column will not display in the table, but the data is still available in the TableModel. to access the data you use:
table.getModel().getValueAt(...);
You could try a subclassing DefaultTableCellRenderer, and call setCellRenderer. When the relevant column number is passed to getTableCellRendererComponent, return a blank JLabel, otherwise call the default super.getTableCellRendererComponent.
This should mean the column is still visible, but each cell will be blank.
If you want to display the data when a row is selected, you will need add a listener to the selection model (from getSelectionModel) to store the selected row in a variable, and call a repaint. You can then use this value in your CellRenderer.
Why don't you just let the dataModel control what is shown? I'm assuming you are using a tableModel.
All you have to do is change the getValueAt(..) method so that it does not return a value for the column you do not want to show. Make sure that getColumnCount() method is reduced by one as well.

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.

remove focus from JTable first row

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

Categories