0

I am trying to make a JTextArea vertically scrollable. I did some research and I'm pretty sure I'm using a LayoutManager, not adding JTextArea directly to the parent panel, and is setting the preferred size of both JTextArea and JScrollPane. Not sure what am I missing here... Here's the code:

public class ConsolePane extends JDialog {
    private static final long serialVersionUID = -5034705087218383053L;

    public static final Dimension CONSOLE_DIALOG_SIZE = new Dimension(400, 445);
    public static final Dimension CONSOLE_LOG_SIZE = new Dimension(400, 400);
    public static final Dimension CONSOLE_INPUT_SIZE = new Dimension(400, 25);

    private static ConsolePane instance = new ConsolePane();

    public static ConsolePane getInstance() {
        return instance;
    }

    private JTextArea taLog;
    private JTextField tfInput;

    public ConsolePane() {
        this.setTitle("Console");
        JPanel contentPane = new JPanel();
        this.setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout());
        contentPane.add(createConsoleLog(), BorderLayout.CENTER);
        contentPane.add(createConsoleInput(), BorderLayout.SOUTH);
        contentPane.setPreferredSize(CONSOLE_DIALOG_SIZE);
    }

    private JComponent createConsoleLog() {
        taLog = new JTextArea();
        taLog.setLineWrap(true);
        taLog.setPreferredSize(CONSOLE_LOG_SIZE);

        ((DefaultCaret) taLog.getCaret())
                .setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

        JScrollPane container = new JScrollPane(taLog);
        container
                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        container
                .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        container.setPreferredSize(CONSOLE_LOG_SIZE);
        return container;
    }

    private JComponent createConsoleInput() {
        tfInput = new JTextField();
        tfInput.setPreferredSize(CONSOLE_INPUT_SIZE);
        tfInput.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                taLog.append(tfInput.getText() + "\r\n");
                tfInput.setText("");
            }
        });
        tfInput.requestFocus();
        return tfInput;
    }

    public static void main(String[] args) {
        ConsolePane.getInstance().pack();
        ConsolePane.getInstance().setVisible(true);
    }
}

Thx in advance!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jim Zhai
  • 21
  • 3
  • `taLog = new JTextArea(); .. taLog.setPreferredSize(CONSOLE_LOG_SIZE);` would best be replaced by something like (adjust cols & rows to need) `taLog = new JTextArea(3,20); ..` also `tfInput = new JTextField(); tfInput.setPreferredSize(CONSOLE_INPUT_SIZE);` would better be `tfInput = new JTextField(15);` – Andrew Thompson Nov 18 '14 at 05:24
  • More generally, see [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Nov 18 '14 at 05:25
  • 1
    Yeah, just resolve this myself. taLog.setPreferredSize() is preventing the scrolling. removed that and everything worked fine. I'm definitely giving the article a read. Thx Andrew! – Jim Zhai Nov 18 '14 at 05:28
  • Glad you got it sorted. :) – Andrew Thompson Nov 18 '14 at 05:30

2 Answers2

0

Try the following code, It may help you

JTextArea txt=new JTextArea();
JScrollPane pane=new JScrollPane(txt,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Anptk
  • 1,125
  • 2
  • 17
  • 28
0

Figured it out myself. taLog.setPreferredSize() is preventing the scrolling. removed that and everything worked fine. Thx everyone for your help.

Jim Zhai
  • 21
  • 3