Strategy for detecting an object in JTable row? - java

Here's the thing: a sortable JTable backed by JTableModel with an array of objects that populate rows (one object = one row). Need to delete rows.
Without sorting, deleting an object is simple: get selected row index, delete array object under the same index. With sorting, though, row indexes mess up in a sense that they no longer match backing array object indexes. What's the proper way to overcome this?

Oscar was almost right, here's how it should be done:
int selectedRow = table.getSelectedRow();
tableModel.removeRow(table.convertRowIndexToModel(selectedRow));

I think ( not quite sure ) there is a method like "modelToView" which returns the actual index in the model a view index represents.
So, for instance you have A,B,C,D and then you sort desc. D,C,B,A this method would return 0 for view index 3 ( A )
I think this was on JXTable which supports sorting or in JTable in Java 6.
If you have build this sorting your self, consider adding this method.

Related

Spark access Row object value

I want to iterate a dataframe by partitions and for each partition iterate all of its rows and create a deleteList of them that will contain HBase's delete objects for each row.
I'm using Spark and HBase with Java and I've created a Row object with the following code:
df.foreachPartition((ForeachPartitionFunction<Row> iterator -> {
while (iterator.hasNext()) {
Row row = RowFactory.create(iterator.next());
deleteList.add(new Delete(Bytes.toBytes(String.valueOf(row))));
}
}
But it won't work because I cannot access row's value correctly. While df has one column named "hbase_key".
It's hard to tell from your post which class exactly is Row, but I suspect it is org.apache.spark.sql.Row ?
If that's the case, try the methods like getString(i) or similar, where i is the index of the column in the row you are trying to access.
Again, depending on how you are configuring your Hbase access, I suspect that in your case the 0 index would be the value of the row-key of the physical HBase table, and the subsequent indices will be the respective column values that are returned with your row. But again, that would depend on how exactly you arrived at this point in your code.
Your Row object should have methods to access other data types as well, such as getInt(i), etc.

How to access a particular element in JLIST?

I am trying to push a selected value at index 'a' to index 'a+1'.
But if index 'a+1' is not empty, I have to swap the two values. For this, I do not know how to obtain the value at index 'a+1'. It is not selected. The JList API does not support random access.
How do I implement this ?
Thanks.
To access the elements in a JList you need to get hold of its ListModel.
The model has a getElementAt(int) method.
item = myJList.getModel().getElementAt(a+1);

Get the ArrayList index of an element after using a row filter on a JTable

I have an ArrayList containing multiple elements, each element contains the next fields:
Name
Address
Age
I have a JTable to show the information contained in the ArrayList.
I have a TableRowSorter to filter the information, according to a certain field.
Let´s say after filtering I get only one element, therefore the JTable has now one row. How can I get the index of that element in the original ArrayList? Do I have to implement one more field kind of "ID"?
int viewIndex = 0; // the index in the table of the unique filtered row.
int modelIndex = table.convertRowIndexToModel();
modelIndex is the index of the row in the list backing the table model.

delete and move elements in a table : java

private JEditorPane textArea[]= new JEditorPane[maxTabs];
I've got this table,I want to delete some elements and move the others, for example if I delete the second element, the third will be instead of the second and so on.
Depending on scenario you are interested (we remove for example element at position 1)
1) If elements should be shifted to left and last element should be set to 0 or null depending on type of array like
before [0,1,2,3,4]
after [0,2,3,4,0]
you can use
System.arraycopy(textArea, index+1, textArea, index, textArea.length-index-1);
array[array.length-1]=null;
2) when you want to replace old array with new one that wont contain selected element like
before [0,1,2,3,4]
after [0,2,3,4] //we removed element at position 1 (new array is smaller)
you can try something like this
List<JEditorPane> listCopy = new ArrayList<JEditorPane>(
Arrays.asList(textArea));
listCopy.remove(index);
textArea = listCopy.toArray(new JEditorPane[listCopy.size()]);
You can use System.arraycopy() to move chunks of arrays around: see https://stackoverflow.com/a/5536023/150001
If you can, though, consider using a Collection class like java.util.LinkedList, which is much better suited to removing and reordering elements. If you still need an array when you're done with your deletions/moves (maybe the API you're working with requires it), you can use the toArray() method.

Getting grouped data from database and process further in java

I have a table , say A ; now in A i have attribute ID as string and Time as DateTime.
Now the condition is that different entries to the table can have same ID and they have to be clubbed together and further do some refinement on it.
I am using java, I write the SQL query that
Select * from A group by ID;
Now i get this data in a huge list in java. Now what i do is
Set_ID=NULL;
for(each element in List)
{
if(Set_ID equals elements `ID` from table)
Add the element to the same list
else
Create new List and add element to the list. Change Set_ID to current `ID`
}
This way i get all the Entries with same Id in different lists and i can process further.
But is this the efficient way to this; comparing strings for each element.
Any change i can make, to make it better. Thanks.
Instead of reading all the data into a list & then processing it into sub lists, I'd process them directly into sub lists as you pull them from the database

Categories