5

I have this Gui class:

public class Gui extends JFrame implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = -384241835772507459L;
JLabel playerInfo;
JTextField textField;
private final static String newline = "\n";
JTextArea feed;
JScrollPane scrollPane;
Player player;

public Gui() {
    super("Erik's RPG");        
    setLayout(new FlowLayout());        
    textField = new JTextField(30);     
    textField.addActionListener(this);      
    feed = new JTextArea(15, 30);
    feed.setEditable(false);    
}

public void setCurrentPlayer(Player currentPlayer) {
    player = currentPlayer;
    playerInfo = new JLabel("Health = " + currentPlayer.getHealth() + " | Mana = " + player.getMana());
    playerInfo.setBorder(BorderFactory.createTitledBorder(currentPlayer.getName()));
    add(playerInfo);
    add(feed);
    add(textField);
}

And it is annoying when a bunch of text is in the text field because it keeps making the window larger. I need to know how to make a scrollbar so the user doesn't have to keep resizing his JFrame. Also, how can I lock the dimensions of a JFrame so users can adjest the size?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Erik Balen
  • 275
  • 1
  • 4
  • 11

1 Answers1

11

Set the JTextArea instance as the viewport view of a JScrollPane instance, as shown in How to Use Scroll Panes. Add this scroll pane to the frame's content pane.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Ashwini Raman
  • 936
  • 1
  • 7
  • 11
  • 2
    Can you tell me what the code would be? I'm a little confused. – Erik Balen Sep 15 '11 at 05:36
  • It will be easier to suggest code if you provide an [sscce](http://sscce.org/) in your question. More examples may be found [here](http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html#eg). – trashgod Sep 15 '11 at 05:39
  • *"Can you tell me what the code would be?"* Can you post an [SSCCE](http://pscode.org/sscce.html)? (Even then, no guarantees are offered). – Andrew Thompson Sep 15 '11 at 05:40
  • @ebalen which part of the tutorial chapter exactly confuses you? – kleopatra Sep 15 '11 at 06:45