JTable - Checkbox add action listener - java

I've created a simple JTable with check box like below:
DefaultTableModel model = new DefaultTableModel();
jTable1.setModel(model);
model.addColumn("No:", no1);
model.addColumn("Remark", remark1);
model.addColumn("Color", colors1);
model.addColumn("Done");
TableColumn col1 = jTable1.getColumnModel().getColumn(0);
col1.setPreferredWidth(1);
TableColumn col4 = jTable1.getColumnModel().getColumn(3);
col4.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
col4.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));
col4.setPreferredWidth(50);
jTable1.setShowGrid(true);
jTable1.setGridColor(Color.BLACK);
jTable1.setAutoCreateRowSorter(true);
It's working fine but how to do if I want to add action listener for the check box. For an example, when my check box is checked I need to pop up a confirmation message.

For an example, when my check box is checked I need to pop up a
confirmation message.
You don't need to add an ActionListener to the renderers/editors but you need to listen to table model data changes. Take a look to Listening for Data Changes section of How to Use Tables tutorial:
Add a new TableModelListener to your TableModel
Validate if the updated cell value is a Boolean and its value is true.
Ask the user to confirm the update. If s/he doesn't then set the cell's value back to false.
See TableModelEvent API as well.
Edit
Note in this case as you're working with booleans then there's 2 possible values to do the check. However for input validation in other cases the described procedure won't work simply because the listener will be notified when the change already happened and you won't be able to set the value back just because it won't be there any longer.
Take a look to #kleopatra's answer to this question: JTable Input Verifier. As stated there a better approach is providing a custom CellEditor and do the validation in stopCellEditing() method implementation. Just as a suggestion I'd use a DefaultCellEditor that takes a JCheckBox as parameter and override the aforementioned method.

Related

Getting modified values from JTable

I have a JTable with the required values. After editing a cell, if I use table.getvalue(row,column), I get the previous unaltered values. But if I select another cell before clicking the save button I get the modified values. Can anyone help me to remove this problem??
PS: I have not yet added any actionlisteners for the table
The default update mechanism only changes the model when the cell editor loses the focus. Either tabbing out of the cell or clicking in a different cell will cause the vital "focus lost" event which triggers the model change
You could add an ActionListener (see http://download.oracle.com/javase/tutorial/uiswing/components/textfield.html). It will get triggered when you press RETURN. In the handler, call fireEditingStopped() to trigger the "copy to model" code (see http://download.oracle.com/javase/tutorial/uiswing/components/table.html#editor).
or add following code to your table,
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Try TableModel.
Every table object uses a table model object to manage the actual table data. A table model object must implement the TableModel interface. If the programmer does not provide a table model object, JTable automatically creates an instance of DefaultTableModel.
A table model can have a set of listeners that are notified whenever the table data changes. Listeners are instances of TableModelListener.
Have you tried this
int row=table.getSelectedRow();
int column=table.getSelectedColumn();
table.getValue(row,Column)
If Yes you need to use TableModel

How to make filterable to jtable record using netbeans ide

There are many rows on above showing jtable. I want to filter record by short code. If I type short code like 1234 it should be display only short code 1234 associated row on jtable.
Thanks
You're going to have write the code...Start by checking out how to use tables, in particular sorting and filtering
The basic requirement would be to attach an ActionListener to both the field and button (you can do this from the form editor if you wish).
Within the actionPerformed event handler method, you need to create a RowFilter and apply it to the tables RowSorter.
A table can be configured to automatically create a row sorter by setting the autoCreateRowSorter property to true.
It's all explained nicely in the linked tutorials...
And another example

JTable cell listener?

I am using a JTable. I need to get a notification whenever a cell
selection change. I tried to use the ListSelectionListener but
I only get notification when the row selection change. If I select a
new column on the same row, I don't get notify. I need to know when
the cell is selected, not when the cell is changed. Is there a listener that I can use to do this ?
The easiest way to do this is to call setCellSelectionEnabled(true), and pass a reference to your table to the listener. When the listener is invoked, call getSelectedRow() and getSelectedColumn() on the original table.
The alternative is to set a row selection listener on the table, a column selection listener on the ColumnModel, and then figure out their intersection.
One way to receive notification on column selection changes - as already answered by #parsifal(in the comments - is to grab the TableColumnModel's internal selectionModel and register a listener:
table.getColumnModel().getSelectionModel().addListSelectionListener(selectionListener);
Another way is to register a TableColumnModelListener with the columnModel:
table.getColumnModel().addColumnModelListener(columnModelListener);
The first is "shorter" in terms of code: just one method to implement vs. several - most empty except the columnSelectionChanged.
The second is more robust against dynamic changes: with the first there is no possibility to guard against changes of the selectionModel property of the columnModel ... because it is not a property. Or in other words: in the (concededly rare) case that application code swaps out the selectionModel the listener is listening to the Void. Installing a columnModelListener is immune against such a change, as the columnModel passes on the events from its selectionModel whichever it would be.

Use of mouselisteners in a jTable

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.

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.

Categories