0

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);
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mazzy
  • 13,354
  • 43
  • 126
  • 207

1 Answers1

2

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.

Zéychin
  • 4,135
  • 2
  • 28
  • 27
  • -1 no, don't setXXSize _ever_ (see http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi/7229519#7229519) – kleopatra Jan 08 '12 at 12:51
  • As much as you would like to believe otherwise, considering that you are promoting your own answer, there are times where it it acceptable to set a preferred size for a component, namely when one can guarantee that the font/image style/size will fit 'regularly' within the component for a given application. There is a split community argument along these lines and so it is up to preference, but anyway, I was not advising upon best practice, but upon how one should fix the error mentioned, while maintaining the intended styling attributes. – Zéychin Jan 08 '12 at 13:16
  • _when one can guarantee_ - that's one of the points: you can't ;-) As to the split argument: sure, there always (since the very beginning of Swing/LayoutManager nearly 3 lustrums ago) was a certain fraction of resistance to follow the winds. Which is perfectly fine as a personal decision. – kleopatra Jan 08 '12 at 13:37