SWING JTable, cell start to edit event - java

I have a JTable and these cells of it are editable, there are any ways to handle the "cell start to edit" event in order that I can display a message when users start to edit a cell.

If this were my code, I'd start by trying to override the TableEditor's getTableCellEditorComponent method. Inside of the override, I'd call the method that I want to call when editing starts, and then I'd call the super's getTableCellEditorComponent method inside the override.
You can find out more details on how to use custom cell editors (since this is what you need to do) at the JTable tutorial.

Add a PropertyChangeListener to the JTable:
//
// Implement the PropertyChangeListener interface
//
#Override
public void propertyChange(PropertyChangeEvent e)
{
// A cell has started/stopped editing
if ("tableCellEditor".equals(e.getPropertyName()))
{
if (table.isEditing())
// code for editing started;
else
// code for editing stopped;
}
}

Related

How to add a table listener to a JTable?

I am having problems with fixing something in my program. Basically I know how to use action Listeners but there is no option to add one to a JTable. How is this done?
Basically I want to add an action Listener to my table so that every time a value is changed it will update that field in my data base.
I.E.
JTable.addActionListener (new ActionListener) {
// text is changed
updateDataBase();
};
You should add a listener to the TableModel:
yourtableObject.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
// your code goes here, whatever you want to do when something changes in the table
}
});
TableModelEvent contains row and column number and type of modification.
TableModelEvent is used to notify listeners that a table model has changed.
Start by taking a look at How to Use Tables
What you will want to do is register a TableModelListener with the JTable's model and monitor for changes there
You may also find How to Write a Table Model Listener of some use
The kind of thing you are look for is
TableModel#getType equals TableModelEvent.UPDATE
TableModel#getFirstRow and TableModel#getLastRow are typically equals (singularly that a single row was update), this may or may not be relevant, that's up to you to decide
TableModel#getColumn is not equal to TableModelEvent.ALL_COLUMNS, this signifies that a single cell was updated. Again, this may or may not be important, but if the cell was edited by the user, this will be set
Take a look at javax.swing.event.TableModelEvent for more details
If you want to have an event when there is a change in selection you can use:
table.getSelectionModel()addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent l) {
//action
}
});
Source: http://www.java2s.com/Tutorial/Java/0240__Swing/TableSelectionEventsandListeners.htm

Start Editing a cell in JTable automaically

I am developing a restaurant billing system.
So here is the order panel interface
So now when i click on the menuTable the item code automatically gets added to kotTable
and when i press "Q" the focus shifts to quantity column in kotTable.
`
private void menuTableKeyTyped(java.awt.event.KeyEvent evt)
{
if(evt.getKeyChar()=='Q') {
kotTable.editCellAt(i-1,2);
}
} `
The problem is The cell doesnt start editing automatically. I need to click on that cell and then the editing starts.
i tried using DefaultCellEditor , getInputMap() and many other. But I am bit confused and the problem is not solved.. Thanx
Using a key binding, you can map the Q key to the table's "startEditing" action. More examples may be found here.
table.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), "startEditing");

Java: do file menu clicks take focus?

I had a problem with tables whereby a cell being edited did not store its new value until the user pressed enter or tab, whereas I wanted it to commit the value as soon as focus was lost as I was finding that selecting 'file -> save' whilst editing a cell caused that cell's data to be lost.
I found that you can simply set
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
which works well for every change of focus except when the file menu is selected, in which case the cell stays in edit mode.
My question is, does setting this property not behave as one might expect it to, or does focus work differently for menu items by default in Java?
If you don't want to make the menu items focusable then you will need to add code like the following in your Save Action:
if (table.isEditing())
table.getCellEditor().stopCellEditing();
If you look at the JMenuItem source code there is a function call being made to initFocusability() witch in turns calls setFocusable(false).
Java Doc for initFocusability()
Inititalizes the focusability of the the JMenuItem.
JMenuItem's are focusable, but subclasses may want to be,
this provides them the and invoke something else, or nothing at all.
Refer to {#link javax.swing.JMenu#initFocusability} for the motivation
of this.
Java Doc from JMenu.JMenu#initFocusability
Overriden to do nothing. We want JMenu to be focusable, but JMenuItem
doesn't want to be, thus we override this do nothing. We don't invoke
setFocusable(true) after super's constructor has completed as this has
the side effect that JMenu will be considered traversable via the
keyboard, which we don't want. Making a Component traversable by the
keyboard after invoking setFocusable(true) is OK, as setFocusable is
new API and is speced as such, but internally we don't want to use it
like this else we change the keyboard traversability.
So it sounds like you need to call setFocusable(true);.
I have found that a combination of the two answers above has solved this problem. I have a focusable menu bar, and have added a DefaultCellEditor with an overridden getTableCellEditorComponent method that adds a focus listener to stop editing:
table.setDefaultEditor(String.class, new DefaultCellEditor(new JTextField()){
#Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected,
int row, int column)
{
Component c = super.getTableCellEditorComponent(table, value, isSelected, row, column);
c.addFocusListener(new FocusListener()
{
public void focusGained(FocusEvent e)
{
}
public void focusLost(FocusEvent e)
{
stopCellEditing();
}
});
return c;
}
});

Detecting if any cell is being edited in a JTable

I have a JTable in which users can edit cells. When the user is through editing the cell, I want to enable a button that allows the user to validate the input. However, I only want to do this when the user is no longer in edit mode. Is there an easy way for me to detect if any cell in a Jtable is currently being edited?
Thank you,
Even easier:
if (!table.isEditing())
//
It turns out that the Jtable has a way to detect if any cell is being edited. It is actually very simple. You simply check if myJtable.getCellEditor() == null. If it is not, you are editing a cell, if it is, no cells are being edited.
class TableEditor extends DefaultCellEditor{
JTextField com;
public TableEditor(){
getComponent().addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e){
com.setBackground(new Color.red);
}
});
}
}
And then
tb.setDefaultEditor(Object.class,new TableEditor());
I would use a custom TabelModel and override the setValueAt function to perform your validation. It should automatically be called whenever a user is done editing a table cell.

Using JTable for a JTree cell editor

I would like to use a JTable for editing a JTree, I extended DefaultTreeCellEditor and implemented isCellEditable getTreeCellEditorComponent, in getTreeCellEditorComponent I return a JTable. Everything works up to this point when a node is edited swing displays the JTable filled with the objects content however when editing is complete, valueForPathChanged of DefaultTreeModel never gets called. If I use a text field for editing which is the default everything works fine.
JTextField has a notifyAction, named "notify-field-accept" and typically bound to KeyEvent.VK_ENTER, that signals the CellEditor to stopEditing() and ultimately invokes the DefaultTreeCellEditor method, valueForPathChanged().
It's not clear how you indicate that editing is complete for your JTable. You should be able to do something similar with the JTextField in a CellEditorListener that is added to your custom editor via addCellEditorListener().
Incidentally, valueForPathChanged() mentions that "If you use custom user objects in the TreeModel you're going to need to subclass this and set the user object of the changed node to something meaningful."

Categories