Autoscroll

* Implemented functionality to automatically scroll down when user is on
the bottom of the chat and then there are new messages added.
* When chat is opened, the vertical scroll bar starts at the bottom.
* When rereading messages, the chat doesn't scroll down if new messages
are added. (Besides see first point)
This commit is contained in:
DieGurke 2019-12-15 12:48:40 +01:00
parent 6bfa3c2b79
commit 3fefeb1082

View File

@ -63,6 +63,8 @@ public class ChatWindow extends JFrame {
// private JCheckBox jCbChangeMode;
private PrimaryButton postButton = new PrimaryButton("Post");
private PrimaryButton settingsButton = new PrimaryButton("Settings");
private int verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum();
private boolean chatOpened = false;
private static int space = 4;
@ -208,6 +210,7 @@ public class ChatWindow extends JFrame {
textPane.setText(currentChat.getRecipient().getName());
messageList.setModel(currentChat.getModel());
chatOpened = true;
contentPane.revalidate();
}
});
@ -253,11 +256,8 @@ public class ChatWindow extends JFrame {
// scrollPane
scrollPane.setForeground(theme.getBackgroundColor());
scrollPane.setBackground(theme.getCellColor());
// scrollPane.getVerticalScrollBar()
// .setBackground(
// new Color(theme.getBackgroundColor().getRed() + 50,
// theme.getBackgroundColor().getGreen() + 50,
// theme.getBackgroundColor().getBlue() + 50));
// Scroll Bar Styling
scrollPane.getVerticalScrollBar().setBackground(theme.getCellColor());
scrollPane.getVerticalScrollBar()
.setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(),
@ -267,10 +267,26 @@ public class ChatWindow extends JFrame {
scrollPane.getHorizontalScrollBar()
.setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(), new Color(theme.getInteractableBackgroundColor().getRGB() - 50),
new Color(theme.getInteractableBackgroundColor().getRGB() + 170), false));
// int currentVerticalScrollBarValue =
// scrollPane.getVerticalScrollBar().getValue() +
// scrollPane.getVerticalScrollBar().getVisibleAmount(); Work in Progress for
// autoscroll
// Autoscroll
scrollPane.getVerticalScrollBar().addAdjustmentListener(e -> {
if ((verticalScrollBarMaximumValue - e.getAdjustable().getMaximum()) == 0) {
return;
}
if (chatOpened == true) {
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum();
chatOpened = false;
return;
}
if (scrollPane.getVerticalScrollBar().getValue()
+ scrollPane.getVerticalScrollBar().getVisibleAmount() + 100 >= scrollPane.getVerticalScrollBar().getMaximum()) {
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum();
}
});
// messageEnterTextArea
messageEnterTextArea.setCaretColor(theme.getTypingMessageColor());
messageEnterTextArea.setForeground(theme.getTypingMessageColor());