How can we show/hide some columns in JTable?
I recommend JXTable from the SwingX project, hiding columns in the view is very easy:
table.getColumnExt(index).setVisible(false);
JXTable also provides a column control(menu in top-right corner) where user by them selfs can hide/show columns.
When using JXTable from the SwingX project, as suggested by Uhlen,
It's better to use
table.getColumnExt("columnName").setVisible(true);
table.getColumnExt("columnName").setVisible(false);
the index when using getColumnExt(index)is the column index that is in the view (i.e.visible)
Once you set a column invisible, you can't access to it by getColumnExt(index)
Instead of having to loop through the list for every cell call as shown at codeGuru, you could change the columnModel to switch the column-visibility by setting from/to zero-width and setting editable/noneditable. That wat default handling skips over that column.
You can find a sample at codeguru:
http://www.codeguru.com/java/articles/660.shtml
Don't forget that google is your best friend ...
Related
I want to be able to do two things:
set colors of rows based on index, so first row is red, second blue,
third green
be able to set color of columns also based on something, be it index
or their names etc, whatever is possible.
I do not need to detect selection change or anything. Could someone tell me how to do that? What methods would help etc? In case the title wasn't read, this is regarding DefaultTableModel in JTables.
set colors of rows based on index,
Table Row Rendering might give you some ideas.
be able to set color of columns also based on something
You can provide a custom render for any column. Then you can add you logic to color the column based on something. Read the JTable API and follow the link to the Swing tutorial on How to Use Table and you will find a section on creating a custom renderer.
How can I add "row headers" to a Jface TableViewer?
Have a look at the Grid widget from the Nebula Project.
It does support "row headers".
A TableViewer does not by default provide this capability. But you can have a look at this snippet to achieve something similar to a row header. This example uses two tables to make the first column fixed and rest of the columns horizontally scrollable. If you do not want fixed columns than you can just use different colors for columns to achieve the look and feel of a row header.
I would like to "grey out" particular rows of a JTable so that they may not be selected by any means. The other rows should still be selectable. How do I accomplish this?
You can either override JTable.changeSelection() to deselect the offending row whenever it's selected, or provide your table with a custom ListSelectionModel where you override setSelectionInterval(), addSelectionInterval(), etc. to prevent the row from being selected in the first place.
You will want to create a custom TableCellRenderer, one that will display "disabled" information greyed out. Read the Swing Table Tutorial for more on how to create these renderers, especially the section, Concepts: Editors and Renderers.
Create a temporary TableModel which has only the rows that you want to select. After the selection made and when you want to revert, change back to original TableModel
I have too many columns in a table to display them all at once, and would like to let the user change which columns are visible. How can I do this?
note: It is easy to make the application select columns at runtime. What I am asking is what UI element(s) to add to allow the user to hide/unhide columns at runtime.
If you can import some external libraries, you could have a look to
http://swinglabs.org/docs/components/JXTable/tutorial.jsp which supports such runtime modifications.
Table Column Manager allows the user to right click on the table header to control which columns are visible.
There isn't a standard way, however what you could do is something like this:
Use a custom table header render component to install additional actions/UI on the column headers (e.g. through a context menu of checkboxes)
Add a custom model which you can re-configure to display different items depending on what the user selected through additional actions on the column headers
Do the event wiring/plumbing.
Alternatively: find a custom component that does this. There probably is something out there already: projects like the component library from JIDE would be a good place to look.
Use TableModel.addColumn(TableColumn) and TableModel.removeColumn(TableColumn) methods to show/hide columns on-the-fly.
You can attach that calls to any other GUI components (for example, make a JPanel or a JTable with a few checkboxes).
Either display a popup menu with the possible columns when user right-clicks the header or implement a small (and light) popup dialog with a checkbox list for selecting the visible columns. The dialog can be opened by right-clicking, by clicking a toolbar button or from a toolbar menu.
How should I disable just a single column from dragging in JTable? I want to allow other columns from dragging but just the first column (indexed at 0). Thanks.
You should be able to create your own TableColumnModel (extend DefaultTableColumnModel) and to override moveColumn to only call super.moveColumn when the column is allowed to be dragged.
EDIT: Have a look at this post first.
Not quite on point for you ....found on another blog
yourJTable.getTableHeader().setReorderingAllowed(false)
this gets the entire table not to reorder columns which is what I needed. Exploring the Override is the correct route