我一直在为我和一些朋友开发一个小型聊天客户端,同时自学Java和JFX8。不幸的是,由于某些原因,我无法以编程方式滚动到滚动窗格的底部,那里有聊天消息。
我有一个简单地称为scrollToBottom()的方法,它执行以下代码:
public void scrollToBottom() {
Platform.runLater(() -> this.getChatView().vvalueProperty().setValue(1.0));
}
下面是应用程序中唯一真正相关的类--ChatBox类型只是VBox的扩展,它只使用扩展类型的文本。
public class MainScreenController {
//Lists
//Buttons
private Button logoutButton;
private Button btn1 = new Button("Test Button");
private Button scrollButton = new Button("Scroll Button");
//Numbers
//Booleans
private boolean isHosting;
//Strings
private String username = "";
//Scene
private Stage window;
private GridPane layout = new GridPane();
//Other Objects
private TextArea chatField = new TextArea();
private Label usernameLabel;
private TextArea usersArea = new TextArea("Connected users: ");
private VBox firstColumn = new VBox(10);
private VBox secondColumn = new VBox(10);
private ImageView mediaColumn = new ImageView();
private Server server = new Server();
private Client client = new Client();
private TextField dlField = new TextField();
private AudioHandler audioHandler = new AudioHandler();
private ChatBox chatBox = new ChatBox();
private ScrollPane chatView = new ScrollPane(this.chatBox);
private HBox buttonBox = new HBox(10);
private void initChatView() {
this.getChatView().setMinSize(500, 500);
this.getChatView().setPrefSize(500, 500);
this.getChatView().setMaxSize(500, 500);
this.getChatView().setStyle("-fx-focus-color: transparent; -fx-background-color: gainsboro");
this.chatView.hbarPolicyProperty().set(ScrollBarPolicy.NEVER);
this.chatView.vbarPolicyProperty().set(ScrollBarPolicy.AS_NEEDED);
/*this.chatView.vvalueProperty().addListener(e -> {
if(this.chatView.getVvalue() != 1) {
System.out.println("Pre: " + this.chatView.getVvalue());
this.chatView.vvalueProperty().set(1);
System.out.println("Post: " + this.chatView.getVvalue());
}
});*/
this.scrollButton.setOnAction(e -> {
scrollToBottom();
});
}
private void initSecondColumn() {
this.secondColumn.setStyle("-fx-background-color: gainsboro");
this.secondColumn.setPrefSize(525, 550);
this.secondColumn.setMinSize(450, 550);
this.secondColumn.setMaxSize(525, 550);
Button dlButton = new Button("Download link:");
dlButton.setStyle("-fx-focus-color: transparent");
dlButton.setOnAction(e -> {
if (!this.dlField.getText().equals(null) || !this.dlField.getText().equals(null)) {
try {
FileHandler.downloadFile(this.window, this.dlField.getText());
} catch (Exception ex) {
ex.printStackTrace();
}
this.dlField.clear();
}
});
this.dlField.setPrefSize(450, 20);
this.dlField.setMinSize(450, 20);
this.dlField.setMaxSize(450, 20);
this.mediaColumn.setFitHeight(475);
this.mediaColumn.setPreserveRatio(true);
this.mediaColumn.setStyle("-fx-border-color: red");
this.mediaColumn.prefWidth(450);
this.mediaColumn.prefHeight(475);
this.mediaColumn.minWidth(450);
this.mediaColumn.minHeight(475);
this.mediaColumn.maxWidth(450);
this.mediaColumn.maxHeight(475);
this.secondColumn.getChildren().addAll(dlButton, this.dlField, mediaColumn);
}
private void initUsersArea() {
this.usersArea.setPrefSize(500, 75);
this.usersArea.setMinSize(500, 75);
this.usersArea.setMaxSize(500, 75);
this.usersArea.setEditable(false);
this.usersArea.setStyle("-fx-focus-color: transparent; -fx-background-color: gainsboro");
}
public void scrollToBottom() {
Platform.runLater(() -> this.getChatView().vvalueProperty().setValue(1.0));
System.out.println(this.chatView.getVvalue());
}
public MainScreenController(GridPane layout, Stage window, Scene currentScene, Scene nextScene, WindowController windowController) throws URISyntaxException, IOException {
new File(FileHandler.downloadsPath).mkdirs();
this.layout = layout;
this.window = window;
this.initChatView();
FileHandler.readLog(this.getChatBox());
}
private void initUsernameLabel() {
this.usernameLabel = new Label();
this.usernameLabel.setStyle("-fx-border-color: black; -fx-background-color: silver; -fx-focus-color: transparent");
this.usernameLabel.setText(" Logged in as ");
this.usernameLabel.setTextFill(Color.BLUE);
}
private void initLayout() {
this.layout.setPadding(new Insets(10, 10, 10, 10));
this.layout.setVgap(10);
this.layout.setHgap(10);
this.layout.getChildren().addAll(this.firstColumn, this.secondColumn);
GridPane.setColumnIndex(this.firstColumn, 0);
GridPane.setColumnIndex(this.secondColumn, 1);
GridPane.setValignment(secondColumn, VPos.BOTTOM);
this.buttonBox.getChildren().addAll(this.logoutButton, this.scrollButton);
this.firstColumn.getChildren().addAll(this.buttonBox, this.usernameLabel, this.usersArea, this.chatView, this.getChatField());
}
public void initBtn1() {
btn1.setStyle("-fx-focus-color: transparent");
GridPane.setConstraints(btn1, 1, 0);
GridPane.setValignment(btn1, VPos.BASELINE);
this.layout.getChildren().add(btn1);
btn1.addEventHandler(ActionEvent.ACTION, e -> {
boolean b = false;
if (!b) {
audioHandler.startRecording();
b = !b;
} else if (b) {
audioHandler.stopRecording();
b = !b;
}
});
}
public void addMessage(String msg, String color) {
FileHandler.writeToChatLog(msg);
if (!msg.startsWith("*!") && !msg.startsWith("/")) {
Platform.runLater(() -> {
this.getChatBox().addText(new ChatText(msg, color));
this.scrollButton.arm();
this.scrollButton.fire();
});
}
}
private void initChatField() {
this.getChatField().setPrefSize(500, 10);
this.getChatField().setMaxHeight(10);
this.getChatField().autosize();
this.getChatField().setWrapText(true);
this.getChatField().addEventHandler(KeyEvent.KEY_PRESSED, key -> {
if (key.getCode() == KeyCode.ENTER) {
key.consume();
if (this.getChatField().getText().startsWith("/")) {
CommandParser.parse(this.getChatField().getText(), this);
} else {
try {
this.getClient().getClientSendingData().writeUTF(this.getChatField().getText().trim());
this.getChatField().clear();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
this.getChatField().setStyle("-fx-background-color: gainsboro");
}
private void initLogoutButton() {
this.logoutButton = new Button();
this.logoutButton.setText("Log out");
this.logoutButton.setOnAction(e -> {
try {
this.client.getClientSendingData().writeUTF("*![System] " + SystemInfo.getDate() + ": " + this.client.getClientName() + " has disconnected.");
System.out.println("Logging out.");
window.close();
new ChatClient().start(new Stage());
} catch (Exception ex) {
ex.printStackTrace();
}
});
this.logoutButton.setStyle("-fx-focus-color: transparent");
}
public void initMainScreen() {
initUsernameLabel();
initLogoutButton();
initChatField();
initLayout();
initUsersArea();
initBtn1();
initSecondColumn();
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return this.username;
}
public Label getUsernameLabel() {
return this.usernameLabel;
}
public GridPane getLayout() {
return this.layout;
}
public void setIsHosting(boolean b) {
this.isHosting = b;
}
public boolean getIsHosting() {
return this.isHosting;
}
public Server getServer() {
return this.server;
}
public void setServer(Server hostServer) {
this.server = hostServer;
}
public Client getClient() {
return this.client;
}
public void setClient(Client client) {
this.client = client;
}
public ScrollPane getChatView() {
return chatView;
}
public void setChatView(ScrollPane chatView) {
this.chatView = chatView;
}
public TextArea getChatField() {
return chatField;
}
public void setChatField(TextArea chatField) {
this.chatField = chatField;
}
public TextArea getUsersArea() {
return this.usersArea;
}
public ChatBox getChatBox() {
return chatBox;
}
public void setChatBox(ChatBox chatBox) {
this.chatBox = chatBox;
}
}
如果您有任何其他一般性建议(我现在正在学习Java),欢迎所有提示。:)谢谢!
我在使用MasonryPane(来自JFoenix)不使用ScrollPane时也遇到了这个bug。我找到的解决方案是调用scrollpane及其内容的layout()方法。层次结构:ScrollPane->MasonRypane`
masonryPane.getChildren().addListener((ListChangeListener<Node>) c -> {
masonryPane.layout();
scrollPane.layout();
scrollPane.setVvalue(1.0f);
});
我想知道是否有任何“简单”的方法,我可以设置垂直滚动条“滚动”一个特定的数量,按下按钮,或真正的任何事件在所有。 例如,我有一个应用程序,它有一个侧栏,侧栏上的一个选项是设置。Settings有子项,我想添加点击其中一个子项的功能,它将打开Settings页面,并自动向下滚动到该子项所在页面上的特定点。
我有一个滚动视图,里面有很多布局,一步一步地出现(按钮1显示布局1,...)。最后一步,我的最后一个按钮显示一个页脚。 在每次出现布局时,我希望scrollview滚动到底部。我的页脚出现时也一样。 布局文件 kt文件
问题内容: 如何滚动到代码底部?还是以更通用的方式,到子视图的任何一点? 问题答案: 您可以使用UIScrollView的功能滚动到内容视图的任何部分。假设您的scrollView是,这是一些滚动到底部的代码: 希望有帮助!
我在JavaFX应用程序上使用ScrollPane。Scrollpane以TilePane作为其内容,显示了数千个瓷砖。敲击太慢了。如何增加滚动窗格的滚动?谢了。
我有一个无法调整大小的设置面板,它变得越来越拥挤,所以我决定将包含设置的gridpane添加到滚动窗格中。问题是当应用程序启动时,滚动窗格从底部开始。奇怪的是,我将HValue设置为0,它应该从顶部开始。这是在场景生成器中显示的gif:https://gyazo.com/f10a1641b8357e4fc30cd180c0425dae没有任何代码与scrollpane交互,正如您所看到的,scen
问题内容: 我在ScrollPane中有一个Label。我正在循环中更新标签(在另一个线程中)。如果用户未将ScrollPane保持在某个位置,如何更新ScrollPane使其向下滚动(而不是横向滚动,这将手动完成)?是否有二传手? 问题答案: @数学谢谢,这工作! 我用这部分代码解决了“必须看标签”的问题