简体   繁体   中英

JScrollPane not showing in my JTextArea

I have a JTextArea named as txtChat which is used as the chatting area in my chatbot. I have created a JScrollPane scroll = new JScrollPane(txtChat) and added my txtChat inside the JScrollPane. My JTextArea is being put inside a Panel which has BorderLayout, so I have added my scroll to the panel which contains my JTextArea chatPanel.add(scroll, BorderLayout.East). BUT IT IS NOT WORKING! Like when my conversation go longer, the scroll does not appear! Sorry the code is quite long, so I just pasted the essential parts here.

public class GUI_V3 extends JFrame {

//mainPanel into Frame
private JPanel mainPanel = new JPanel();

//clock component
private JTextField timeF = new JTextField(8);

//Button component
private Box btnBox = Box.createHorizontalBox();
private JButton button1 = new JButton("Add Keywords");
private JButton button2 = new JButton("Help");

//Top menu bar
private JPanel topPanel = new JPanel();

//Typing Area
private JTextField txtEnter = new JTextField();
//Typing Panel
private JPanel typingPanel = new JPanel();

//Chat Area
private JTextArea txtChat = new JTextArea();

private JPanel chatPanel = new JPanel();

//Scroll
private JScrollPane scroll = new JScrollPane(txtChat);

private String name;

private Conversation_V3 convo = new Conversation_V3();

public GUI_V3(){
    //Frame attributes
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(1000,1000);
    this.setVisible(true);
    //this.setResizable(false);
    this.setLocationRelativeTo(null);
    this.setTitle("Welcome to Haidilao");

    //clock attributes
    timeF.setEditable(false);
    timeF.setFont(new Font("Arial",Font.BOLD,48));
    timeF.setBackground(new Color(228,224,199));
    timeF.setHorizontalAlignment(SwingConstants.RIGHT);

    //button attributes
    button1.setFont(new Font("Arial",Font.PLAIN,38));
    button2.setFont(new Font("Arial",Font.PLAIN,38));
    btnBox.add(button1);
    btnBox.add(Box.createRigidArea(new Dimension(10,70)));
    btnBox.add(button2);

    //Add ActionListener to buttons
    Button1Handler handlerB1 = new Button1Handler();
    button1.addActionListener(handlerB1);
    Button2Handler handlerB2 = new Button2Handler();
    button2.addActionListener(handlerB2);

    //Top menu bar
    topPanel.setLayout(new BorderLayout());
    topPanel.add(btnBox, BorderLayout.WEST);
    topPanel.add(timeF, BorderLayout.EAST);
    topPanel.setBackground(new Color(228,224,199));

    //Typing Area Attribute
    txtEnter.setFont(new Font("DejaVu Sans",Font.PLAIN,30));
    typingPanel.setBorder(BorderFactory.createEmptyBorder(10,20,10,20));
    typingPanel.setLayout(new BorderLayout());
    typingPanel.setBackground(new Color(228,224,199));
    typingPanel.add(txtEnter);

    //Add action listener for typing area
    TypingHandler handlerT = new TypingHandler();
    txtEnter.addActionListener(handlerT);



    //Chat Area Attribute
    txtChat.setEditable(false);
    txtChat.setFont(new Font("DejaVu Sans",Font.PLAIN,30));
    txtChat.setLineWrap(true);
    txtChat.setWrapStyleWord(true);

    chatPanel.setBorder(BorderFactory.createEmptyBorder(5,20,10,20));
    chatPanel.setLayout(new BorderLayout());
    chatPanel.setBackground(new Color(228,224,199));
    chatPanel.add(txtChat, BorderLayout.CENTER);

    chatPanel.add(scroll, BorderLayout.EAST);

    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);


    //mainPanel Layout
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(topPanel, BorderLayout.NORTH);
    mainPanel.add(typingPanel, BorderLayout.SOUTH);
    mainPanel.add(chatPanel, BorderLayout.CENTER);



    //Add components to the JFrame
    this.add(mainPanel);
    //Add actionListener to clock
    ClockHandler handlerC = new ClockHandler();
    Timer t = new Timer(500, handlerC);
    t.start();

    //display greetings and ask for name
    name = greetings();
    addText(botSays("Welcome! " + name + ", you can ask me anything about the menu by providing keywords."));
}

//Action handler for txtEnter
private class TypingHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        //grab input
        String input = txtEnter.getText();
        //add to chat area
        txtChat.append(name + ": " + input + "\n");
        //set input field to empty
        txtEnter.setText("");
        SoundEffect.music();
        convo.setInput(input);
        addText(botSays(convo.getReply()));
        //set the chat area to bottom of scroll
        txtChat.setCaretPosition(txtChat.getDocument().getLength());
    }
}

Please find a working solution and tweak this as per your requirements.

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class TestFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestFrame frame = new TestFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);

        JTextArea textArea = new JTextArea();
        scrollPane.setViewportView(textArea);
    }
}

Hope this will be helpful. :-)

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