1

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)?

Grice
  • 1,345
  • 11
  • 24

3 Answers3

1

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(...);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • from what you said I concluded that "table.removeColumn( table.getColumn(...) );" will remove the column from the view and you said "to access the data you use: table.getModel().getValueAt(...); can i get the value of a cell in the hidden column by this code although it is not visible,for example suppose the hidden column is in the index 3 and i want to get the value at (2,3) can i get it although this cell is not visible ,i hope it is clear – mohamed_elsharkawey Sep 22 '14 at 20:37
0

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.

Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58
-2

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.

Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
  • 1
    -1, `Why don't you just let the dataModel control what is shown?` The Model holds the data. The View controls how the data is displayed. So any change should be made to the View, not the Model. This is the basics of Swing's MVC design. `All you have to do is change the getValueAt(..) method so that it does not return a value` Then that is what you get whether you are trying to render the data in the table, or just access the data by other code in your application. – camickr Sep 22 '14 at 18:56
  • yes the model holds 'what' data, the view controls 'how'. For me it is more a question of 'what' than 'how'. If I have a customer entity, and only ever want to show name, address, but not telephone, then I would just have the getValueAt() return 'name' and 'address'. For me a renderer deals with the visual representation of the data. – Oliver Watkins Sep 23 '14 at 06:51
  • `and only ever want to show name, address, but not telephone, then I would just have the getValueAt() return 'name' and 'address'` - In general I would agree, but the requirement is that the "telephone" needs to be used for other processing in the application. – camickr Sep 23 '14 at 15:19