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

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.

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.

how to edit all cells in a row with the cell editor of first cell in the row in the Jtable?

I have a JTable with 6 columns. for column 0 and 1 i have created a comboBox as editor.
I want all the cells in the same row be edited when user select an item from combobox of column 0.
Does any one know if it is the best way or is there other way much better?
JTable table = new JTable();// a table with 6 columns
TableColumn column0 = tabel.getColumnModel().getColumn(0);
comboBox = new JComboBox(summary.getGenerics());
column0.setCellEditor(new DefaultCellEditor(comboBox));
column0.setCellRenderer(new DefaultTableCellRenderer());
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String selectedItem = comboBox.getSelectedItem().toString();
table.setValueAt("class" , table.getSelectedRow() , 1);
}
});
This example overrides getValueAt() to condition the value returned by a dependent column based on the selection in a column having a JComboBox editor. Any dependent column should be non-editable, and any TableModelListener should be notified, as shown here.

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();
}
}
}
}

How to get the line number or cell number by double-clicking the mouse in the table

How to get the line number or cell number by double-clicking the mouse in the table.
This isn't the clearest question, but I'm going to assume:
You're talking about JTables
You're asking for the row index
You want to output the row index to stdout
You can add a MouseListener to a JTable that fires on mouse events, and implement the mouseClicked method. The MouseEvent passed to the mouseClicked method has getButton to determine if it was a left click, and getClickCount to determine if it was a double click. If so, the JTable has getSelectedRow to determine the selected row index
It'll look something like:
final JTable table;
// ...
table.addMouseListener(new MouseAdapter() {
#Override public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2)
System.out.println("Current row index: " + table.getSelectedRow());
}
});

Categories