Wish [SOLVED] TableView - Column_Resized event

LucaMs

Expert
Licensed User
Longtime User
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.
 

LucaMs

Expert
Licensed User
Longtime User
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:
 

stevel05

Expert
Licensed User
Longtime User
Try this:

B4X:
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
 

LucaMs

Expert
Licensed User
Longtime User
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.
 

LucaMs

Expert
Licensed User
Longtime User
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,


.......
 

stevel05

Expert
Licensed User
Longtime User
I haven't tried it in a B4xPages project. Can you upload it so I don't have to create one?
 

LucaMs

Expert
Licensed User
Longtime User
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.
 

LucaMs

Expert
Licensed User
Longtime User
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 ??
 

LucaMs

Expert
Licensed User
Longtime User
Help!!! :confused::)

B4X:
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);
            }
        });
    }
});
 

LucaMs

Expert
Licensed User
Longtime User
A different (less "elegant") way:

B4X:
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;
              }
          }
      });
    }
 

stevel05

Expert
Licensed User
Longtime User
It works, but is not great. It flashes a bit.

B4X:
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
 

stevel05

Expert
Licensed User
Longtime User
setReordering is not in the documentation for TableHeaderRow for Java 9. Apparently public since Java 12, but it's a hack.
 

LucaMs

Expert
Licensed User
Longtime User
It works, but is not great. It flashes a bit.
Strangely, while the "AddColumnChangedEvent" (I just changed its name) works written right after:
B4X:
Root.LoadLayout ("MainPage")

adding then
B4X:
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!
 

LucaMs

Expert
Licensed User
Longtime User
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:
Private Sub B4XPage_Appear
    If Not(mReorderingDisabled) Then
        DisableReordering
        mReorderingDisabled = True
    End If
End Sub
 

stevel05

Expert
Licensed User
Longtime User
It needs to be called as late as possible and after the table headers have been set. So In page appear sounds good.
 
Top