Nifty Not Updating [Solved]

Hi guys, I made an inner class the basically acts as a chat system because I wanted to easily be able to change how things are done with it later. Anyways I have a text field that allows users to post to the chat box and this ends up working fine. The panels that are added to the chat panel end up getting formatted in a way that they start from the bottom. However, when I try to add messages to this chat box during the beginning of a game chat due to information recieved from sockets, the message gets positioned at the top of the panel, even though I call all of my formating methods. When I click and focus in a focuasable element of the screen, the panel then formats itself. I’m assuming its because nifty redraws or updates in some way, so I tried to include nifty.update(); and (I thought that would cause it to redraw or something) but those things did not work. Here is the code below.

Inner ChatBox class

    private class ChatBox {
        int messageCount = 0;
        Element chatPanel;
        LinkedList<Element> messagePanels = new LinkedList<>();
        public ChatBox(Element chatPanel) {
            this.chatPanel = chatPanel;
        public void addMessage(String message) {
            messagePanels.addFirst(new PanelBuilder("chat"+messageCount){{
            }}.build(nifty, screen, chatPanel));
            Element text = new TextBuilder("chatText"+messageCount) {{
                System.out.println("alsdkjha Built text");
            }}.build(nifty, screen, messagePanels.getFirst());
            messagePanels.getFirst().setConstraintHeight(new SizeValue(text.getRenderer(TextRenderer.class).getTextHeight()+""));
            System.out.println("Height "+text.getRenderer(TextRenderer.class).getTextHeight());
            System.out.println("Height "+messagePanels.getFirst().getHeight());
            System.out.println("ID "+text.getParent().getId()+" "+messageCount);
            //messagePanels.getLast().setConstraintHeight(new SizeValue("30px"));
        private void updatePositions(int newInitial) {
            //I believe doesn't update the dimensions of objects immediatly, so I will start with this. By the time the next panel is added the current will have been updated
            int counter = newInitial;
            for (Element element: messagePanels) {
                System.out.println("Counter is now "+counter);
                element.setConstraintY(new SizeValue(chatPanel.getHeight()-counter+""));
            System.out.println("Updated positions "+chatPanel.getHeight());

Whenever the enter button is clicked this method is called, which checks to see if the chat textbox is the current focused element, and if it is it posts a message to the chat box and sends a command via server socket to other machines. This works fine.

private void handleEnterClicked() {
        Element focusedElement = screen.getFocusHandler().getKeyboardFocusElement();
        if (focusedElement.getId().equals("chatTextfield")) {
            sendMessage(screen.findNiftyControl("chatTextfield", TextField.class).getDisplayedText());
        screen.findNiftyControl("chatTextfield", TextField.class).setText("");

However, at the beginning of a game tick when I handle information recieved by other machines and execute my addMessage method via the below lines

chatbox.addMessage(command.getClient().getClientName()+" : "+command.getMergedArguments());

My text box glitches out as shown in the video below…

Any thoughts on what I could do to fix this?

Are you trying to run that in a different thread or is it in the main rendering thread? Also, can’t see the video.

Ultimately the commands end up being handled in the update() method of the application.

Also I just changed the video to a link so that should work now.

Did you try this already:

or in here chatPanel.layoutElements();, you always need to do it. But again, listbox. Dem scrollbars. And this manual coding.

Edit: in Nifty there actually was a ChatBoxControl ready for you.

1 Like

What can I say, I’m a stubborn guy :grin:

That solved the problem, works wonderfully now. Wonder how I missed that method while browsing through the class. Thank you :+1: