I am having trouble with calculating the sum of my table column whenever a row is added or deleted. I have a button which adds a row to the table, and I want the sum of the to be automatically calculated and displayed in a text field. I have made a method getsum for the calculation and the am calling this method on the actionperformed event of the button. I am getting an error when I run the program and nothing is displayed in the text field.
Add a TableModelListener to your table. That way, the text field will get updated every time the table's model changes.
model.addTableModelListener(new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
textField.setText(getSum());
}
});
you have to call .setText function of the particular textField to bind the value u obtain onto it.
Related
I am currently using a JTable to display a few patient details. I have a JCheckBox, that when ticked adds a new column to the table and adds the data to the new column. When it is unticked, it should remove the column, sort of like adding extra filters to the table. However, when I tick the box again after unticking it, it duplicates the columns in the table.
I tried to use the fireTableStructureChanged() method, but it would never update the table and so the columns stay there even if the checkbox was unticked. But if I remove it, then when I untick the the checkbox it works, but then the duplication problem comes back. I pasted my actionPerformed() method for when the checkbox is ticked. The first image is of my table before I click my Checkbox. The second is when I click the Checkbox and the column is added. Lastly the third is when I untick the checkbox and tick again. Any help will be much appreciated.
#Override
public void actionPerformed(ActionEvent e)
{
if (gui.getChkCholesterol().isSelected() == true)
{
try {
gui.getRightTableModel().addColumn("Cholesterol");
newColumnIndeces.put("2093-3",gui.getRightTable().getColumnCount()-1);
gui.getRightTableModel().addColumn("Cholesterol Effective Date/Time");
newColumnIndeces.put("2093-3e",gui.getRightTable().getColumnCount()-1);
if(gui.getRightTable().getRowCount()>0)
{
int columnNumberValue = newColumnIndeces.get("2093-3");
int columnNumberDate = newColumnIndeces.get("2093-3e");
for (int i = 0; i<gui.getRightTable().getRowCount(); i++)
{
String value = op.getPatientObservationValue(gui.getRightTable().getValueAt(i,0).toString(),"2093-3");
String date = op.getPatientObservationEffectiveDate(gui.getRightTable().getValueAt(i,0).toString(),"2093-3");
gui.getRightTableModel().setValueAt(value, i, columnNumberValue);
gui.getRightTableModel().setValueAt(date, i, columnNumberDate);
}
}
} catch (ParseException parseException) {
parseException.printStackTrace();
}
setCellColourForTable();
timer.schedule(new RefreshTable(), 0, seconds);
}
else
{
gui.getRightTable().removeColumn(gui.getRightTable().getColumnModel().getColumn(newColumnIndeces.get("2093-3")));
gui.getRightTable().removeColumn(gui.getRightTable().getColumnModel().getColumn(newColumnIndeces.get("2093-3e")-1));
newColumnIndeces.remove("2093-3");
newColumnIndeces.remove("2093-3e");
timer.cancel();
timer = new Timer();
}
gui.getRightTableModel().fireTableStructureChanged();
}
You add a column to the model by using:
gui.getRightTableModel().addColumn("Cholesterol");
This will notify the view that the data has changed and the table will also be updated.
However, you remove the column from the table using:
gui.getRightTable().removeColumn(…);
This only removes the column from the "table view". The column has not been removed from the DefaultTableModel. So the next time you add a column it just gets added to the end of the DefaultTableModel.
So the solution is to remove the column from the DefaultTableModel, not the table.
Unfortunately there is no removeColumn(…) method.
However because your requirement is to only add/remove columns from the end of the DefaultTableModel you can use:
model.setColumnCount(model.getColumnCount() - 2);
which will effectively remove the last two columns from the model.
The other option is to not keep adding columns to the DefaultTableModel, but instead you can just add/remove TableColumns from the TableColumnModel directly. So this would mean the data for the Cholestoral column would always be in the model, but the view would just not display the columns.
So instead of using the model.addColumn(…) you would use the table.addColumn(…) method.
Here am using mouseClicked event to get data on the field while clicking on the table for that i used my code as below
scrollPane.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int rowIndex= table.getSelectedRow();
DefaultTableModel model=(DefaultTableModel) table.getModel();
txt_Product_ID.setText(model.getValueAt(rowIndex,0).toString());
txt_Product_Code.setText(model.getValueAt(rowIndex,1).toString());
txt_Product_Name.setText(model.getValueAt(rowIndex,2).toString());
}
});
Here the problem is when i click on the row or column the data is not appearing on the corresponding fields but appearing when clicking on row or column and clicking on the remaining space available on the table.so double time clicking only producing the result.please help me to solve my problem
scrollPane.addMouseListener(new MouseAdapter() {
Here the problem is when i click on the row or column the data is not appearing on the corresponding fields
Don't add the MouseListener to the scrollPane. The MouseListener should be added to the table, since that is the component you are clicking on.
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 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 created a JTable and I have an xml file with stock symbols and their names.
I want the user to enter the stock symbol in the first column and when he press Enter to see the name of the Stock in the second column.
What is the proper JTable event to use?
Also, is it possible to calculate totals from cells with numeric values?
Thank you.
You can override the TableChanged method of TableModelListener to access the data entered and then make the calculations you need.
public void tableChanged(TableModelEvent e) {
TableModel model = (TableModel)e.getSource();
Object data = model.getValueAt(e.getRow(), e.getColumn());
//do something
}