I'm using TableCellRenderer to render a button in a cell for a JTable created with Matisse in netbeans.
My problem is ... When a double click on the button, I can reach the text field behind. So I want to set the textfield not editable.
For now, my setEnabled are on true: table_watchlistMain.setEnabled(true); I need that because I want to user to be eable to select a row ...
I'm using a DefaultTableModel... do I need to make my own model?
I'm just searching a solution to put the jtable enabled, but not editable. this is possible??
The DefaultTableModel.isCellEditable() method always returns true:
Returns true regardless of parameter values.
So, yes, you should create your own model, for example:
public class MyTableModel extends DefaultTableModel
{
#Override
public boolean isCellEditable(int row, int column)
{
return false;
}
}
Related
Is there a way to update the current DefaultTableModel on a JTable? To clarify what I mean I will use some code examples below.
You might declare a JTable like this
DefaultTableModel model = new DefaultTableModel(tableData, tableHeaders)
{
private static final long serialVersionUID = 8785594035471551113L;
#Override
public boolean isCellEditable(int row, int column)
{
return false;
}
};
JTable table = new JTable(model);
panel.add(table);
I am asking if there is a way to do something similar to this
myTable.java
public class myTable extends JTable {
private static final long serialVersionUID = -5819940358496590055L;
public myTable(TableModel dM) {
setModel(dM);
}
public myTable(Object[][] tableData, Object[] columnHeaders) {
this(new DefaultTableModel(tableData, columnHeaders));
}
public void setEditable(boolean b) {
DefaultTableModel model = (DefaultTableModel) this.getModel();
//Do some code here to make the editable or uneditable
setModel(model);
}
}
someJavaFile.java
JTable table = new myTable(tableData, tableHeaders);
table.setEditable(false);
panel.add(table);
I am not asking you to do this for me but I am asking you to aid me in finding a way to update the current DefaultTableModel.
I appreciate any help
Edit for clarification
I am trying to find a way to manipulate the current DefaultTableModel on a JTable for things such as whether the table is editable or not as there are no methods which support toggling whether the table is editable after the model has been made. There is only isCellEditable().
Why would this be useful?
This would be useful if you were wanting to manipulate the way the table worked with an ActionListener on a JButton or something similar
things such as whether the table is editable or not as there are no methods which support toggling whether the table is editable after the model has been made
You will need to provide a custom TableModel. You can build this functionality into the DefaultTableModel by extending the model and adding a couple of methods.
Or you can check out the Row Table Model.
Among other features it allows you to make the entire model editable or not. You can also control whether individual columns are editable or not.
I have a TreeViewer which has two columns: ProximityClustersColumn: which has names as String, selectionColumn: which has checkbox as shown in the figure
TreeViewer
I have two questions:
On clicking the selection column's checkbox, the corresponding name of ProximityClustersColumn should become editable.
For eg: When I click on the checkbox corresponding to "Studium organisieren-Formelles", the cell "Studium organisieren-Formelles" should become editable.
Also, as seen in the figure, a check must be made such that only one value in the group, whose checkbox is checked becomes editable.
In other words, for each group, only one category name can be checked and that corresponding name should be editable.
For eg:
If you look at the second group, there are two proximity Cluster names, i.e "Infos für Studis" and "Finanzielles im Studium", along with their respective checkboxes. Now, I can choose one among the two names, by selecting the corresponding checkbox. Suppose, I click on the checkbox corresponding to "Infos für Studis", only that cell should become editable.
The main idea is that : I should be able to select only one name from each group and edit it.
I have tried EditingSupport as suggested by #keyur, but the "canEdit" method is not called at all.
My LabelProvider extends ColumnLabelProvider and implements ITableLabelProvider. My ContentProvider implements ITreeContentProvider.
Is there any reason why EditingSupport will fail?
public class ProximityClustersEditingSupport extends EditingSupport{
private TreeViewer viewer;
private CellEditor editor;
public ProximityClustersEditingSupport(ColumnViewer columnViewer, TreeViewer treeViewer) {
super(columnViewer);
this.viewer = treeViewer;
this.editor = new TextCellEditor(treeViewer.getTree());
}
#Override
protected CellEditor getCellEditor(Object element) {
return new TextCellEditor();
}
#Override
protected boolean canEdit(Object element) {
return true;
}
#Override
protected Object getValue(Object element) {
if(element instanceof ProbeSort)
return ((ProximityClusters)element).proximityClusterNames;
return element;
}
#Override
protected void setValue(Object element, Object value) {
if (element instanceof ProbeSort)
{
((ProximityClusters)element).setProximityClusterNames(String.valueOf(value));
}
viewer.update(element, null);
}
}
I think I need more information to answer it completely. First of all is that Treeviewer or Tableviewer. To me it looks like TableViewer.
What is the expected behavior of the cell when you mean editable. Those cells which are not editable should have disable type foreground colored text and the one which is editable can have normal foreground color say black. Only when user changes to focus (if tab is supported) or by clicking on the cell it is better to show the editable text where user can select the text and change/edit it. And pressing Enter key or Tab Key program can accept the change. Is that what you are looking for?
I guess I didnt get the question. Can you give example from the above figure. Like what is group?
I think EditingSupport will be useful for you.
You can create your concrete EditingSupport class and assign it to your column. It has method "canEdit" through which you can control the editing dynamically.
So what you have to do is to store the boolean flag in the model from checkbox status or read the check box status directly and return false/true value , which will enable/disable editing.
The program in the link shows all possible Tree viewer related implementations. Copy paste the program into a new java class and run as java application. This one example solved all tree related issues i had in my implementation.
So i am working with a JTable, It has Columns A-K. with A and B being the only editable ones. If someone edits an empty row in A, I make an API call to get B then i make a DB call to get all rows where B exists.If someone edits an empty row in B, i make the same call as the will be retrieved from the DB for that row as well. The call returns 0-N rows. If 0 rows were returned, I change the values of all row except B to N/A otherwise i populate the rows using the data.Once populated, i make all columns non-editable. The DB call occurs in its own thread as once the call is return i create my own record object which I add to the tablemodel.
I have my own TableModel and a TableModelListener to keep the data and handle changes in values.
Here is my issue. I am using TableCellRenderer and using the cellrenderer to see if the value was changed, if so then i make the calls and populate as needed. When a large number of rows is being pulled from DB, it takes a while to load and making all that records so I tried to use a ProgressBar to show the user that the screen isn't just frozen, it is progressing and by how much. However the frame that comes up is blank and nothing gets displayed. I get the feeling i am doing something either improperly or missing something.Any help much appreciated.
some code to understand what i am talking about
public class MyPanel extends JPanel {
private JTable myTable;
private MyTableModel tm;
//OTHER FIELDS
public static void createPanel() {
tm = new MyTableModel(columnnames);
myTable = new JTable(tm);
TableColumn account = myTable.getColumnModel().getColumn(
MyTableModel.ACCOUNT_INDEX);
account.setCellRenderer(new MyTableRenderer(
MyTableModel.ACCOUNT_INDEX));
}
}
public class MyTableRenderer extends DefaultTableCellRenderer{
protected int interactiveColumn;
public MyTableRenderer(int interactiveColumn) {
this.interactiveColumn = interactiveColumn;
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
if (column == interactiveColumn && hasFocus) {
//DO DB and API CALLS HERE
//IF DB CALL DISPLAY A NEW FRAME WITH PROGRESSBAR
}
return c;
}
}
Sorry for formatting issues
Use SwingWorker, which allows you to update your TableModel as you examine your result set.
Addendum: Don't try to update the TableModel from the renderer. You can update the model when your implementation of CellEditor has concluded, by starting a suitable worker in getCellEditorValue(). In that way, the revised data will be available when the renderer is next invoked for any modified cell(s). This related example outlines the approach.
Addendum: getCellEditorValue() is invoked after editing has concluded, but starting the worker in setValueAt() offers more reliable access to the target row and column.
I have a custom TableCellRenderer (ValueRenderer) for a JTable, the cell is a Checkbox.
I have attached an ItemListener to the valueRenderer to listen to the checkbox's state change (selected/deselected) as mentioned by this example.
My problem is that inside the itemStateChanged(ItemEvent e), I do not know how to get the row in which this checkbox is contained knowing that the ItemEvent source is the ValueRenderer.
Can you help me?
Here is some of my code:
Custom TableCellRender:
public class ValueRenderer extends JCheckBox implements TableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
this.setSelected((Boolean) value);
return this;
}
}
ItemListener:
public class TableRowCheckBoxListener implements ItemListener {
private JTable hqlRequestTable;
public TableRowCheckBoxListener(JTable hqlRequestTable) {
this.hqlRequestTable = hqlRequestTable;
}
#Override
public void itemStateChanged(ItemEvent e) {
/*How do I get the row which contains the checkbox clicked knowing that :
e.getSource() == ValueRenderer
e.getItem() == ValueRender
*/
}
}
If you want to know when some value changes in your table, you must not register a listener on the renderer. You must register a listener on the table model: that's where the data displayed by the table is held, and that's the object which fires an event if anything changes in the data.
The alternative is to use a custom table model consisting in a list of beans, have the table model modify the properties of the beans it holds, and have the bean fire a property change event when a property changes. You'll then register listeners on the beans themselves rather than registering a table model listener (note that the table model still has to fire table model events, though).
I have a JTable with 3 columns. One on the columns must have an integer value and I must validate the input before the cell loses its focus.
I've used cell editor and overrided the stopCellEditing() function. I've wrote the validation in stopCellEditing and it does keep the focus on the cell but but my problem is this:
the column cannot accept more than one value (if I move to editing another cell the content disappears!!)
I cannot hit enter if I'm editing one of the cells in this column!
this is my editor class:
public class MyEditor extends DefaultCellEditor implements TableCellEditor {
public MyEditor() {
super(new JTextField());
}
#Override
public boolean stopCellEditing() {
Object obj = delegate.getCellEditorValue();
if (obj is not an integer) {
return false;
}
return true;
}
and this is how I'm using in in my Frame:
studentsTable.getColumnModel().getColumn(2).setCellEditor(new
MyEditor());
Plz help me :)
One on the columns must have an integer value
No need to write a custom editor for this.
All you need to do is override the getColumnClass() method of JTable or your TableModel and the table will use the supplied Integer editor.
Regarding the code you posted:
It won't compile since your if condition is not valid. We want real code so we can spot possible logic errors. The code should also be posted in the form of an SSCCE.
There is no need to reference the delegate variable. Just invoke the getCellEditor() method directly.
Don't know if it makes a difference bu when I override stopCellEditing(), instead of returning true I use:
return super.stopCellEditing();