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.
Related
First of all thanks for your support. I'm creating an application with MySQL database to store User details (Names, Age..). Just want to send the Name value to a JTable cell clicked by the user.
I'm able to store the value in a String, and show it in System.out.println, but cannot fill the cell up with the value.
Is this possible any how?
I'm able to store the value in a String, and show it in System.out.println, but cannot fill the cell up with the value.
It is unclear how do you get this String and what have you tried but you can use setValueAt(...) method to set this String as the selected cell's value. In order to get the selected cell row and column indexes you should use getSelectedRow() and getSelectedColumn() methods, respectively. Finally to detect user's "click" you have to attach a MouseListener to the table or a ListSelectionListener to the table's ListSelectionModel to listen for selection changes.
See the API:
JTable#setValueAt(Object value, int rowIndex, int columnIndex
JTable#getSelectedRow()
JTable#getSelectedColumn()
See also:
How to Use Tables tutorial.
I have JTable object, filled with data provided from my implementation of AbstractTableModel. I have mouse event listener and when I click on some cell I get its row and column position. With this values I call getValueAt(int row, int column) method from TableModel and I get my data. The problem is the data in the table can be sorted when I click on column name. When again I try to call getValueAt() I get the old value
Example:
col1|col2
val11|val21
val12|val22
When I click on col1
values are:
col1|col2
val12|val22
val11|val21
The I call getValueAt(0,0) and still get val11, and I should get val12. How can I fix this ? Thanks!
The row index in the view (that you probably get from your mouse listener - if you posted the code as requested, we would know instead of having to guess) is not the same as the row index in the model, once the table is sorted. To convert from the view index to the model index, use convertRowIndexToModel().
Note that the same care must be taken for columns, as the user can reorder them by drag and dropping them.
I've manage to do it. table.getValueAt() should be called direct to the table object. My previous attempt was table.getModel().getValueAt().
Here i have an 1-10 row is listed in jtable i want to delete/hide the 5th row before it listed in jtable.
i set the rowheight but it affected the cellselection.Is there any way to hide/delete the row without affected the normal flow code?
If i remove the row it will throws ArrayIndexoutofBoundException.
in my project executed means one gui open in that gui listed the some string. In here we can add the more string via Add Button on popup Button
Here what i need is i have to hide the particular string. That string is placed on 1st row.
i need to hide the string from end user.
now u hope understand.
You can use the JTable row filtering support in order to hide certain rows without deleting them from the model. Also see this: How can I filter rows in a JTable?
You can eliminate rows in the table by calling the removeRow() method. If you want to just hide it instead of elimintaing it you need to customize the JTable's model to meet your specs on what to display.
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
using DefaultTableModel with JTable you should be able to use model.removeRow(int row) function to remove A row from JTable. There is no way to hide a row based on index as much as i know. However, If you need to hide and re-show mechanism you need to save the row prior to delete it and Save the removedRow in a ArrayList to re-use them.. Something as follows:
List<Vector>deletedRows = new ArrayList<>();
Vector removingRow = (Vector) model.getDataVector().get(5);
deletedRows.add(removingRow);
model.removeRow(5);
I'm developing a Java Swing application which contains a JTable. By default, while launching the application for the first time, the call to the method jtable.getSelectedRow() or jtable.getSelectedColumn() returns -1, which means that no row selected at that moment. After user clicked any row or column, the call to the method jtable.getSelectedRow() or jtable.getSelectedColumn() returns the appropriate values of selected rows & columns. What I actually need is that I want to set the selected row or column to -1 i.e. "no row or column selected state". How can I do this?
The JTable method clearSelection will do what you want -- clear the selection of all the table's selected rows and columns. The JTable API is the place to look for methods such as these.
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.