JScrollPane not appearing on JTextArea

血红的双手。 提交于 2019-12-02 12:51:40

Stop mucking with setBounds and setPreferredSize, you're just making live more difficult for your self.

If you want to affect the size of JTextArea (and the viewable area of the JScrollPane) have a look at the JTextArea constructor JTextArea(int rows, int columns), which will allow you to specify the number of rows/columns you want the JTextArea to default to, and which will allow the JTextArea to calculate it's preferredSize based on the current font's metrics in more stable cross platform way

Your core problem, however, is right here...

scrollPane.getViewport().add(notificationBox);
panel.add(notificationBox);

You add the notificationBox to the JScrollPanes JViewport, which is good, but then you add notificationBox to the panel, which will remove it from the JScrollPane's JViewport, which is bad

Instead, add the JScrollPane to the panel

scrollPane.getViewport().add(notificationBox);
panel.add(scrollPane);

You're also making overuse of static. I'd highly recommend you take the time to reduce static down to it's absolute minimum required usage, this will probably mean that rather then constructing the UI in the main method, you have a "main" class which you can insatiate (from main) which will perform the initial setup - IMHO

I've tried that. I think someone else suggested that from another post, but when I tried that, it just took away the JTextArea completely from the panel

Get rid of panel.setLayout(null); and start making use of appropriate layout managers and compound layouts. Start by having look at Laying Out Components Within a Container for more details

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