Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Hashmap样式循环中,目标JTextArea包装在JScrollPane中_Java_Swing_Jscrollpane_Jtextarea - Fatal编程技术网

Java 在Hashmap样式循环中,目标JTextArea包装在JScrollPane中

Java 在Hashmap样式循环中,目标JTextArea包装在JScrollPane中,java,swing,jscrollpane,jtextarea,Java,Swing,Jscrollpane,Jtextarea,假设我有一个JTextArea包装在一个JScrollPane中,一个Hashmap充满了JScrollPane: protected static JTextArea jta1 = new JTextArea(); protected static JScrollPane jts1 = new JScrollPane(jta1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

假设我有一个JTextArea包装在一个JScrollPane中,一个Hashmap充满了JScrollPane:

protected static JTextArea jta1 = new JTextArea();
protected static JScrollPane jts1 = new JScrollPane(jta1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

public static Hashtable<Integer, JScrollPane> textAreas = new Hashtable<Integer, JScrollPane>();

textAreas.put(1, jts1);
我可以将目标对准JTextarea正在包装的外部JScrollPane,但是
项。setLineWrap(true)
特别适用于内部JTextarea,因此如何选择它?
无法从JTextArea
((JTextArea)项)强制转换,先尝试一下

您必须使用JScrollPane上的getViewport()方法访问视口,然后在返回的JViewport上调用getView()方法,返回的组件应该是您的JTextArea,您需要将组件强制转换为JTextArea才能在其上调用setLineWrap


((JTextArea)item.getViewport().getView()).setLineWrap(true)

您需要根据键值查找每个项。。。
for (JScrollPane item : textAreas.values()) {
        item.setPreferredSize(new Dimension(540, 200)); // This is JScrollPane
        item.setLineWrap(true); // This is the JTextArea attribute <----
        item.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Settings.SITE_ORANGE));  // This is JScrollPane      
    }