I have a custom TableModel that extends the AbstractTableModel.
public class IndicatorPropertyTableModel extends AbstractTableModel
The setValueAt function I wrote fires appropriately in cases when you type a new value into the editable cell and then move on to another cell. The problem I am having is that for the last editable cell the user will immediately click a JButton to continue. This does not trigger the setValueAt function thus not saving the data. Is there a good way to ensure that this value is always stored when a user immediately hits a button after editing a cell? setValueAt function below for reference if needed.
public void setValueAt(Object value, int row, int col) {
if (value == null)
return;
if (col == 0) {
//this.indicatorArguments[row].setIsSelected(value);
} else if (col == 2) {
this.indicatorArguments[row].setValue(value);
}
this.fireTableCellUpdated(row, col);
}
Thanks for any help you can provide.
I assume that your button is outside the table.
In the event listener or action of your button you would need a reference to the table. This table you could then ask whether isEditing returns true. Then you would get the current editor and after validating the last input value either call stopCellEditing to commit the value or cancelCellEditing to undo the value.
In short:
if(table.isEditing()){
table.getCellEditor().stopCellEditing();
}
would always commit not yet entered values.
Checking if the table is editing requires you to add code to all buttons on your GUI.
You may be able to use a simple one line solution that avoids this. Check out Table Stop Editing.
Related
Things that i am trying to achieve:
Select the row and add in values, the first value at col=0 will determine what will generate at col=2 and what col=2 value is will generate values for col=3.
To achieve what i have said on the selected row, for example if i have more than 2 rows of data which is empty. When i fill in the first row of data, the second row of data should be remain untouched. The second row of values are only generated when i click of the second row value.
Lastly this is just another button that i have which is a "save" button where it will retrieve the data from the table and store it. However i am getting this problem where when my mouse is still selected on the cell, that selected cell value is not being passed in and stored. So is there a way when i click the button, i can deselect the cell?. Side note i have used clearSelection and it does not work.
Currently what i have:
At the moment i managed to do the change where when i click a value from the col=0 and it will update the value for col=1. However i am now stuck at how i am going to get the value from the col=1 to generate my values for col=3 which is the last col. I have tried adding another if else in the method however that does not work.
At the moment the values i add in are combobox these combobox values are derived from a method that passes in the selection choice of the values from their respective cols.
Method for table listener:
public static void TableListen(JTable Model,SQLObject so){
Model.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent evt) {
if(Model.getModel().getRowCount()!=0){
if(Model.getSelectedColumn()==0){
if(!Model.getModel().getValueAt( Model.getSelectedRow(), 0).toString().equals("-Selection-")){
String treatmentChoice=Model.getModel().getValueAt(Model.getSelectedRow(), Model.getSelectedColumn()).toString();
JComboBox<String> comboBox = null;
TableColumn feedingCol=Model.getColumnModel().getColumn(1);
comboBox=DosageObject.GetListOfMedication(so,treatmentChoice);
feedingCol.setCellEditor(new DefaultCellEditor(comboBox));
}
}
}
}
});
}
I wanna add a button to some specific cells where i might get null characters from my database. I tried overriding 'TableCellRenderer' . But it keep adding buttons to entire column. What should i do? (example wd b better)
But it keep adding buttons to entire column.
Yes, a renderer is designed to work for the entire column.
If you want a different renderer for a different row then you can override the getCellRenderer(...) method. Something like:
public TableCellEditor getCellEditor(int row, int column)
{
Object value = getValueAt(row, column);
if (value == null)
{
return super.getCellEditor(row, column);
}
else
return getDefaultRenderer(value.getClass());
}
You would also need similar code for the getCellEditor(...) method.
You can also check out Table Button Column for an example of a button renderer/editor.
I need a listener on a CombobBox which is a cellEditor on a JTable.
This listener must give me the new selected value and the row id.
Problem with my below solution is that the listner is linked to all rows, so when I change one ComboBox value in one row, then move to another row (with a different combo value) an event is raised, but the selected row has not yet changed. How can I get rid of this case ?
Thanks
column = jTableCheck.getColumnModel().getColumn(9);
JComboBox comboBox = new JComboBox(comboGenre);
comboBox.addItemListener(new ItemListener(){
#Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
int row = jTableCheck.getSelectedRow();
Popup.info(e.getItem() + " SELECTED, row="+row);
}
}
});
column.setCellEditor(new DefaultCellEditor(comboBox));
Don't use an ItemListener on the combo box.
Instead you should be using a TableModelListener. An event will be fired whenever the data in the TableModel is changed. So you add the TableModelListener to the TableModel of your JTable.
The TableModelEvent will give your row/column of the cell that changed. You can get the changed value from the TableModel.
Or maybe you would want to use a Table Cell Listener which is similar to the TableModelListener except the code is only invoked when the value is actually changed and you use an Action to do the processing.
In fact, I already used a TableCellListener on another table, but forgot about that!
I found out a usefull class here: http://tips4java.wordpress.com/2009/06/07/table-cell-listener/
I have a JTable with several columns. I override the getColumnClass method of the table model in order to specify which columns hold Integer values. So basically when a user tries to enter a String into an Integer column, he/she is not allowed to do so. The problem is that the user can still click on a button on my form which then uses the improper value in that cell.
How can I not allow the user to click on any buttons as long as one of the cells in the table is still being edited?
Add a PropertyChangeListener to the JTable:
#Override
public void propertyChange(PropertyChangeEvent e)
{
// A cell has started/stopped editing
if ("tableCellEditor".equals(e.getPropertyName()))
{
if (table.isEditing())
// disable buttons;
else
// enable buttons;
}
}
Or, if you don't want to disable the buttons, you can just add code to the ActionListener to check if the table.isEditing() and if so then just return.
You could use one of the 3 methods returning information on the editing process to enable your button.
JTable table = new JTable();
table.getEditingColumn();
table.getEditingRow();
table.getEditorComponent();
Check JTable documentation to see which could be best used for your case. You could make your button enabled only if
table.getEditorComponent();
returns null for example.
add a TableModelListener to your JTable
you should override its tableChanged method somewhat like this :
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
//Check the data!!!
//Check the data!!!
//Check the data!!!
//disable the button if needed right here
}
ref:http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#modelchange
I want to know why my getvalueAt() is picking old data when Enter is pressed.
I tried all update and table change modules, but I couldn't get it working. I am making an Excel sheet-like structure in JTable, in which one row updates on change to another row.
public void setValueAt(Object aValue, int row, int column) {
if(column == 2 || column == 3 || column == 4 || column == 5)
{
System.out.println("2 is: "+getValueAt(row, 2));
System.out.println("3 is: "+getValueAt(row, 3));
System.out.println("4 is: "+getValueAt(row, 4));
long closingbalance = Long.parseLong(getValueAt(row,2).toString())
+ Long.parseLong(getValueAt(row,3).toString())
- Long.parseLong(getValueAt(row,4).toString());
System.out.println("closing: "+closingbalance);
super.setValueAt(closingbalance,row, 6);
}
super.setValueAt(aValue, row, column);
}
I can't be able to answering your question on another forum, because a new forums version driving me crazy
there no reason parsing value from JTable,
JTable implemets all important data types
examples here or here
for better help edit your question with SSCCE
Verify that your CellEditor has concluded and that your TableModel is in a consistent state when you access other values. This example override's getValueAt() to derive the dependent cell's value. To avoid so much parsing, your TableModel should probably just contain instances of Long.
Assuming you really need to override setValueAt(), it's not clear what TableModel you're using. If you're extending DefaultTableModel, the super.setValueAt() implementation is appropriate; if you're extending AbstractTableModel, the super implementation is empty, and yours will need to fire the appropriate event.