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 myElement.show() (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){{
style("messagePanel");
height("d");
}}.build(nifty, screen, chatPanel));
Element text = new TextBuilder("chatText"+messageCount) {{
style("messageText");
text(message);
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"));
updatePositions(text.getRenderer(TextRenderer.class).getTextHeight());
messageCount++;
}
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) {
counter+=element.getHeight();
System.out.println("Counter is now "+counter);
element.setConstraintY(new SizeValue(chatPanel.getHeight()-counter+""));
}
chatPanel.show();
nifty.update();
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…
https://drive.google.com/open?id=0B3Mz_oLfgE8wV2dxZzF4U3E3Nnc
Any thoughts on what I could do to fix this?