Get all selected rows by shift click - java

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);

Related

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.

Removing unused Rows from jTable

How do I remove unused rows from a JTable?
I first create a table of a fixed size in a JPanel and then fill elements as needed.
Now I don't want unused rows to be displayed in my table. Please help.
I presently get a table with lots of empty rows in bottom, only top 5-6 rows being used, with rest blank. I want to hide them or remove them somehow
Work the other way around. Start with an empty DefaultTableModel. DefaultTableModel supports an addRow() method. So, as you get data to add to the model use:
model.addRow(...);
Don't let the GUI editor determine how you code your GUI. Adding cleanup code to remove rows is a bad design.
DefaultTableModel has a method removeRow which removes a row from the table. Pass the row index which you want to remove from the table.
Now I don't want unused rows to be displayed in my table.
This I am not sure please post an SSCCE to show us what is the flag that says these rows are not used.

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 add row values to jtable in java swing?

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
}

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