简体   繁体   中英

NON-editable autoscrolling JTextArea

I have a JScrollPane with a JTextArea added to it. I then add the ScrollPane to the panel created for the gui. when running everything works as planned. The text that is typed into the input section is removed from input and added to the output. However when the text exceeds the size of the JTextArea it refuses to scroll after it. The code i will be providing is a lot as i do not know where the issue is or how to fix it.

I have browsed StackOverflow and have tried many different things i will give some links. How to set AUTO-SCROLLING of JTextArea in Java GUI? . as well as i have used the oracle website. https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html . but none of them have helped me.

public Sork()
{
    txtara = new JTextArea("");
    panel = new JPanel();
    txtfld = new JTextField(""); 
    sb = new JScrollBar();
    scrollBar = new JScrollPane(txtara, 
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    txtfld.setPreferredSize(new Dimension(740, 20));
    txtfld.setLocation(new Point (0, 510));
    txtfld.setBackground(Color.BLACK);
    txtfld.setForeground(Color.WHITE);

    txtara.setPreferredSize(new Dimension(740, 510));
    txtara.setBackground(Color.BLACK);
    txtara.setForeground(Color.WHITE);
    txtara.setEditable(false);

    panel.setPreferredSize(new Dimension(750, 575));
    panel.setForeground(Color.BLACK);
    panel.setBackground(Color.BLACK);

    panel.add(scrollBar);
    panel.add(txtfld);

    txtfld.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e)
        {               
            txtara.append("\n" + txtfld.getText());;
            txtfld.setText("");
            txtfld.grabFocus(); 
        }});
    }

I want the JTextArea to auto scroll once the text reaches the bottom. I should also mention i would like the actual scrollbar to not be visible, this is being made for a text based adventure. therefore the less showing the better.

The following works for me. It uses hardly any of your code because I felt it would be more beneficial to you if I started from scratch. The code below displays a JTextField where you enter text. Below the JTextField is a (non-editable) JTextArea . Hitting the Enter key when keyboard focus is in the JTextField will append its text to the JTextArea . I recommend you make sure you thoroughly understand the code as I think it will push you along the Swing learning curve.

For your information the below code requires at least Java 7 since it uses multi-catch .

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

public class Sork implements ActionListener, Runnable {
    private JButton exitButton;
    private JFrame frame;
    private JTextArea txtara;
    private JTextField txtfld;

    @Override // java.awt.event.ActionListener
    public void actionPerformed(ActionEvent actnEvnt) {
        Object src = actnEvnt.getSource();
        if (src == exitButton) {
            System.exit(0);
        }
        else if (src == txtfld) {
            txtara.append("\n");
            txtara.append(txtfld.getText());
            txtfld.setText("");
        }
    }

    @Override // java.lang.Runnable
    public void run() {
        showGui();
    }

    private JPanel createButton() {
        JPanel panel = new JPanel();
        exitButton = new JButton("Exit");
        exitButton.addActionListener(this);
        panel.add(exitButton);
        return panel;
    }

    private JScrollPane createTextArea() {
        txtara = new JTextArea(2, 10);
        JScrollPane scrollPane = new JScrollPane(txtara);
        txtara.setLineWrap(true);
        txtara.setWrapStyleWord(true);
        txtara.setEditable(false);
        return scrollPane;
    }

    private JPanel createTextField() {
        JPanel panel = new JPanel();
        txtfld = new JTextField(10);
        txtfld.addActionListener(this);
        panel.add(txtfld);
        return panel;
    }

    private void showGui() {
        frame = new JFrame("Sork");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(createTextField(), BorderLayout.PAGE_START);
        frame.add(createTextArea(), BorderLayout.CENTER);
        frame.add(createButton(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        String slaf = UIManager.getSystemLookAndFeelClassName();
        try {
            UIManager.setLookAndFeel(slaf);
        }
        catch (ClassNotFoundException |
               InstantiationException |
               IllegalAccessException |
               UnsupportedLookAndFeelException x) {
            System.err.println("WARN (ignored): Failed to set [System] look-and-feel.");
            x.printStackTrace();
        }
        Sork instance = new Sork();
        EventQueue.invokeLater(instance);
    }
}

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