java swing - deselect a selected row in jtable - java

I have an instance of JTable in my java swing application. I want to deselect a selected row from this table. From This answer, JTable has provided clearSelection() method that deselect all selected rows in the table. But I want to deselect one row. How can I do this?

Have you tried:
ListSelectionModel.removeSelectionInterval(int index0, int index1)?

See this it toggles the selection of a row in jtable.

You can do this like that:
JTable table = new JTable(); // your table instance
TableModel dataModel = new DefaultTableModel(); // table model
DefaultListSelectionModel selectionModel = new DefaultListSelectionModel(); //table selection model
table.setModel(dataModel);
table.setSelectionModel(selectionModel);
int desiredRow = 0; // row which you want to deselect
selectionModel.removeSelectionInterval(desiredRow, desiredRow); // Removing selection for desired row

Related

JTable change view but not change row selection after filter data

When data filtered table row was change view according to result but when I select row then table select always first row of column not filtered row but I want both things filter row and same row selection please solve me problem.
jTextField2.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent ke){
trs.setRowFilter(RowFilter.regexFilter("(?i)"+jTextField2.getText(),0));
}
});
tm = (TableModel)table.getModel();
trs = new TableRowSorter(tm);
table.setRowSorter(trs);
Thanks!
ANSWER IS FOR FORMATTED CODE PURPOSE
int modelRow = table.convertRowIndexToModel(table.getSelectedRow());
DefaultTableModel model = (DefaultTableModel) table.getModel();
name=model.getValueAt(index,0).toString();
sale.setVisible(true);
sale.pack();
sale.setLocationRelativeTo(null);
sale.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
sale.jTextField5.setText(name);
dispose();
so this code is not working for you in your mouse event? does the mouse event get triggered? could you edit your question and add more code (if possible all)?

How can i add a mouse click listener to a jCombobox in a jTable cell?

I created a TableColumn like this:
TableColumn ColonneArticle = jTableBonCommande.getColumnModel().getColumn(1);
I filled it from DB table named "Article"
List<Article> l = new ArrayList<Article>();
l= em.createNamedQuery("Article.findAll").getResultList();
TableColumn ColonneArticle = jTableBonCommande.getColumnModel().getColumn(1);
JComboBox comboBox = new JComboBox();
for (int i = 0; i < l.size(); i++) {
comboBox.addItem(l.get(i).getDesignationarticle());
}
ColonneArticle.setCellEditor(new DefaultCellEditor(comboBox));
Now i want to fill my jTable with selected "Article" credentials, so can i add a mouse click listener to a jCombobox in a jTable cell ?
Please Help !
When a cell is updated, the TableModel#setValueAt method is called. When this occurs for the first column, you should then load the values for the row based on the value passed to the this method

JavaFX: Get the selected row cell's text

I need your help.
I am developing JavaFX project that deals with table view and forms.
My Problem is I can't get the text of the selected row in the table view.
I want to get the row cell's text using the row index or the selected row one.
Here is my code:
myTableView.getSelectionModel().selectedItemProperty().addListener( new ChangeListener() {
#Override
public void changed(ObservableValue ov, Object t, Object t1) {
TableView.TableViewSelectionModel selectionModel = myTableView.getSelectionModel();
ObservableList selectedCells = selectionModel.getSelectedCells();
TablePosition tablePosition = (TablePosition) selectedCells.get(0);
int rowIndex = tablePosition.getRow(); // yields the row that the currently selected cell is in
// I Want to get the cell's text in the row using the row_index or the selected row one
}
});
Any solution is appreciated. Thank you!
If you only care about which row is selected, assuming you have a TableView<SomeObject>, you can simply use:
List<SomeObject> selected = selectionModel.getSelectedItems();
or if your table only allows single row selection:
SomeObject selected = selectionModel.getSelectedItem();

Problem with adding JCombobox in JTable in Java?

I have added a combobox in a JTable, the adding code as follows:
Vector<String> header = new Vector<String>();
Vector data = new Vector();
String[] h = new String[]{"Music", "Movie", "Sport"};
header.add("Code");
header.add("Name");
header.add("Salary");
header.add("Hobby");
loadData(); // Add some data to the table
DefaultTableModel tblModel;
tblModel = (DefaultTableModel) this.tblEmp.getModel();
tblModel.setDataVector(data, header);
// Adding combobox to the last column
TableColumn hobbyColumn = tblEmp.getColumnModel().getColumn(3);
hobbyColumn.setCellEditor(new MyComboBoxEditor(h));
Things worked fine until I dynamically add a new row to the table using the code:
Vector v = new Vector();
v.add("E333");
v.add("Peter");
v.add(343);
v.add(""); // This last colum is the combobox so I put it as ""
data.add(v);
tblEmp.updateUI();
Data is added to the table but the combobox in the last column cannot be selected anymore. The combobox is still displayed when I click on the row but cannot select a value.
How can I handle this problem, please?
Never use the updateUI() method. Read the API to see what this method actually does. It has nothing to do with changing the data in a model.
JTable already supports a combo box editor so there is no need to create a custom MyComboBoxEditor. Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables", for a working example of using a combo box as an editor.

JTable's and DefaultTableModel's row indexes lose their synchronization after I sort JTable

JAVA
NETBEANS
// resultsTable, myModel
JTable resultsTable;
DefaultTableModel myModel; //javax.swing.table.DefaultTableModel
myModel = (DefaultTableModel) resultsTable.getModel();
// event of clicking on item of table
String value = (String) myModel.getValueAt(resultsTable.getSelectedRow(), columnIndex)
I use JTable and DefaultTableModel to view a table of various info
and I want to get a value of a certain column of the selected index of the table.
The code I wrote above works fine except when:
I use the sort of the GUI (click on the field name I want to sort on the table)
The table is properly sorted but after that when I select a row, it gets
the value of the row that was there before the sort.
This means that after sorting (using the JTable's GUI)
the 'myModel' and 'resultsTable' objects have different row indexes.
How do I synchronize those two?
You need to use the 'convertXXX' methods on the JTable see the JavaDoc
int row = resultsTable.getSelectedRow();
if (row != -1) {
row = table.convertRowIndexToModel(row);
String value = (String) myModel.getValueAt(row, columnIndex)
A problem with using the JTable.getValueAt() is to get the column you want. When the columns are moved around in the GUI the indexes "change" to match the view. By using the AbstractTableModel.getValueAt() and the JTable.convertXXX() (as outlined by Guillaume) it's just a matter of using the column indexes for the model when retrieving data.
Except from the solution Guillaume gave (Thanks)
I did this:
// resultsTable, myModel
JTable resultsTable;
DefaultTableModel myModel; //javax.swing.table.DefaultTableModel
myModel = (DefaultTableModel) resultsTable.getModel();
// event of clicking on item of table
String value = (String) **resultsTable**.getValueAt(resultsTable.getSelectedRow(), columnIndex)
I used the resultsTable Object instead of the myModel Object to get the value.

Categories