Search code examples
jscrollpanejtextarea

JTextArea in JScrollPane


I want to make JTextArea scrollable...
I have tried following code.. but am not getting why its not working... Pls help !

jacontactsDeatil = new JTextArea();  
jspn = new JScrollPane();  
jspn.setLayout(null);  
jspn.setBounds(10, 170, 950, 300);
jspn.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);  
    jspn.isWheelScrollingEnabled();  
    jspn.setVisible(true);  
    jspn.setAutoscrolls(true);  
    jspn.setEnabled(true);

    jspn.setBackground(Color.orange);  
    jspn.add(jacontactsDetail);
    jspn.repaint();
    jacontactsDetail.setBounds(0,0,jspn.getWidth(),jspn.getHeight());

Solution

  • One option is to use the JTextArea and the JScrollPane together with a GroupLayout.

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jScrollPane1.setViewportView(jTextArea1);
    
    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(jPanel1Layout.createSequentialGroup().
        addComponent(jScrollPane1,
        javax.swing.GroupLayout.PREFERRED_SIZE, 328,
        javax.swing.GroupLayout.PREFERRED_SIZE)
    );
    jPanel1Layout.setVerticalGroup(jPanel1Layout.createSequentialGroup().
        addComponent(jScrollPane1,
        javax.swing.GroupLayout.PREFERRED_SIZE, 79,
        javax.swing.GroupLayout.PREFERRED_SIZE)
    );