I have a column in JTable that binds to the underlying boolean property on a list of business objects. I also have a combobox, which should select which items should be selected. I basically added the following code as a handler to the combobox:
macroCombo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
Predicate filter = (Predicate) comboBox.getSelectedItem();
for(SelectableKey key : tableEntries){
key.setSelected(filter.evaluate(key));
}
}
});
I also have a few other controls I want to change based on the value. At the moment, only a few cells in the table change their state to be selected/deselected. Only when I click on the row, or select multiple rows, the UI updates itself. Is there a call from the handler I need to make to tell GUI to redraw itself? ALos, if I modify other controls than JTable, how would I tell them to change their state?
Thanks
When you update a value in your TableModel, the model should fire a corresponding TableModelEvent (type: UPDATE).
If your TableModel for example extends from AbstractTableModel, you can call the fireTableRowsUpdated method after you have made the changes.
Another approach is a TableModel which knows when it gets updated (for example by adding listeners to the objects it contains). This allows other code to simply update the objects contained in the TableModel, without having knowledge of the TableModel. The TableModel itself will then fire the event when it detects changes made to the objects it contains.
I prefer the second approach, as this avoids that I have to pass that TableModel around to all my other classes.
Consult the table tutorial for more information.
Related
I want to be able to write a statement that manually selects an item in a JList, such as:
JList myList = new JList(items);
myList.selectValueAt(index);
Documentation of JList:
The selection state of a JList is managed by another separate model, an instance of ListSelectionModel. JList is initialized with a selection model on construction, and also contains methods to query or set this selection model. Additionally, JList provides convenient methods for easily managing the selection. These methods, such as setSelectedIndex and getSelectedValue, are cover methods that take care of the details of interacting with the selection model. By default, JList's selection model is configured to allow any combination of items to be selected at a time; selection mode MULTIPLE_INTERVAL_SELECTION. The selection mode can be changed on the selection model directly, or via JList's cover method. Responsibility for updating the selection model in response to user gestures lies with the list's ListUI.
In your case:
myList.setSelectionIndex(index);
You need to get the ListSelectionModel from the JList. With that, you can modify the selection:
...
ListSelectionModel sm = myList.getSelectionModel();
sm.clearSelection(); // clears the selection
sm.setSelectionInterval(index, index); // Sets a selection interval
...
While the ListSelectionModel allows fine grained control over the selection behaviour of the JList, JList itself also provides convenient methods like JList.setSelectedIndex() to simply modify the selected elements.
You can use
jList.setSelectedValue(string, rootPaneCheckingEnabled)
or
jList.setSelectedIndex(index);
Hej.
There is a collection with data.
static ArrayList<MyBeans> all = new ArrayList<MyBeans>();
There is a jTableModel too which invoke this table on jFrame.
TableModel model = new Table(all);
JTable table = new JTable(model);
For example, at some moment ArrayList<MyBeans> all was changed. What should do for change this table too?
If your all ArrayList<MyBeans> (which by the way should not be static) is the nucleus of a TableModel, then your problem is that you're trying to make changes directly to the ArrayList which you should never do. Instead you should make changes only by calling public methods in your TableModel, and these methods should fire the appropriate AbstractTableModel notification method that will notify any registered listeners (here, the displayed JTable itself) of any changes to its model.
I'm starting to learn how implement MVC pattern in swing. I have a confusion when in comes to the data in model.
In the application that I'm doing, I have several JTextFields. The data set in the fields come from JTable. I have a ListSelectionListener in my controller added to that jtable (in the view) so that when selection has changed, the data from the selected row will be reflected to the respective textfields
public void transferTableDataToFields(){
if(tblProduct.getSelectedRows().length != 0){
int selRow = tblProduct.getSelectedRow();
txtID.setText(tblProduct.getValueAt(selRow, 0).toString());
txtName.setText(tblProduct.getValueAt(selRow, 1).toString());
txtDescIn.setText(tblProduct.getValueAt(selRow, 2).toString());
txtSupplier.setText(tblProduct.getValueAt(selRow, 4).toString());
txtPrice.setText(tblProduct.getValueAt(selRow, 5).toString());
}
}
My question is, am I doing it the right way? Should I define fields in the model that corresponds to the textfields in my view, then change my code to controller setting the model's fields to values from view's jtable then let the model fire a property change notification to its listener, then let the listener call the update method in the view based on the property change event?
My problem with this approach is that, the view should have a method that will return the row selected from the jtable which I find a bit ugly.
EDIT
#trashgod this is what I'm saying.
Inside my TableModel implementation, is it better to populate the data here?
public MyTableModel extends AbstractTableModel{
List<Row> data;
Row header;
public MyTableModel(){
initializeData();
}
public void initializeData{
//query database here then put it in the list
}
//other methods to implement e.g. getvalueAt(int x, inty){data.get(x).get(y);}
}
Is it better if I make it like this? because currently I stored and populate the data of my table in my main model (also containing fields with corresponding textfields in the SelectedRow view) then I pass the data in my JTable's model.
In this case, the current selection is a property of the view, an instance of JTable, not the model, an implementation of TableModel.
If you choose to supersede editing in the JTable itself, a ListSelectionListener is the correct way to update your (anonymous) dependent view, e.g. SelectedRow. Your controller should have little to do except add SelectedRow as a listener to your JTable. As SelectedRow contains (presumably) editable fields, you are responsible for several things:
Propagate any changes back to the original table's TableModel, typically via setValueAt().
Convert between view and model coordinates, mentioned here.
Preclude (or synchronize via the ListSelectionListener) editing in the source JTable.
Addendum: Inside my TableModel implementation, is it better to populate the data here?
The answer depends on the application. Your TableModel implementation should expose a public API that provides as much (or as little) as required to support the application's requirements for concurrency and latency. For reference, DefaultTableModel is a general purpose example, although you'll want to use something more recent than Vector internally.
In my form I have a JTable with a TableModelListener. The tableChanged method updates the logic on my JButtons. This all works correctly. When a user edits a value in a cell in the jtable - the tableChanged method executes and the buttons are refreshed accordingly.
The problem I am having and it is a show stopper. The JTable displays objects and some attributes of the object. The user selects the objects from the application.
If I there is a object selected and being displayed in the jtable. If that user changes a attribute value in the application and not edit it in the JTable. The jtable is still refreshed and the changed value is displayed. But a TableModelEvent is not taking place and my button logic is never refreshed.
I have looked at TableCellListeners - but that is still looking for a edit in the table. So I don't think that will work here.
How can I tell that something has changed and the Table has been updated without a Event taking place in the jtable itself?
EDIT: Placing some of the jtable code
This is in my base dialog class
selectTable = new JTable(SingletonSelectTable.getInstance());
selectTable.getModel().addTableModelListener(this);
selectTable.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Font font = comp.getFont();
if (SingletonSelectTable.getInstance().isCellBold(row, column) == true){
comp.setFont(font.deriveFont(Font.BOLD));
}
return comp;
}
});
selectTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
selectTable.setCellSelectionEnabled(false);
selectTable.setRowSelectionAllowed(true);
selectTable.setColumnSelectionAllowed(false);
JScrollPane ScrollPane = new JScrollPane(selectTable);
the tableChanged method
public void tableChanged(TableModelEvent e) {
setRemoveButtonVisibility();
setRemoveAllButtonVisibility();
setCommentButtonVisibility();
setOKButtonEnabledStatus();
}
my singleton class
public class SingletonSelectTable extends AbstractTableModel {
I hope this helps
How can I tell that something has changed and the Table has been updated without a Event taking place in the jtable itself?
There is never an event in the JTable. It is the underlying TableModel that changes, and it is the TableModel that fires an event.
The JTable registers a listener to the TableModel so it knows when it should update the displayed table content.
So if you are saying that the JTable gets updated (automatically, so without you scrolling/selecting/hovering/...) means that the TableModel does fire an event, meaning you can also listen for it.
In the scenario an element of your TableModel is changed in your application, something should fire the TableModelEvent from the TableModel. Typically this is done by either letting the TableModel listen for changes in the objects it contains, or letting the code that changes the object also notify the TableModel that the underlying data has been changed. Either way, the TableModel needs to fire an event, so there is no reason why your listener would not receive it, unless it is simply not fired (meaning an incorrect TableModel).
My best guess, based on your comments, is that you have an incorrect implementation of your TableModel and that your TableModel simply does not fire an event, and that the JTable gets updated 'by accident' (in my experience is a JTable rather robust for missing events, certainly when no rows are added/removed ... definetely compared to a JTree)
The getValueAt() method picks up the change and then updates the jtable
This really sounds incorrect. The getValueAt() method is normally called by the JTable after it receives an event. Thanks to the event, the JTable knows it must be updated so it queries the model for the new data. So the getValueAt method does not pick up the change, but gets called as a result of the change.
I would strongly suggest to take a look at the Swing table tutorial, and certainly the sections about Creating a table model, Listening for data changes and Firing data change events
I've implemented my own event handler and added it to the selection model of the table:
table.getSelectionModel().addListSelectionListener(event);
And implemented the method for "event" (mentioned above):
public void valueChanged(ListSelectionEvent e) {
log.debug("value changed");
}
Unfortunately the event fires twice if I chance the selection and it doesn't seem possible to find the associated table, because e.getSource provides javax.swing.DefaultListSelectionModel.
Hence my questions are:
1) Why does it fire twice although the eventListener is only registered once?
2) How can I find the table for which the selection applies? The DefaultListSelectionModel doesn't seem to offer any getSource() or similar.
Many thanks!
Thanks Draemon..It Works fine....
Our Code
vMachinesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent lse) {
if (!lse.getValueIsAdjusting()) {
System.out.println("Selection Changed");
}
}
});
Thanks By
TF Team
1) I think you'll find it fires once for de-selecting the old selection and once for selecting the new selection. If you log the details of the event you should see exactly what's going on. I can't remember the details, so perhaps this is wrong. Either way you should be able to call getValueIsAdjusting() on the event and only use the last one in the chain (ie when it returns false).
2) You shouldn't normally need to, but AFAIK the only way to do this is to create your Listener specifically for the table (ie pass the table to the constructor and remember it).
Since more than one JTable (or other component I'm guessing) can share the same selection model, it doesn't make sense to ask for the associated JTable from the event. This is the same reason that you can't retrieve a JTable from a TableModel. As Draemon suggests, store the reference to the JTable in (or make it accessible to) your listener class.