javaswingjbuttonjtextfieldjscrollbar

How do i add a scrollbar with JtextArea and implement JScrollPane in a GUI?


I need help adding a scrollbar to an empty textbox that will output the information that i input into the required textboxes by then clicking the buttons. My issue is im not sure how to implement the scrollbar correctly with the textbox that is in the code, it just goes over and it and does nothing like in the picture shown. I need it to align with the textbox and not mess with the Jtextfield so that it can properly scroll the textbox, i will handle the events after i finish the design.

Code:

    // Display box for all the inputs
    JTextField outputBox = new JTextField(5);
    wv.add(outputBox, 39, 575, 800, 150);
    JScrollBar outputBoxScrollBar = new JScrollBar(JScrollBar.VERTICAL, 30, 20, 0, 500);
    wv.add(outputBoxScrollBar, 790, 300, 50 , 250);

Output:

GUI Picture with the scrollbar and Jtextfield box

https://i.sstatic.net/oHnY8.png - Link for different OS


Solution

    1. A JTextField is single-line, I guess you are looking for JTextArea.
    2. You need to wrap JTextArea in a JScrollPane (not a JScrollBar) like this.
            JTextArea outputBox = new JTextArea(5, 20); //rows, columns
            wv.add(new JScrollPane(outputBox), 790, 300, 50 , 250);