Adding a JScrollPane to JDialog

女生的网名这么多〃 提交于 2019-12-02 11:38:54

I think you have to create a JDialog, then add a JScrollPane to it and your content inside that. The following code worked for me:

import javax.swing.JDialog;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.JLabel;

public class Dialog extends JDialog {
    private final JScrollPane scrollPane = new JScrollPane();
    //I added the breaks to the label below to be able to scroll down.
    private final JLabel lblContent = new JLabel("<html><body><p>Content<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>Content<br><br><br><br><br><br>Content</p></body></html>");

    public static void main(String[] args) {
                    Dialog dialog = new Dialog();
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setVisible(true);
    }

    public Dialog() { 
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout(0, 0));

        getContentPane().add(scrollPane, BorderLayout.CENTER);

        scrollPane.setViewportView(lblContent);

    }
}

Hope this helps.

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