Replacing one JTextArea with another
In my GUI a have a JTextArea within a JScrollPane that is attached to a container.
ta = new JTextArea();
jsp = new JScrollPane(ta);
container.add(jsp);
I want to be able to replace this JTextArea with ano开发者_StackOverflow中文版ther JTextArea, for example
JTextArea ta1 = new JTextArea("New text area");
ta = ta1;
ta.repaint();
However, when I reassign this JTextArea nothing in the GUI changes. Is there a better/correct way for doing this?
You should just replace text of textarea with new one.
newTextArea.setText(oldTextArea.getText())
or simply put new text by
newTextArea.setText("new text")
ta = ta1;
If you want a component to be shown in a GUI then you need to add(...) the component to the GUI.
panel.add(ta1);
panel.revalidate();
panel.repaint();
Changing the reference does not add a component to the GUI.
This is your second question that attempts to play with the reference of a variable in order to access a GUI component. Again I ask the question why are you doing this. It wasn't required in your last question and I doubt it is required in this question. You have some kind of design problem.
精彩评论