Question

I'm expecting a box with text that wraps and a scrollbar that appears when the window becomes too small to display all text. Neither of these elements of functionality are present, however.

  JFrame f = new JFrame();
  JRootPane root = f.getRootPane();
  Container content = root.getContentPane();
  JButton button = new JButton("Generate New Phrase");
  JPanel infoPanel = new JPanel();
  JTextArea info = new JTextArea();
  JScrollPane scrilla = new JScrollPane(info);

  public CorporateSloganGenerator() throws FileNotFoundException, IOException {
      phraseGenerator = new PhraseGeneratorFromFile();

      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      infoPanel.setLayout(new FlowLayout());
      info.setColumns(40);
      info.setRows(20);
      infoPanel.add(info);
      ControlListener listensUp = new ControlListener();
      button.addActionListener(listensUp);
      content.add(infoPanel, BorderLayout.CENTER);
      content.add(button, BorderLayout.SOUTH);
      root.setDefaultButton(button);    

      info.add(scrilla);
      info.setFont(new Font("Dialog", Font.ITALIC, 28));
      button.setFont(new Font("Dialog", Font.PLAIN, 18));
      info.setLineWrap(true);
      info.setWrapStyleWord(true);
      info.setEditable(false);
      f.setVisible(true);
      f.pack();
Was it helpful?

Solution

Normally, a component is set as a scroll panes view when you want to provide scrollable support, for example, instead of...

info.add(scrilla);

You should using something like...

scrilla.setViewportView(info);

You should then add this to the frame...

f.add(scrilla);

Before making it visible...

Also, while not entirely required, it would make sense to pack the frame before you show it, for example...

f.pack();
f.setVisible(true);

You may also like to take a look at How to use scroll panes for more details

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top