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
Related
In three of the columns of my JTable that I have set up I would like to have a list in each cell of the column. I'm not sure where to start on possibly creating a custom cell renderer class if that's the best option? My goal is to list the group names each on their own line in each cell, and expand the cell height as new lines are added. Each group will have an AdmType and Admitted entry, so I will also need to figure out how to add another checkbox to the Admitted column cell for every new Group entry.
This below solution did not work for me, but it clearly worked for the person who posted a screen shot of their success. My problems may be due to how I have my TableModel set up.
https://stackoverflow.com/a/32793088/6867420
I have some JDialogs displaying JTables.
When the header columns are clicked a sort occurs on that column.
My question is : how can I know when a column header has been clicked and thus made a sort active.
When the sort is active, I know I should user the .convertRowIndexToModel method.
But how do I detect that a column is sorting in order not to mess the correct index if no sort is active?
Generally speaking, you should ALWAYS uses the convertRowIndexToModel when you take an index value from the view (JTable) and try and look up some value within the model. The JTable does this automatically when you use it's methods, but incase you're not, you need to take care of it yourself.
There's no need to know if the view is sorted or not...
If you "really" want to know when a table is sorted, you could attach a RowSorterListener to the TableRowSorter used by the table.
You could also use the TableRowSorter#getSortKeys to see which columns are included in the sort...
I have used it for my selection table. When the auto order is activated (setAutoCreateRowSorter(true))
the indices of the model table and the visual change, so you have to tell it to look for it within the model with respect to the one you are seeing.
((CustomTable)form.getAvailListView().getModel()).data.get(form.getListView().convertRowIndexToModel(i))
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.
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 a jTable with columns 'Job_no' and 'Status'with values such as:
Job_no Status
1 Active
2 Pending
3 Pending
I would like it so that if a user clicks on a Status say in this case the first 'Pending'(where Job_no = 2) an inputDialog pops up allowing the user to change the status of the cell clicked-how can I do this? Bear in mind you will also have to retrieve the Job_no(that corresponds to that status) somehow, and that, though I'm OK with JOptionPane's, I'm new to JTables. I'm using JDBC(mySQL) and have a table 'Jobs' which amongst other things, has column Job_no and status.
Thanks for your help.
You don't do that by using a mouse listener and a popup, you just make the cells editable, and perhaps set a custom TableCellEditor. Take a look at the Java Tutorial for more details.
1) add a MouseListener to the JTable
2) Read the JTable API for the methods that will convert a mouse point to a row/column
3) Now that you know the row/column you can use the getValueAt(...) method to query the data in the Job_no column
4) Then you can change the status of the selected cell using the setValueAt(...) method.
So you break the problem down one step at a time.