How to refresh JTable with vectors - java

I have been stumped with this for quite some time now. I understand you use the table model to refresh the actual table with the new values however I cannot seem to get this to work. I have added a tablemodellistener to my form and have a tableChanged method. However, I cannot seem to figure out why the tableChanged method isn't getting called when I insert into a the table.
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
DefaultTableModel model = (DefaultTableModel)e.getSource();
// String columnName = model.getColumnName(column);
//Object data_1 = model.getValueAt(row, column);
//model.fireTableCellUpdated(row, column);
//model.fireTableDataChanged();
//customerTable.repaint();
}
Could I completely rebuild the table if I click the refresh button on my form? Would that at all be possible? If not, do I have to call my tableChanged method from my refresh button's action performed method in order for it to trigger? I've been stuck on this for quite some time now and I would just like to get this figured out for the benefit of learning.

If you have the option and it fits your needs, I'd reccomend taking a look at GlazedLists. Then you won't have to worry about updating your table models--it's all handled for you.
Here's a jump to a relevant part of the GlazedLists tutorial.

I believe you need to manually add your Table as Listener of table TableModel.

Related

Equivalent of getSelected column method of JTable in DefaultTableModel

I was re designing my codes with formerly using
JTable jtable = new JTable();
int selectedIndex = jtable.getSelectedColumn();
//implementations
Now, I need to use DefaultTableModel. Is there a method or implementation that is related to this method of JTable? Thanks!
A TableModel has nothing to do with the selected column of the JTable.
It doesn't matter what TableModel you use with the JTable, you still use
int selectedIndex = table.getSelectedColumn();
method to get the selected column.
Of course, there is no selected column when you first create the JTable. The user must select the column or you must set the selection on a particular cell of the table.
So you only use the getSelectedColumn() method in response to some kind of user event.

Get Multiple raws selected in Jtable in java

Im triying to get all raws selected in table , I us the GetSelectedRaw() method to get the raw wich its selected by the user :
int raw_index = table.getSelectedRow();
but what if the user select more than one raw ??? I need to get all index of all raws selected in the table ...
I put onMoussPressed and onMoussReleased to the table :
int start_rows_to_delete; // the first selected raw
int end_rows_to_delete; // the last selected raw
private void tableMousePressed(java.awt.event.MouseEvent evt) {
start_rows_to_delete = table.getSelectedRow();
}
private void tableMouseReleased(java.awt.event.MouseEvent evt) {
end_rows_to_delete = table.getSelectedRow();
}
runing of prog. give me:
start_rows_to_delete = start_rows_to_delete !!!!!!
To more understande my goal , plz check this picture :
How do I do that? I googled a lot, but either I used the wrong keywords or there are no simple solutions on the internet. I hope somebody here can help me.
Best regards and thanks in advance, Fadel
From the JavaDocs
Returns the index of the first selected row, -1 if no row is selected.
Try using JTable#getSelectedRows, which will return an array of the selected row indicies
You may also want to take a look JTable#convertRowIndexToModel which will convert the view index to the model index which is useful when the table is sorted.
Instead of using a MouseListener you should use a ListSelectionListener which will let you know when the selection has changed, as the use may change the selection using the keyboard, which the MouseListener, obviously, won't tell you about.
Check out How to write a List Selection Listener for more details
Use the ListSelectionModel.
You can get it from a table Table.getSelectionModel()
The ListSelectionModel gives you the selected indexes. With them you can get the selected objects through the TableModel.

tableChange() executing when the JTable is refreshed

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

Detecting Selection Changes in a JTable

I'm trying to find a way to detect changes in which column the user selected in a JTable. I did some poking around and it appears that you need to somehow use a TableColumnModelListener in order to detect the changes, but that doesn't seem to fire an event when you change the column you have selected.
You need to add a ListSelectionListener instead. That will capture selection events. Here are some Swing tutorials that go further in depth:
http://download.oracle.com/javase/tutorial/uiswing/events/listselectionlistener.html
http://download.oracle.com/javase/tutorial/uiswing/components/table.html#selection
From what I read, I think you need to add a MouseListener to your table, which for example in mouseClicked will get the row and column using the following code, below:
table.addMouseListener(new MouseListener()
{
#Override
public void mouseClicked(MouseEvent e)
{
Point pnt = evt.getPoint();
int row = table.rowAtPoint(pnt);
int col = table.columnAtPoint(pnt);
}
}
It should work great for you I have used similar thing myself before.
BTW it look similar to the problem I found on coderanch, link:
http://www.coderanch.com/t/332737/GUI/java/detect-single-click-any-cell
Good luck, Boro
If by "change" you mean changing the value of a cell then you can use an AbstractTableModel and implement the fireTableCellUpdated method

Java: JTable adding and moving columns

im quite new in Java.
I want to add Columns in a JTable at a specified index.
For this i am using addColumn(..) and then move them with moveColumn(...), this works great at the first time, but when i add another column it kind of moves also the other(before added columns).
Do you have any suggestions?
this is the code i've written in the TableModel is:
public void addColumn(Object columnName,
Vector columnData, JTable table) {
int moveTo = ((Integer)columnName);
boolean unselected = moveTo==-1;
super.addColumn(this.getColumnCount(), columnData);
if(!unselected) {//if a column was selected
table.moveColumn(this.getColumnCount()-1, moveTo+1);
}
}
this works great at the first time,
but when i add another column it kind
of moves also the other(before added
columns).
I don't understand what that means.
If you need more help post your SSCCE that demonstrates the problem.

Categories

Resources