how to add row values to jtable in java swing? - java

I have a jcombo box which has some items like "schoolbooks","collegebooks","historybooks".and i have dynamic ArrayList object of corresponding books...When i click the combo box item "schoolbooks" or "historybooks" ,it should display the contents in JTable from the arraylist.while every action performing,the JTable has to display the contents of the corresponding item of 'schoolbooks' or 'historybooks' .it should not append new rows when every action is performing ...i have used default table model in this.but when i add 3 or 4 rows using default table model,its appending the row with previous here..if i use removeRow(i) in for loop ,its removing 1 row or 2 rows only...Its not removing previous all rows suppose if i have 7 rows ..i am not able to solve this..please if anyone know this,please help...

You might want to review How to Use Tables as a guide to preparing your sscce. As you are using DefaultTableModel, you'll need to show how you construct the Object[] passed to addRow() and how you calculate the index passed to removeRow().

if i use removeRow(i) in for loop ,its removing 1 row or 2 rows only...Its not removing previous all rows suppose if i have 7 rows
When you remove multiple rows you need to remove the row from the end of the table down to 0:
for (int i = table.getRowCount() - 1; i > 0; i--)
{
// add logic here
}

Related

Get all selected rows by shift click

If I have a JTable in java and if I click on the first row then shift+click lets say on the 10th row, how would get all the selected rows between 1 and 10... Is there a code for it? thanks in advance
how would get all the selected rows
Check out the JTable API. You can use the getSelectedRows() method to get the indexes of all the selected rows. Then you write a loop to iterate through the indexes to access the data that you need.
I believe what you're asking for is the method JTable.setSelectionMode(int), using as parameter ListSelectionModel.SINGLE_INTERVAL_SELECTION or possibly ListSelectionModel.MULTIPLE_INTERVAL_SELECTION, to enable the selection of multiple contiguous rows. For instance:
JTable table = ...;
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

How would I populate a new two-dimensional array from selected data using JTable

wondering how I would get around populating a two-dimensional array after the user manually selects data from a JTable i.e. if you select two cells from the JTable which are (Football, Rugby) then they will be added to the new array the two array should populate in relation to [row][column]. I am asking this so I'm able to pass this array through to another class which will create visualizations. The code for my ListSelectionModel is:
http://pastebin.com/eNYz3us2
If i were to highlight the two cells (Football, Rugby) it would print out to the command line "FootballRugby" so just need to store this in the array!
Thanks for any help :)
This will give you the selected row and the selected column from the JTable. If no cell is selected then getSelectedXXX method will return -1.
table.getSelectedColumn(); // Gives the selected column.
table.getSelectedRow(); // Gives the selected row.
After getting the row and the column call getValueAt method to get the value at that position.
Now you have the required data with you and you can store it in the array and pass it to another class for visualization.

Updating JTable row dynamically after the deletion successive rows

I have a JTable and I am inserting rows to that JTable dynamically.
After some user intervention, a new row will be inserted .Then I will call a function with parameter as selected row number and inside that function, I have some code which will update the same row accordingly. Each row insertion and row value update code will run in a separate thread.
public void updateRow(int row,JTable myTable)
{
String text = "";
//After lot of processing, setting the table cell value at the 'row' on the 4th column
myTable.setValueAt(text, row, 4);
}
The issue I am facing is as follows,
If the user deleting any row, then row's position will change and at that time if the function updateRow() trying to update some other row, then it will fail because of change of row count.
Lets say I have 3 row at a time and each row's updateRow is in progress.
updateRow(0,userTable);//For the 1st row
updateRow(1,userTable);//For the 2nd row
updateRow(2,userTable);//For the 3rd row
and assume 2nd row's updateRow() is completed. This will cause issue in updateRow() function of 3rd row. Because, it has the row value as '2' . Since the 3rd row got deleted, there is no 3rd row and which in turn cause the following code to fail
myTable.setValueAt(text, row, 4);//Currently, row has the value as '2'
Can anyone suggest me how can I keep track of the row update with row values accordingly, even if the row's position got changed dynamically ?
Thanks in advance.
Instead of relying on the view's row number, operate on the model directly, as suggested in a comment by #mKorbel; if you alter the model, the view will follow. As discussed here, "JTable provides methods that convert from model coordinates to view coordinates — convertColumnIndexToView and convertRowIndexToView — and that convert from view coordinates to model coordinates — convertColumnIndexToModel and convertRowIndexToModel.

Moving JTable rows downwards when new row is added to the table

I have to maintain list of events generated by the user in a JTable.The JTable consists of 2 columns Time and event and 5 rows. When 1st event is generated it's data must be placed in 1st row of JTable, when 2nd event is generated the data in 1st row must be moved to 2nd row and the data of 2nd event must be place in the 1st row of the JTabel and so on up to 5th row. There is no limit to the events(n events can be generated). But i have to display only 5 events in JTable and older once will be stored in text file. I have tried some logics but they are failed to work.
This is my problem. Can any one give me an idea how to move rows downwards...
Regards...
Upendra.S
You simply add your new row at index 0 in your TableModel. If your TableModel extends from DefaultTableModel, you can even use the available insertRow method.
For the requirement to only show 5 rows, you simple remove the extra row from your TableModel.
Consult the JTable tutorial for more information
have look at JTable tutorial
part TableModel
method from DefaultTableModel insertRow()

How to Keep Track of row index when JTable has been sorted by the user?

I have a JTable which has 1st row blank. Now when I sort the table based on a column by clicking on that column then the blank row goes at the bottom. If I insert something in the blank row and do the sorting then the row is placed accordingly. How do I keep track of its row index even when it is sorted. I need to access that row but if user does the sorting then i loose the row index as it is no more the 1st row.
Assuming you're using the TableRowSorter stuff added in Java 6, I think what you need to look at are the methods convertRowIndexToModel and convertRowIndexToView in the RowSorter Class. You'd do something like
table.getRowSorter().convertRowIndexToView(0)
to find out which visible row index is actually row index 0 from your model.
Edit: As Tulskiy pointed out in the comments, this may cause a NullPointerException if no row sorter is assigned to the table. Better to use the methods directly on JTable instead, e.g. table.convertRowIndexToView(0)
JTable.convertRowIndexToView() will return you an index of the row in the view based on its index in the model. JTable.convertRowIndexToModel() will do the opposite.
SOLUTION 1
There is some issue when using jTable1.convertRowIndexToModel(jTable1.getSelectedRow()) if you sort the values according to date or time will return incorrect values from the convertRowIndexToView(0) therefore use convertRowIndexToModel(0).
SOLUTION 2
This will retrieve from the new model when sorted.
jTable1.getValueAt(jTable1.getSelectedRow(), 0);
This will retrieve value according to the original index even when sorted.
((DefaultTableModel)jTable1.getModel()).getValuAt(jTable1.getSelectedRow(), 0);

Categories

Resources