-
Problem with refreshing
Hello
I have a textarea:
Code:
textarea = new JTextArea();
textarea.setFont(new Font("Courier", Font.PLAIN, 22));
scrollPane = new JScrollPane(textarea);
scrollPane.setPreferredSize(new Dimension(300,300));
add(scrollPane, BorderLayout.CENTER);
And when some button is clicked I want to create second textarea:
Code:
remove(scrollPane);
JTextArea textarea1 = new JTextArea();
textarea1.setPreferredSize(new Dimension(100,100));
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPane, textarea1);
splitPane.setOneTouchExpandable(true);
add(splitPane);
Everything works fine but when I click the button, window is one can say "frozen", if you press any menu and close is, grey color still stays on textarea place, but when i resize window it goes back to what it was suppoused to be and I have 2 textareas. How to fix it?
Thanks for responds.
Regards
-
When dynamically adding components to a container (after it has been validated), call revalidate() on the component.
-
Eh, I feel so dumb in java.
I tried to do splitPane.revalidate(); but it didn't gave effect I expected.
This is my whole code Ideone.com | Online Java Compiler & Debugging Tool
Could you tell me where to put revalidate?
I've just started programming in java, I'm though good at C and crossing over to new language is quite hard for me.
Thanks
-
If removing a component, don't forget to also call repaint() on the container.
-
I did remove then repaint, then when I added a new component I put revalidate on new added component and I get a grey background, tried after adding a component ravalidate and repaint - effect is the same
-
Post an SSCCE that demonstrates the problem (and recommended to place the code in the post itself - flanking the code with the [code][/code] tags)
-
Here is program - http://www.misiom1.pl/test.jar Just pick menu "Kod" and press "kompiluj"
And here's code responsible for this change:
Code:
Action compileAction = new AbstractAction("Kompiluj")
{
@Override
public void actionPerformed(ActionEvent arg0) {
remove(scrollPane);
repaint();
JTextArea textarea1 = new JTextArea();
//add(textarea1, BorderLayout.SOUTH);
//scrollPane.setPreferredSize(new Dimension(300,300));
textarea1.setPreferredSize(new Dimension(100,100));
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPane, textarea1);
splitPane.setOneTouchExpandable(true);
add(splitPane);
splitPane.revalidate();
}
};
-
answer is to add validate(); after adding splitPane