Search code examples
javascrolljframejpanel

Invisible textarea when I add scroll Jpanel


When I add the scrollbar, I can't see the chat output and inputbar. If I delete the scrollbar, everything is ok. I don't know why... Scroll must be vertical. Are there any other ways to display vertical text in chat? This is my code:

public class ChatBot extends JFrame{

private JTextField inputBar = new JTextField();  
private JTextArea chatOutput = new JTextArea();
private JScrollPane scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

public ChatBot() {

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(600, 600);
    frame.setLocationRelativeTo(null);  
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setLayout(null);
    frame.setTitle("ChatBot");
    frame.add(inputBar);
    frame.add(chatOutput);
    frame.setContentPane(scroll);
    frame.setVisible(true);
    
    frame.addWindowListener(new WindowListener() { 
        
        @Override
        public void windowClosing(WindowEvent e) {  
            not_open = true;
        }
        
        @Override
        public void windowOpened(WindowEvent e) {
        }
        @Override
        public void windowClosed(WindowEvent e) {
        }
        @Override
        public void windowIconified(WindowEvent e) {
        }
        @Override
        public void windowDeiconified(WindowEvent e) {
        }
        @Override
        public void windowActivated(WindowEvent e) {
        }
        @Override
        public void windowDeactivated(WindowEvent e) {
        }
        
    });
           
    inputBar.setLocation(2, 500);
    inputBar.setSize(540, 30);
    
    chatOutput.append("- ChatBot: Hello");
    
    inputBar.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0) {
            String userInput = inputBar.getText();        
            chatOutput.append("- You: " + userInput + "\n");
            
            if(userInput.contains("Chat")){
                    botOutput("Connected...");
            }else{
                    botOutput("Invalid command");
            }
            inputBar.setText("");
        }
    });
    
    //Chat output
    chatOutput.setSize(538, 493);
    chatOutput.setLocation(2, 2);
    chatOutput.setBackground(Color.white);
    chatOutput.setLineWrap(true);
    chatOutput.setEditable(false);

}

public void botOutput(String s){
    chatOutput.append("- ChatBot: " + s + "\n");
}
        
}

What's wrong? It seems the problem is jpanel scroll. This is a simple chatbot in java


Solution

  • Added a layout and assigned the JTextArea to the JScrollpane. Hope it helps.

    public class ChatBot {
    
        private JTextField inputBar = new JTextField();
        private JTextArea chatOutput = new JTextArea();
        private JScrollPane scroll = new JScrollPane(chatOutput, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    
        public ChatBot() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setSize(600, 600);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.setResizable(false);
            frame.setLayout(new BorderLayout());
            frame.setTitle("ChatBot");
            inputBar.setPreferredSize(new Dimension(1, 30));
            frame.add(inputBar, BorderLayout.SOUTH);
            frame.add(scroll, BorderLayout.CENTER);
            frame.setVisible(true);
    
            frame.addWindowListener(new WindowListener() {
    
                @Override
                public void windowClosing(WindowEvent e) {
                     //not_open = true;
                }
    
                @Override
                public void windowOpened(WindowEvent e) {
                }
    
                @Override
                public void windowClosed(WindowEvent e) {
                }
    
                @Override
                public void windowIconified(WindowEvent e) {
                }
    
                @Override
                public void windowDeiconified(WindowEvent e) {
                }
    
                @Override
                public void windowActivated(WindowEvent e) {
                }
    
                @Override
                public void windowDeactivated(WindowEvent e) {
                }
    
            });
    
            chatOutput.append("- ChatBot: Hello");
    
            inputBar.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    String userInput = inputBar.getText();
                    chatOutput.append("- You: " + userInput + "\n");
    
                    if (userInput.contains("Chat")) {
                        botOutput("Connected...");
                    } else {
                        botOutput("Invalid command");
                    }
                    inputBar.setText("");
                }
            });
    
            //Chat output
            chatOutput.setBackground(Color.white);
            chatOutput.setLineWrap(true);
            chatOutput.setEditable(false);
        }
    
        public void botOutput(String s) {
            chatOutput.append("- ChatBot: " + s + "\n");
        }
    }