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
Related
In my project i have used JTable to billing. In first column i will give some number as input like 1 when i click tab button second column should display name of the product. I wrote it for mouse clicked event. But i dont know how to write it for focus gained. Am using Netbeans IDE.
if(table.getSelectedColumn() == 1)
{
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
int code = Integer.parseInt(table.getValueAt(table.getSelectedRow(), 0).toString());
if(code < 1 || code > 48)
{
JOptionPane.showMessageDialog(this, "Please Enter Correct Product Code");
return;
}
if(table.getValueAt(row, 0) != null)
{
table.setValueAt(tamil.get(code-1), row, 1);
}
}
This is my code its working fine for mouse clicked. Anyone can help me?
I wrote it for mouse clicked event.
Don't write code for a mouse event. What it the user uses the tab key to move to the next cell?
Instead implement a general solution by overriding the setValueAt(...) method of your TableModel.
When you change the value in the first column do your lookup and change the value in the second column. Something like:
#Override
public void setValueAt(Object value, int row, int column)
{
super.setValueAt(value, row, column);
if (column == 0)
{
String name = lookupName(...);
super.setValueAt(name, row, 1);
}
}
I'm building a spread sheet application. But this is not a question like using table.getSelectedColumn() and table.getSelectedRow() to find the selected cell in a JTable.
In Microsoft Excel, when we navigate through cells using arrow keys, the content in the cells are displayed in the Formula Bar immediately after highlighting the cell. Here, the most important thing is, when a cell is highlighted by the selection as above, the value inside the cell is displayed at the sametime. So my question is, how can we do the same in JTable?
I have tried to do something similar using keyEvent listener, but the problem with that is, when a key event is generated, the next cell is being highlighted but the indices of the previous(which was previously highlighted) is being returned in the getSelectedRow() and getSelectedColumn() methods.
Also i tried the ListSelectionListener. but the same fault exists.
If there's any way to get the selected cell's indices immediately after the new cell is highlighted when navigated using arrow keys, that will work. Also an event should be generated since I want to update the formula bar as in Excel. Can someone help me with this?
Thanks in advance!
You can use this simple trick!
Only have to make two excess jTextfields. (can set to be invisible at the run time)
Try to get an idea from below code segment.
private int r;
private int c;
private String buffer;
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
jTextField2.requestFocus();
buffer = jTextField1.getText();
jTable1.getModel().setValueAt(jTextField1.getText(), r, c);
}
private void jTable1KeyTyped(java.awt.event.KeyEvent evt) {
r = jTable1.getSelectedRow();
c = jTable1.getSelectedColumn();
jTable1.putClientProperty("terminateEditOnFocusLost", true);
jTextField1.requestFocus();
}
private void jTextField1FocusGained(java.awt.event.FocusEvent evt) {
buffer = (String)jTable1.getModel().getValueAt(r, c);
jTextField1.setText(buffer);
jLabel1.setText(buffer);
}
private void jTextField2FocusGained(java.awt.event.FocusEvent evt) {
buffer = jTextField1.getText();
jTable1.getModel().setValueAt(buffer, r, c);
jTextField1.requestFocus();
}
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.
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();
}
}
}
}
I need to show a tooltip above (or below :) a cell when the user enter a wrong value in it (see the image below).
I have a tooltip, but I need a Point to display it at the right position, so I want to get a cell position. Do you know how to get this?
BUT, if you have a better solution to realize this behaviour, I'm open to all proposition (especially for the fact that the tooltip is not bind with the cell/Jtable/Panel and if I move/close/minmize my window the tooltip is display at the same position)
Thanks,
Damien
Please refer below code snippet, and you'll get the solution
JTable table = new JTable() {
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (c instanceof JComponent) {
JComponent jc = (JComponent) c;
jc.setToolTipText(getValueAt(row, column).toString());
}
return c;
}
};
If you want to only show the specific cell, all you have to do is change the column param in the params of getValueAt(...) method to a specific column which contains that cell
You have an example of such feature in the Swing components visual guide.
Edit: In fact, it is not really a tooltip that you need here, as the tooltip need to have the cursor positionned over the cell. You want to display the tooltip even if the cursor is outside the cell, right?
Anyway, an alternative solution is to change the background of the cell when the value entered by the user is invalid (in orange or red for example), and then add a "real" tooltip (using the link I provided) in order to give the user a complete error message.
Just use below code while creation of JTable object.
JTable auditTable = new JTable(){
//Implement table cell tool tips.
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);
try {
//comment row, exclude heading
if(rowIndex != 0){
tip = getValueAt(rowIndex, colIndex).toString();
}
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
}
return tip;
}
};
Try getCellRect
Use the following code to get the value for the correct row if using RowSorter:
jc.setToolTipText(getValueAt(convertRowIndexToModel(row), column).toString());