Add a JScrollPane to a JLabel

半城伤御伤魂 提交于 2019-12-10 21:01:12

问题


I can't seem to find out how I can add a JScrollPane to a JLabel. The JLabel that I'm using is populated with a long formatted HTML string. Please help.

area = new JLabel();

JScrollPane scroller = new JScrollPane(area, 
      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
      JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

panel.add(scroller);

回答1:


Really not good idea to hold or display long Html formatted text in the JLabel, since is possible, better would be use JEditorPanes / JTextPanes, these JComponets support styled and html formatted text, Icons etc ... , examples for JTextPane and JEditorPane




回答2:


Can you provide us your code? Are you setting the viewport view to the JLabel? Instantiate your JLabel and a JScrollPane. then set the JScrollPane viewport to the JLabel (setViewPortView(jlabel);) then add the JScrollPane to whatever component you want the scrolling JLabel to be on.

Hope this helps!




回答3:


You can't add a JScrollPane to a JLabel , what you can do is to create a JScrollPane and add a JLabel.

See this: http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node63.html




回答4:


You need to set the JScrollPane's viewport view:

scroller.setViewPortView(area);

stick that line just before you go panel.add(scroller);

Let us know if that helps or not.



来源:https://stackoverflow.com/questions/9335138/add-a-jscrollpane-to-a-jlabel

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