Results 1 to 2 of 2
  1. #1
    gshadrin is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Question JScrollPane words wrap

    Hello.

    I have a problem. In the JScrollPane I use inheritor of the JPanel as viewPort. The inheritor can contain labels, radio buttons, check boxes and other elements (depending on the implementation). In the JScrollPane set HORIZONTAL_SCROLLBAR_NEVER. It is necessary that contents in the inheritor of the JPanel scrolling in the vertical and horizontal words wrap. When displaying long texts in the labels (special in html tags) there is no word wrap, despite HORIZONTAL_SCROLLBAR_NEVER. If don't use JScrollPane (inheritor of the JPanel has concrete size) words to be wrapped, but I need a vertical scrolling.

    Tried set listeners for resize JScrollPane and set necessary size for internal JPanel. Did not work. Set listeners for resize internal JPanel yet. After setting the width is happening another change back to (obviously of great importance).

    How can I solve this problem? Requires scrolling in the vertical and horizontal words wrap. Thanks in advance. Sorry, for my English).

    Example:
    JScrollPane words wrap-wrap2.png
    Java Code:
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JScrollPane;
    
    public class Test extends JFrame
    {
    
    private JPanel contentPane;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    Test frame = new Test();
                    frame.setVisible(true);
    
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public Test()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] { 0, 0 };
        gbl_contentPane.rowHeights = new int[] { 0, 0 };
        gbl_contentPane.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
        gbl_contentPane.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
        contentPane.setLayout(gbl_contentPane);
    
        JScrollPane scrollPane = new JScrollPane();
        GridBagConstraints gbc_scrollPane = new GridBagConstraints();
        gbc_scrollPane.fill = GridBagConstraints.BOTH;
        gbc_scrollPane.gridx = 0;
        gbc_scrollPane.gridy = 0;
        contentPane.add(scrollPane, gbc_scrollPane);
    
        JPanel panel = new JPanel();
        scrollPane.setViewportView(panel);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);        
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{0, 0};
        gbl_panel.rowHeights = new int[]{0, 0};
        gbl_panel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
        gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);
    
        JLabel lblNewLabel = new JLabel("<html>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</html>");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 0;
        panel.add(lblNewLabel, gbc_lblNewLabel);
    }
    
     }
    Example word wrap in JLabel with `tag html`:
    JScrollPane words wrap-wrap.png
    Java Code:
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    
    public class Test extends JFrame
    {
    
    private JPanel contentPane;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    Test frame = new Test();
                    frame.setVisible(true);
    
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public Test()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] { 0, 0 };
        gbl_contentPane.rowHeights = new int[] { 0, 0 };
        gbl_contentPane.columnWeights = new double[] { 0.0, Double.MIN_VALUE };
        gbl_contentPane.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
        contentPane.setLayout(gbl_contentPane);
    
        JLabel lblNewLabel = new JLabel(
                "<html>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</html>");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.weightx = 1.0;
        gbc_lblNewLabel.fill = GridBagConstraints.BOTH;
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 0;
        contentPane.add(lblNewLabel, gbc_lblNewLabel);
    }
    
      }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: JScrollPane words wrap

    Cross posted and adequately answered
    java - JScrollPane words wrap - Stack Overflow

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Center AND wrap text
    By Megabuffen in forum AWT / Swing
    Replies: 2
    Last Post: 12-11-2011, 01:34 AM
  2. JTextArea Wrap Around or something?
    By Krooger in forum New To Java
    Replies: 6
    Last Post: 11-28-2009, 04:29 AM
  3. How to wrap words in checkbox,push buttons
    By gunjan.raizada in forum SWT / JFace
    Replies: 3
    Last Post: 08-05-2008, 12:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •