Disable particular rows of a JTable - java

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

Related

JTable how to render different swing objects in the same column

I want to render different swing components in the same JTable column. For example, I want to have few different comboboxes, jlabels and jcheckboxes in the same column. http://docs.oracle.com/javase/tutorial/uiswing/components/table.html provides information how to render only one type of combobox per column, however it is not enough.
How can I setup table cell renderer so that it would achieve this functionality?
If you need to use different editors/renderers in the same column, you can follow the approach described in this answer and override JTable#getCellEditor() based on the cell (column and row intersection). JTable#getCellRenderer() can be overriden if needed as well.
In Concepts: Editors and Renderers is described the strategy followed by tables to get a renderer/editor so you can take advantage of it to solve your problem.

How to restrain checking single checkbox in multiple columns in Jtable

I am trying to implement a Jtable which includes three check-box tables like this:
Can you tell me how to set a single selection group of checkboxes which only allows 1 selected check-box in a single row at any time?
I know of nothing out-of-the-box for doing this. I´d have a TableModelListener check these columns every time a change is made and call setValueAt on the checkboxes as needed.

JCheckBox to display and perform functionality in JList

I am trying to get a JCheckBox to display on a line that is in the multiple select JList and still perform its functionality.
Right now if I add the JCheckBox as an element it just prints its toString format.
Help/ideas?
If you haven't already done so, you'll need to write a custom ListCellRenderer, as discussed in Writing a Custom Cell Renderer.
Addendum: Because you'll also need an editor to handle the checkbox state, you may find it easier to use a one column JTable, as discussed in How to Use Tables. Note that a data column having the type Boolean will be automatically rendered with a check box.

JTable onchange event

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.

How to change data in JTable cells?

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.

Categories