简体   繁体   中英

Java: Make JTextArea scrollable

This is my first post so please forgive me if I am not following the rules correctly.

I am trying to do something relatively simple in Java. I want to make a JTextArea scrollable. I am aware this has been asked before ( Java :Add scroll into text area ). However, when I follow this example by adding JTextArea to JScrollPane my JTextArea does not become scrollable. What is it that I am missing?

Here is my code:

    import ...;

    public class MyControlPanel extends JPanel {

    //Declare variables
    private JComboBox accountsBox;
    private String[] accType = {"Current Account", "Savings Account"};
    private JLabel selAccType, initDeposit, logLabel, simLabel;
    private JTextField depositText;
    private JTextArea log;
    private JScrollPane scroll;
    private JButton createAccount, start, stop;
    private JPanel panel1, panel2, panel3, panel4, panel5, panel6;
    private Timer timer;
    private MyAccount theAccount;
    private Random randNum1, randNum2;
    private DecimalFormat df;

    //Constructor
    public MyControlPanel() {

        //Create instances:
        selAccType = new JLabel("Please select account type: "); //JLabel
        initDeposit = new JLabel("Input initial deposit: ");
        logLabel = new JLabel("Log:");
        simLabel = new JLabel();
        accountsBox = new JComboBox(accType); //JComboBox
        depositText = new JTextField("0"); // JTextField
        log = new JTextArea(); //JTextArea
        scroll = new JScrollPane(log); //JScrollPane
        createAccount = new JButton("Create Account"); //JButton
        start = new JButton("Start");
        stop = new JButton("Stop");
        panel1 = new JPanel(); //JPanel
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel4 = new JPanel();
        panel5 = new JPanel();
        panel6 = new JPanel();
        timer = new Timer(); //Timer
        df = new DecimalFormat("#.00");

        //Add ActionListeners
        createAccount.addActionListener(new ActionListener() {...});

        start.addActionListener(new ActionListener() {...});

        stop.addActionListener(new ActionListener() {...});

        //Set JTextField size
        depositText.setColumns(5);

        //Set JTextArea size
        log.setPreferredSize(new Dimension(780, 150));

        //Set panel size
        panel1.setPreferredSize(new Dimension(500, 500));
        panel2.setPreferredSize(new Dimension(800, 50));
        panel3.setPreferredSize(new Dimension(500, 50));
        panel4.setPreferredSize(new Dimension(500, 50));
        panel5.setPreferredSize(new Dimension(800, 25));
        panel6.setPreferredSize(new Dimension(800, 200));

        //Set layout in panel5 to align left
        panel6.setLayout(new FlowLayout(FlowLayout.LEFT));

        //Add components to each panel
        addPanels();

        //Place objects in the framed window
        this.add(panel1);
        this.add(panel2);
        this.add(panel3);
        this.add(panel4);
        this.add(panel5);
        this.add(panel6);
    }

    public void addPanels() {...}

    public void removePanels() {...}

}

You need to change:

log.setPreferredSize(new Dimension(780, 150));

to:

scroll.setPreferredSize(new Dimension(780, 150));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM