### Disable Column Reorder

Example of implementing a ListChangeListener for a TableView to disable reordering of columns.
The ListChangeListener uses, when changed, the next() API method to determine the action to be taken.
If the action is "wasReplaced" then moving a column is disabled.
Reference: http://docs.oracle.com/javafx/2/api/javafx/collections/ListChangeListener.Change.html
Study also: http://stackoverflow.com/questions/10598639/how-to-disable-column-reordering-in-a-javafx2-tableview
This is a very good example (as provided by B4X) of converting Java Code into B4J Code by making extensive use of JavaObjects.
See http://www.b4x.com/android/forum/threads/reorganization-of-column-in-the-tableview.45047/
Added comments for understanding.
Example Code
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private tv As TableView
Private suspended As Boolean
Private items As List
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.Show
' Build the table view
tv.Initialize("tv")
MainForm.RootPane.AddNode(tv, 10, 10, 400, 400)
items.Initialize
For i = 1 To 5
items.Add(Array(i, i + 1, i + 2))
Next
SetTable
' Assign the tableview to the JavaObject
Dim jo As JavaObject = tv
' Create the ListChangeListener as JavaObject. The event is called colchange, and will be used to handle the column moves
Dim e As Object = jo.CreateEvent("javafx.collections.ListChangeListener", "colchange", Null)
' And add the listener to the tableview columns
jo.RunMethodJO("getColumns", Null).RunMethod("addListener", Array(e))
End Sub`
' Set the tableview properties
`Sub SetTable
' The columns first
tv.SetColumns(Array("col1", "col2", "col3"))
' Add the data to the tableview from a 3 dimentional array
tv.items = items
' Set the colwith
For i = 0 To tv.ColumnsCount - 1
tv.SetColumnWidth(i, 100)
Next
End Sub`
' The colchange event which disables reordering of the columns
`Sub colchange_Event (MethodName As String, Args() As Object) As Object
Log(Args.Length & " = Colchange Event: " & Args(0))
' The change
Dim change As JavaObject = Args(0)
' Important to call go to the next change
change.RunMethod("next", Null)
' Determine what has changed. In this case check wasReplaced which indicates if elements were replaced during this change
If change.RunMethod("wasReplaced", Null) AND Not(suspended) Then
suspended = True
SetTable
suspended = False
End If
Return Null
End Sub`
The Java Code from StackOverFlow
import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
public class TableTest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
final TableView tableView = new TableView();
final TableColumn[] columns = {new TableColumn("1"), new TableColumn("2")};
tableView.getColumns().setAll(columns);
tableView.getColumns().addListener(new ListChangeListener() {
public boolean suspended;
@Override
public void onChanged(Change change) {
change.next();
if (change.wasReplaced() && !suspended) {
this.suspended = true;
tableView.getColumns().setAll(columns);
this.suspended = false;
}
}
});
stage.setScene(new Scene(tableView));
stage.show();
}
}

Changed: 2018-03-29 14:26, Category: TableView, File: T6C259M1880.htm, ID: 1880