Entire row is not getting selected in JTable - java

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.

Related

How to get the index of a column of my jTable?

What happens is I want to get the index of the header of my column of my jTable when I click, I have tried with:
Tabla.getSelectedColumn() But this only devains the index of the column when I click on the cell.
You can add a mouse listener to the table's column header.
In the mouse listener's mouseClicked() method, you can use the getTableHeader().columnAtPoint() method to get the index of the clicked column.
table.getTableHeader().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int column = table.getTableHeader().columnAtPoint(e.getPoint());
System.out.println("Column index: " + column);
}
});
This will print the index of the clicked column to the console every time a column header is clicked.
Alternatively, you can use Jtable's getSelectedColumn(), which will give you the
index of the selected column.
int selectedColumn = table.getSelectedColumn();
This will give you the index of the selected column.

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.

Implement a rename function for a cell in jtable in java

As a beginner,i am creating a jtable with some functionalities like adding and removing contents. I would like to know how to make a rename functionality to my application that on selecting this menu should highlight all the contents of the cell as in an editing mode. Thanks in advance
Continuing from your previous post.... Did you want something like below, when when you hit edit in the context menu, you can edit in some popup window?
→
You pretty much already have to tools for this functionality (in your code). For the new Action, you simply need to show a JOptionPane input dialog with the value of the selected cell. The return input of the JOptionPane will be the value you set back to the table. Something like. Keep in mind though, depending on the type of data, you may want to do some logical parsing or conversion. Below I just take the value as a String.
class EditCellAction extends AbstractAction {
private JTable table;
public EditCellAction(JTable table) {
putValue(NAME, "Edit");
this.table = table;
}
#Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
String newValue = JOptionPane.showInputDialog(table,
"Enter a new value:", table.getValueAt(row, col));
((DefaultTableModel) table.getModel()).setValueAt(
newValue, row, col);
}
}
If you don't want the popup, and just want to programmatically start the cell editing, you can simple use table.editCellAt(row, col) to start the editing, and use the underlying text field of the cell editor to select the field contents. Something like below (tested and works)
#Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
table.editCellAt(row, col);
JTextField field = (JTextField) ((DefaultCellEditor) table
.getCellEditor()).getComponent();
field.requestFocus();
field.setSelectionStart(0);
int endSelection =
(!field.getText().isEmpty()) ? field.getText().length() -1 : 0;
field.setSelectionEnd(endSelection);
}
Keep in mind though, if the cell is editable, the user can just double click the cell to edit it. I guess this adds some more functionality

Get jTable row number from popup item

I have a jTable as from the attached picture
Right click on a row starts a jPopup, with a single item "Thread Stop".
I would like to return the row number by clicking on this menu item
How to accomplish this?
Thanks.
In your MouseListener where you show your popup, simply get the row and column numbers via the JTable methods:
table.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
int row = table.rowAtPoint(p);
int col = table.columnAtPoint(p);
System.out.printf("row, col: [%d, %d]%n", row, col);
// show pop-up menu here
}
});
Your implementation of TableCellEditor includes the row as a parameter, but you should act only when the TableModel is updated, as shown here. TablePopupEditor is a related example.

how to fill JTextFields with the columns of a JTable search?

I have a master/detail form with a JTable on top, and all the JTextFields corresponding below in the JPanel. I'm trying to make a search in the JTable, so that when the correct row gets picked, all the JTextFields can be filled with the column values. I don't know how can I call the rows programmatically to do so. How would it be done?
This is the code I'm using to do the search:
int rows = (masterTable.getModel()).getRowCount();
final int colCedula = 1; //columna de la CEDULA
final int colRuc = 11; //columna de RUC
String value = null ;
for(int i=0; i
value = (String) (masterTable.getModel()).getValueAt(i, colCedula);
if (value.equals(this.txt_BuscaCliente.getText())) {
//CODE FOR FILLING JTEXTFIELDS
}
If the search finds the column value and stops the loop, could I just write in the //CODE section masterTable.getSelectedRow() and then fill all the JTextFields with its column values???
Also, how is it done to have the row selected highlighted, programatically? Let's say, after my search finds the column value, to have that row highlighted in the JTable
I'd start with the example in the tutorial article How to Use Tables: User Selections in order to understand list selection events. Given a SINGLE_SELECTION model, you won't have to search; just fill in the text fields from the selected row. Alternatively, you can make the cells editable in your table model, and you won't have to copy them at all.
Addendum:
Also, how is it done to have the row selected highlighted, programatically?
Instead of searching, let your implementation of ListSelectionListener tell you what selection has been made by the user. In the example cited, modify the RowListener as shown below to iterate through the columns in the selected row.
private class RowListener implements ListSelectionListener {
#Override
public void valueChanged(ListSelectionEvent event) {
if (!event.getValueIsAdjusting()) {
for (int c : table.getSelectedRows()) {
int row = table.convertRowIndexToModel(c);
TableModel model = table.getModel();
for (int col = 0; col < model.getRowCount(); col++) {
System.out.println(model.getValueAt(row, col));
}
System.out.println();
}
}
}
}

Categories