1

I have written this code. Here I want to make a JScrollPane work with JTextArea. But it is not working at all. Earlier I almost did the same thing. It used to work. Please provide a solution. Thanks in advance. I have posted the code.

    protected void startServerProcess(int port) {
    serverFrame = new JFrame("SERVER NOTIFICATIONS PANEL | Labyrinth Developers");
    serverFrame.setSize(500, 500);
    serverFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    serverFrame.setLocationByPlatform(true);
    serverFrame.setLocationRelativeTo(null);
    serverFrame.setVisible(true);

    notificationsTA = new JTextArea();
    notificationsTA.setBounds(0,0,466,500);
    notificationsTA.setLineWrap(true);
    notificationsTA.setRows(1000);

    notificationsSP = new JScrollPane();
    notificationsSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    notificationsSP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    notificationsSP.setViewportView(notificationsTA);
    notificationsSP.setWheelScrollingEnabled(true);
    notificationsSP.setBounds(470, 0, 30, 500);

    serverFrame.add(notificationsTA);
    serverFrame.add(notificationsSP);
}
Siddharth Kamaria
  • 2,448
  • 2
  • 17
  • 37

1 Answers1

2

JTextArea is already added in JScrollPane so there is no need to add it again in JFrame as well. Remove below line:

serverFrame.add(notificationsTA);

You can add the component in the viewport of scroll pane using its Constructor as well that internally calls JScrollPane#setViewport() method.

notificationsSP = new JScrollPane(notificationsTA);

Some Points:

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Thanks...you are right...I forgot that constructor for JScrollPane...My bad I should have used Javadocs....And regarding setVisible...yeah we should use at last to avoid flickers and actually I have used invokeLater but this was an excerpt of the code. Thanks once again to remind me..:) – Siddharth Kamaria Jun 07 '14 at 10:03