I made a column editable in Jtable.
I want old values from a cell when I have finished editing a cell
You can get the value by using
table.getModel().getValueAt(row_index, col_index);
where table is the name of the table and it will return an Object
Go through this Getting cell value. It may be useful for you.
You can use a TableCellListener, like they show here. It uses a PropertyChangeEvent to keep track of the old and new values.
You also could create your own implementation of a TableModel and override the setValueAt method to keep track of the changes.
Add a TableModelListener to your TableModel. Whenever an event fires you can updated the contents of your text field with the new value in the cell.
Related
I want to hide data of a column of Jtable, but not hide the column view,just its data.
The column contain data about profit and the customer doesn't want to show the profit but in my code I use the values of this column and get it when the user select specified row.
How do I achieve the customer need and still be able to get the values of this column when selecting it after hiding its data(displaying column as empty but still has its values)?
You need to remove the TableColumn from the TableColumnModel of the JTable. For example:
table.removeColumn( table.getColumn(...) );
Now the column will not display in the table, but the data is still available in the TableModel. to access the data you use:
table.getModel().getValueAt(...);
You could try a subclassing DefaultTableCellRenderer, and call setCellRenderer. When the relevant column number is passed to getTableCellRendererComponent, return a blank JLabel, otherwise call the default super.getTableCellRendererComponent.
This should mean the column is still visible, but each cell will be blank.
If you want to display the data when a row is selected, you will need add a listener to the selection model (from getSelectionModel) to store the selected row in a variable, and call a repaint. You can then use this value in your CellRenderer.
Why don't you just let the dataModel control what is shown? I'm assuming you are using a tableModel.
All you have to do is change the getValueAt(..) method so that it does not return a value for the column you do not want to show. Make sure that getColumnCount() method is reduced by one as well.
I am trying to change the row color based on the last column's value
Following is the code used:
PaginationUI pageUI =new PaginationUI();
this.dialog = new JDialog(jFrame, csvName, true);
pageUI.setCurrentPageIndex(1);
pageUI.setItemsPerPage(itemsPerPage);
pageUI.setHeader(header);
pageUI.setData(data);
pageUI.paintPaginatedData();
Plz help. I need to check the last value of the "data" Arraylist. Depending on that if value is "abc" ,need to change the value of that row.
I went through
Change the background color of a row in a JTable
and did most of the things suggested therein, except the MyTableCellRenderer, which I did not understand where that is implemented.
i am trying to change the row color based on the last column's value
See Table Row Rendering for one solution that works by overriding the prepareRenderer(...) method of the JTable, so you don't need to provide a custom renderer for each data type in the table.
I need to check the last value of the "data" Arraylist
No, you should NOT be referencing the ArrayList. You should only ever reference the data in the TableModel.
and did most of the things suggested therein
Then it should work. The problem is with your code. Post your SSCCE that demonstrates your problem if the above solution doesn't help.
I have a JTable with the required values. After editing a cell, if I use table.getvalue(row,column), I get the previous unaltered values. But if I select another cell before clicking the save button I get the modified values. Can anyone help me to remove this problem??
PS: I have not yet added any actionlisteners for the table
The default update mechanism only changes the model when the cell editor loses the focus. Either tabbing out of the cell or clicking in a different cell will cause the vital "focus lost" event which triggers the model change
You could add an ActionListener (see http://download.oracle.com/javase/tutorial/uiswing/components/textfield.html). It will get triggered when you press RETURN. In the handler, call fireEditingStopped() to trigger the "copy to model" code (see http://download.oracle.com/javase/tutorial/uiswing/components/table.html#editor).
or add following code to your table,
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Try TableModel.
Every table object uses a table model object to manage the actual table data. A table model object must implement the TableModel interface. If the programmer does not provide a table model object, JTable automatically creates an instance of DefaultTableModel.
A table model can have a set of listeners that are notified whenever the table data changes. Listeners are instances of TableModelListener.
Have you tried this
int row=table.getSelectedRow();
int column=table.getSelectedColumn();
table.getValue(row,Column)
If Yes you need to use TableModel
Is there any way to detect a cell selection change in a JTable? I've found documentation for detecting a row change using ListSelectionListener but it doesn't seam to work when changing selection on the same row. I'm using JTable to render a simple schedule.
Maybe I should use a different component?
No, the right component for showing tabular data is JTable.
You want to add a listener to the TableModel that's underneath the table. That will fire off events whenever data changes. You get it out of JTable, unsurprisingly enough, by calling getTableModel().
Update
Oh wait, I think I misunderstood you. You're not interested in data changes but column selection changes.
JTable has a method called columnSelectionChanged; its documentation says it's called by TableColumnModelListener, which leads me to believe that what you want to do is getColumnModel() and use the addColumnModelListener() method of that to listen for column selection changes.
I can set data in JTable constructor, and then user can change this data when program is running manually(typing from keyboard).
But what method should I use in case I want to change data in some column? To change column header I use TableColumn method setHeaderValue. What should I use to set value in JTable cell?
If you want to allow users to edit the data, then you need to set a TableCellEditor on the cells that you want people to edit. You probably also want to start using a TableModel instead of hard coding the data in the JTable itself.
See http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
While creating the JTable you first need to specify that the values of particular column are editable. You can obviously also provide the row basis edit functionality. but all these things you should define while ccreating the table itself. Please reply if you need any help on this.