Search code examples
javaswingjscrollpanejtextarea

the scroll bar does not work


Hi here it's the code of my scroll bar.unfortunately it does not work. how could it be?

text_area = new JTextArea();
text_area.setPreferredSize(new Dimension(250,150));
text_area.setLineWrap(true);
scrollpane = new JScrollPane(text_area); 

when i insert a text and it's longer than the area the scroll bar does not appear.

here it is the code:

public AziendaGUI() {

    company = new Azienda();

    frame = new JFrame("Immobiliari s.p.a");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    view_list = new JButton("View Property");
    view_list.setActionCommand("view_list");
    view_list.addActionListener(this);

    save_list = new JButton("Save List");
    save_list.setActionCommand("save_list");
    save_list.addActionListener(this);

    text_area = new JTextArea();
    text_area.setPreferredSize(new Dimension(250,150));
    text_area.setLineWrap(true);
    scrollpane = new JScrollPane(text_area); //Non funziona la scroll bar

    grid = new GridBagLayout();
    pane = new JPanel(grid);

    /* Set Constraints view_list button */
    grid.setConstraints(view_list, new GridBagConstraints(0,0,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
    pane.add(view_list);

    /* Set Constraints save_list button */
    grid.setConstraints(save_list,new GridBagConstraints(1,0,1,1,0.0,0.0,GridBagConstraints.EAST,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
    pane.add(save_list);

    /* Set Constraint text area */
    grid.setConstraints(scrollpane, new GridBagConstraints(0,1,2,1,0.0,0.0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
    pane.add(scrollpane);

    frame.setLayout(new FlowLayout());
    frame.add(pane);

    frame.pack();
    frame.setVisible(true);
}

Solution

  • Set the preferred size of the JScrollPane.

    Do not set the preferred size of the JTextArea.

    Source: I slightly modified the source and it worked.