Wish [SOLVED] TableView - Column_Resized event

Found this one, only now:


I want to create text boxes above the header of the grid to be used as a filter boxes and match the width of the columns even when they are resized
1612272335814.png

I have the same need.
 
There is no other solution than to prevent the columns from being resized - and to throw away all the code written to save their widths as user settings.
And I still don't even know if there is a way to prevent column resizing :confused:
 
Try this:

B4X: Copy
Private Sub SetColumnListeners
    Dim jo As JavaObject = TableView1
    Dim ColumnList As List = jo.RunMethodJO("getColumns", Null)
    
    For Each Col As JavaObject In ColumnList
        Dim Prop As JavaObject = Col.RunMethod("widthProperty",Null)
        Dim o As Object = Prop.CreateEventFromUI("javafx.beans.value.ChangeListener","ColumnChanged",Null)
        Prop.RunMethod("addListener",Array(o))
    Next
End Sub

Private Sub ColumnChanged_Event (MethodName As String, Args() As Object) As Object
    Dim Col As JavaObject = Sender
    Dim jo As JavaObject = TableView1
    Dim ColumnList As List = jo.RunMethodJO("getColumns", Null)
    Dim Width As Double = Args(2)
    Dim ColNum As Int = ColumnList.IndexOf(Col.RunMethod("getBean",Null))
    Log(ColNum & " : " & Width)

End Sub
 
I found just now that to prevent column reordering it would be necessary to add a listener and so I figured you could get the required event as well, like in your source, Steve.

Having it ready is a lot better, though :)

Thank you, Steve.
 
An error occurred:
(Line: 0) 0
java.lang.Exception: Sub columnchanged_event signature does not match expected signature.
public static anywheresoftware.b4a.pc.RemoteObject b4j.example.b4xmainpage_subs_0._columnchanged_event(anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject) throws java.lang.Exception
class anywheresoftware.b4a.pc.RemoteObject, class anywheresoftware.b4a.pc.RemoteObject, class anywheresoftware.b4a.pc.RemoteObject,


.......
 
I haven't tried it in a B4xPages project. Can you upload it so I don't have to create one?
 
Um... sorry, my fault...

I had added a parameter to the SetColumnListeners, to pass it a TableView and, automatically, without thinking, I also added it to the event! So I changed the signature of the event and it obviously is no longer recognized.

It works great... even with the B4XPages, of course.

I'm sorry.
 
I was very happy (so to speak ? ? ) but obviously I will have to change/add something (another listener?) to prevent the reordering of the columns as well.

However it is already a great step forward ??
 
Help!!! :confused::)

B4X: Copy
tableView.widthProperty().addListener(new ChangeListener<Number>()
{
    @Override
    public void changed(ObservableValue<? extends Number> source, Number oldWidth, Number newWidth)
    {
        TableHeaderRow header = (TableHeaderRow) tableView.lookup("TableHeaderRow");
        header.reorderingProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                header.setReordering(false);
            }
        });
    }
});
 
A different (less "elegant") way:

B4X: Copy
public static <S, T> void columnReorder(TableView table, TableColumn<S, T> ...columns) {
    table.getColumns().addListener(new ListChangeListener() {
          public boolean suspended;

          @Override
          public void onChanged(Change change) {
              change.next();
              if (change.wasReplaced() && !suspended) {
                  this.suspended = true;
                  table.getColumns().setAll(columns);
                  this.suspended = false;
              }
          }
      });
    }
 
It works, but is not great. It flashes a bit.

B4X: Copy
Private Sub DisableReordering
    Dim Jo As JavaObject = TableView1
    Dim Header As JavaObject = Jo.RunMethod("lookup",Array("TableHeaderRow"))
    Dim Prop As JavaObject = Header.RunMethod("reorderingProperty",Null)
    Dim o As Object = Prop.CreateEventFromUI("javafx.beans.value.ChangeListener","HeaderOrder",Null)
    Prop.RunMethod("addListener",Array(o))
End Sub

Private Sub HeaderOrder_Event (MethodName As String, Args() As Object) As Object
    Dim JO As JavaObject = Sender
    Dim Header As JavaObject = JO.RunMethod("getBean",Null)
    Header.RunMethod("setReordering",Array(False))
End Sub
 
setReordering is not in the documentation for TableHeaderRow for Java 9. Apparently public since Java 12, but it's a hack.
 
It works, but is not great. It flashes a bit.
Strangely, while the "AddColumnChangedEvent" (I just changed its name) works written right after:
B4X: Copy
Root.LoadLayout ("MainPage")

adding then
B4X: Copy
DisableReordering
crashes, as if the TableView hadn't already been initialized.

Maybe it would be necessary to put that call in the page's Appear event, but it works just adding Sleep (0) right before that.


It works, but is not great. It flashes a bit.
IT'S GREAT, STEVE!
THANK YOU AGAIN!
 
crashes, as if the TableView hadn't already been initialized.

Maybe it would be necessary to put that call in the page's Appear event, but it works just adding Sleep (0) right before that.
Yes, it looks better so (not about the flickering, which is not very "annoying"):

B4X: Copy
Private Sub B4XPage_Appear
    If Not(mReorderingDisabled) Then
        DisableReordering
        mReorderingDisabled = True
    End If
End Sub
 
It needs to be called as late as possible and after the table headers have been set. So In page appear sounds good.
 
Back
Top