Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with scroll

 
danesh far
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am writing an application to show a scroll panel attached to a text area
but it does not appear on the screen. If remove the lines

JScrollPane scrollText = new JScrollPane(textArea);
contentPane.add(scrollText);
and add
contentPane.add(textField)
It appears but without scroll?.


import javax.swing.*;
import java.awt.*;

class TestFrame extends JFrame {
private JTextArea textArea;

public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setVisible(true);
}

public TestFrame( ) {

setSize ( 700, 400 );
setLocation ( 5, 5 );

Container contentPane = getContentPane();
contentPane.setBackground(Color.white);
contentPane.setLayout(null);

textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBounds(350, 50, 100, 135);
textArea.setBorder(BorderFactory.createLineBorder(Color.red));
JScrollPane scrollText = new JScrollPane(textArea);
contentPane.add(scrollText);

}



}
 
Kriti Garg
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

problem lies in the line

contentPane.setLayout(null); //remove this line

and as your text area is uneditable ,scrollbar will not be added when typed sufficient characrted add this for ahieving that

scrollText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);


scrollText.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);


hope it will help u out!
BBye

Kriti
 
Gurumurthy Ramamurthy
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final Code:
import javax.swing.*;
import java.awt.*;

class TestFrame extends JFrame {
private JTextArea textArea;

public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setVisible(true);
}

public TestFrame() {
Container contentPane = getContentPane();
contentPane.setBackground(Color.white);

textArea = new JTextArea();
JScrollPane scrollText = new JScrollPane(textArea);
scrollText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
contentPane.add(scrollText);
setSize(700,400);
setLocation(5,5);

}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic