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.
Related
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.
I have a simple code for Add button like this :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel dtm = (DefaultTableModel)table.getModel();
dtm.addRow (new Object[] {name.getText(),mobile.getText()});
}
This code will get text from JTextField and insert into JTable row
I want to add function right-click popup menu when i right-click on table row, and add some like add delete rename.
How can I do that ?
Start by reading the section from the Swing tutorial on How to Bring up a Popup Menu for the basics of displaying a menu and a working demo.
In the case of a JTable, you will probably want to highlight the row that was clicked on so your Actions can act on the selected row.
So you would need to add code like the following to the maybeShowPopup(...) method from the demo example in the tutorial:
if (e.isPopupTrigger())
{
JTable source = (JTable)e.getSource();
int row = source.rowAtPoint( e.getPoint() );
int column = source.columnAtPoint( e.getPoint() );
if (! source.isRowSelected(row))
source.changeSelection(row, column, false, false);
popup.show(e.getComponent(), e.getX(), e.getY());
}
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
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());
}
});
I have a JTable with a set of uneditable cells and I want all the cells in a particular column to have a different mouse cursor displayed whilst the mouse is hovering over them.
I am already using a custom renderer and setting the cursor on the renderer component doesn't seem to work (as it does for tooltips).
It does seem to work for editors.
Is this not possible in JTable when your cell is not being edited or am I missing something?
Add a MouseMotionListener to the JTable and then on mouseMoved() determine which column it is using JTable's columnAtPoint() and if it's the particular column you are after, setCursor() on the JTable.
Here is one way of changing the cursor at a particular column in JTable:
if(tblExamHistoryAll.columnAtPoint(evt.getPoint())==5)
{
setCursor(Cursor.HAND_CURSOR);
}
else
{
setCursor(0);
}
I wanted to only change the cursor when the mouse was over the text in the cell. This was my solution:
private JTable table = ...;
#Override
public void mouseMoved(MouseEvent e) {
Point point = e.getPoint();
int column = table.columnAtPoint(point);
int row = table.rowAtPoint(point);
Component component = table.getCellRenderer(row, column).getTableCellRendererComponent(table,
getValueAt(row, column), false, false, row, column);
Dimension size = component.getPreferredSize();
Rectangle rectangle = table.getCellRect(row, column, false);
rectangle.setSize(size);
if (rectangle.contains(point)) {
table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
return;
}
table.setCursor(Cursor.getDefaultCursor());
}