繁体   English   中英

JavaFX TableView列适合内容

[英]JavaFX TableView columns fit content

我想调整一些TableView列的大小,因为javafx缺少一个实现此目的的方法,我设法找到在MainApp.GUIUtil.fitColumns(TableView tableView)公开的解决方案。
我的问题是:这个解决方案在用户操作调用时工作正常,但我无法找到在启动任何用户干预之前在启动时运行此方法的方法。
我希望将表格显示为安装在表格中的列。
正如您所看到的,我拦截了导致我头痛的Exception (PersonTableController.setMainApp第32行),打印堆栈跟踪,然后让程序继续在向用户提供控制之后证明fit方法有效。
如果显示tableview,我怎样才能通过代码调整列大小?

    Exception in Application start method
java.lang.reflect.InvocationTargetException
    ...
Caused by: java.lang.RuntimeException: Exception in Application start method
    ...
Caused by: java.lang.NullPointerException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at columnstofit.MainApp$GUIUtil.fitColumns(MainApp.java:120)
    at columnstofit.PersonTableController.setMainApp(PersonTableController.java:35)
    at columnstofit.MainApp.showPersonTable(MainApp.java:79)
    at columnstofit.MainApp.start(MainApp.java:65)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application columnstofit.MainApp

这是我的代码:

package columnstofit;

import com.sun.javafx.scene.control.skin.TableViewSkin;
import java.awt.AWTException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class MainApp extends Application {

    private       PersonTableController controller;
    public static Stage                 primaryStage;
                  AnchorPane            personTable;

    private ObservableList<Person> personData = FXCollections.observableArrayList();

    /**
     * Constructor
     */
    public MainApp() {
        // i am entering this name just to force the resizing of the column
        personData.add(new Person("Hansgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", "Muster"));
        personData.add(new Person("Ruth", "Mueller"));
        personData.add(new Person("Heinz", "Kurz"));
        personData.add(new Person("Cornelia", "Meier"));
        personData.add(new Person("Werner", "Meyer"));
        personData.add(new Person("Lydia", "Kunz"));
        personData.add(new Person("Anna", "Best"));
        personData.add(new Person("Stefan", "Meier"));
    }

    /**
     * Returns the data as an observable list of Persons. 
     * @return
     */
    public ObservableList<Person> getPersonData() {
        return personData;
    }

    @Override
    public void start(Stage primaryStage) throws AWTException  {

        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Names Table");

        showPersonTable();
    }

    public void showPersonTable() throws AWTException {

        try 
        {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("PersonTable.fxml"));
            personTable = (AnchorPane) loader.load();

            // Give the controller access to the main app.
            controller = loader.getController();
            controller.setMainApp(this);

            Scene scene = new Scene(personTable);
            primaryStage.setScene(scene);
            primaryStage.show();
        } 
        catch (IOException e) { e.printStackTrace(); }
    }

    /**
     * Returns the main stage.
     * @return
     */
    public Stage getPrimaryStage() {
        return primaryStage;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }


    public static class GUIUtil {
        private static Method columnToFitMethod;

        static 
        {
            try 
            {
                columnToFitMethod = TableViewSkin.class.getDeclaredMethod("resizeColumnToFitContent", TableColumn.class, int.class);
                columnToFitMethod.setAccessible(true);
            } 
            catch (NoSuchMethodException e) {e.printStackTrace();}
        }

        public static void fitColumns(TableView tableView) {
            for (Object column : tableView.getColumns()) 
            {
                try { columnToFitMethod.invoke(tableView.getSkin(), column, -1); } 
                catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); }
            }
        }
    }

    public class Person {
        private final StringProperty firstName;
        private final StringProperty lastName;

        public Person() {
            this(null, null);
        }

        public Person(String firstName, String lastName) {
            this.firstName  = new SimpleStringProperty(firstName);
            this.lastName   = new SimpleStringProperty(lastName);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String firstName) {
            this.firstName.set(firstName);
        }

        public StringProperty firstNameProperty() {
            return firstName;
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String lastName) {
            this.lastName.set(lastName);
        }

        public StringProperty lastNameProperty() {
            return lastName;
        }
    }
}  

这是FXML文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="columnstofit.PersonTableController">
    <TableView fx:id="tableView" layoutX="-39.0" layoutY="39.0" onKeyPressed="#fitColumns" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <columns>
                      <TableColumn fx:id="firstNameColumn" editable="false" minWidth="-1.0" prefWidth="-1.0" text="First Name" />
                      <TableColumn fx:id="lastNameColumn" editable="false" minWidth="-1.0" prefWidth="-1.0" text="Last Name" />
                    </columns>
                     <columnResizePolicy>
                        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                     </columnResizePolicy>
                  </TableView>
</AnchorPane>

与他各自的控制器:

package columnstofit;

import java.awt.AWTException;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

    /**
    * FXML Controller class
    */
    public class PersonTableController {

        // Reference to the main application.
        private MainApp mainApp;

        @FXML
        private TableColumn<MainApp.Person, String> firstNameColumn;
        @FXML
        private TableColumn<MainApp.Person, String> lastNameColumn;
        @FXML
        private TableView                           tableView;

        public PersonTableController() {
        }

        public void setMainApp(MainApp mainApp) throws AWTException {
           this.mainApp = mainApp;
           // Add observable list data to the table
            tableView.setItems(mainApp.getPersonData());
            try
            {
                fitColumns();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        @FXML
        private void initialize() throws AWTException {
            // Initialize the person table with the two columns.
            firstNameColumn.setCellValueFactory(
                    cellData -> cellData.getValue().firstNameProperty());
            lastNameColumn.setCellValueFactory(
                    cellData -> cellData.getValue().lastNameProperty());
        }

        @FXML
        private void fitColumns() {
            MainApp.GUIUtil.fitColumns(tableView);
        }
    }

我的想法是在渲染结束时调用该方法,所以我尝试使用这个解决方案: 在JavaFX中发布渲染事件 ,但如果我这样做,则不会发生任何事情。

所以,区别在于您已添加

try
{
    fitColumns();
}
catch (Exception e) {
    e.printStackTrace();
}

在控制器的setMainApp方法中。 正如James_D所指出的那样, 抛出了空指针异常,因为此时尚未安装外观。

您可以使用控制器中的更新setMainApp方法来解决此问题,如下所示:

public void setMainApp(MainApp mainApp) {
    this.mainApp = mainApp;
    tableView.setItems(mainApp.getPersonData());
    if(mainApp.primaryStage.isShowing())
        fitColumns();
    else {
        mainApp.primaryStage.showingProperty().addListener((obs, oldVal, newVal) -> {
            if(newVal)
                fitColumns();
        });
    }
}

这将检查您的ApplicationStage是否正在显示,如果是,则它适合列。 如果它没有显示,它会将一个监听器附加到StageshowingProperty ,因此只要显示Stage ,就会调用fitColumns方法。

只要它改变了,只要监听tableView.skinProperty,如果皮肤不为null,你只需调用fitColumns方法即可。

另一种方法是创建自定义表皮肤并覆盖TableView类的createDefaultSkin方法以返回自定义皮肤的实例。 然后,您可以为实例化的每个TableView对象fitColumns。

Javafx允许您通过css属性-fx-skin设置皮肤。 这样,您就可以在不覆盖createDefaultSkin方法的情况下设置外观以获得更清晰的代码。

通过扩展TableViewSkin,resizeToFitContent方法即可使用而无需使用反射。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM