javaswingjscrollpanejtextareapreferredsize

JTextArea no scroll bar


private JPanel contentPane;
public Driver() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 867, 502);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    final JTextArea textArea = new JTextArea();
    textArea.setEditable(false);
    textArea.setBounds(10, 11, 831, 393);
    JScrollPane scroll = new JScrollPane(textArea);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    textArea.setText("dfgf");
    contentPane.add(scroll);
}

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Driver frame = new Driver();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

Why this doesn't not show the textArea with a scroll bar?

my problem is that I don't see even the textArea. but if I do contentPane.add(scroll); instead, I can see textArea but no scroll.


Solution

  • "why this doesn not show the textArea with a scroll bar?"

    Because the viewport of the scrollpane has its own layout manager and is changing the position in the text area to suit its needs

    The viewport will use the text area's preferred size property to determine how to lay it out. You can effect this value by adding text to the text area and/or setting the row/column properties according to your needs.

    Updated with example

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestScrollPane03 {
    
        public static void main(String[] args) {
            new TestScrollPane03();
        }
    
        public TestScrollPane03() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
                
                    JTextArea textArea = new JTextArea(100, 50);
                    JScrollPane scrollPane = new JScrollPane(textArea);
                
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(scrollPane);
                    frame.setSize(200, 200);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }    
    }
    

    Updated based on changes to question

    As camickr has already pointed out, the problem you are having is related to the how you are laying out your components.

    This contentPane.setLayout(null); is just one of your many problems. Stop using null layouts and start using appropriate layout managers.