Using JTextarea with JTabbedPanel

人走茶凉 提交于 2019-12-13 06:04:31

问题


JTextarea is dynamically created and added to the jTabbed panel using the code:

            // tabidis is a variable with unique value in each case

            JScrollPane panel2 = new JScrollPane();
            panel2.setName(tabidis);

            ta = new JTextArea("");
            ta.setColumns(30);
            ta.setRows(20);
            ta.setEditable(false);
            panel2.setViewportView(ta);
            ta.setName(tabidis);

            jTabbedPane1.add(username4, panel2);

When new tabs are added (ta textarea is added along with it), the last tabs textarea recieves all the text.

 private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt){
                send3 = ta.getName();
                ta.setName(send3);
                ta.setText(ta.getText()+send3);
                }

In the above code you can see that The text in both the textareas(In two tabs) should be updated. But what really happens is that only the second TextArea is getting updated.The first TextArea is not updated.


回答1:


ta only have a value at a time, what you need is a Collection of TextArea you have to have a reference to them for example in a List<JTextArea> textAreas

Then in your code

        JTextArea ta = new JTextArea("");
        ta.setColumns(30);
        ta.setRows(20);
        ta.setEditable(false);
        textAreas.add(ta);

And in your event something like this:

private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt){
                for(JTextArea ta : textAreas ){
                 send3 = ta.getName(); // this line an below are redundant
                 ta.setName(send3);
                 ta.setText(ta.getText()+send3);
               }
}


来源:https://stackoverflow.com/questions/17431755/using-jtextarea-with-jtabbedpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!