Java: JTable adding and moving columns - java

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.

Related

How to update JTableModel after the sorter has changed the rows order?

I have implemented a JTable, on which I can search entries with a textField.
When a request is made, my code gets the DefaultTableModel, looks for the element in each column and row, and then sets the selection in the table to the line where the element was found.
This works, but it's useless if I sort the table by clicking on any column, because the sorting doesn't seem to update the DefaultTableModel.
The table is part of a bigger project which is extremely complicated and full of dependencies so I cannot post a small example, but I think this will sum it up:
Given a DefaultTableModel A full of non-sorted data about a JTable B, where B.setAutoCreateRowSorter() is true , how does one update B after several/any cloumn-sortings of A?
I have read the docs and also looked into this:
http://www.codejava.net/java-se/swing/6-techniques-for-sorting-jtable-you-should-know
As well as dug a bit into TableRowSorter#addRowSorterListener, but it can only tell me that a column was sorted, not really useful. Unless of course I use what that column is to try and sort all the values in a multidimensional array, then clear the table, then assign everything back.. but clearly this is extremely slow for and not really an option in my case.
Refer to this for the info provided by a RowSorterEvent:
https://docs.oracle.com/javase/7/docs/api/javax/swing/event/RowSorterEvent.html
Can someone point me in the right direction ?
When a request is made, my code gets the DefaultTableModel, looks for the element in each column and row...
So don't search the TableModel. You can use the table.getValueAt(...) method to search for the element in each row/column of the table. The data will be accessed in the currently sorted order of the table.
because the sorting doesn't seem to update the DefaultTableModel.
Correct, only the View (JTable) is updated.
If you want to keep searching the TableModel directly then you need to convert the model indexes to the view indexes whenever you want to invoke a JTable method (ie. selecting a table row). This is done by using the following JTable methods:
int columnColumn = table.convertColumnIndexToView(modelColumn);
int row = table.convertRowIndexToView(modelRow);
There are also methods to convert the view values to the model values.

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.

How to refresh JTable with vectors

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.

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: Selected rows's index does not changes when sorted

I have a Jtable on which I called the method
table1.setAutoCreateRowSorter(true);.
So this works on well.
But I also have a methos in my JFrame class which is fired when i push a button. It gets the selected rows indexes using this code
int selectedRows[] = this.table1.getSelectedRows();.
And displays an edit window for the first row corresponding in the selected interval.
The problem is that if I don't click on column's headers (I mean i don't sorte them at all) my method works perfect. But when I sort the row, the indexes of the rows doesn't seems to change at all - thus resulting an edit window for the old row whicn was initially in that position before making any sort.
I am using JDK 6 could anyonw give ma a tip?
The underlying model does not change order. Only the view changes. You can read more about this in Sun's tutorial. You will need to use JTable.convertRowIndexToView() and JTable.convertRowIndexToModel().
You need to use convertRowIndexToView(int) and convertRowIndexToModel(int) to convert model (underlying data) indices and view indices.

Categories

Resources