How to create a Scrolling Pane for a Chat System?

45 views Asked by At

I've already created a working scrolling pane using JScrollPane, however, the current code works as follows: I type something into the chat input, press enter, it creates a JPanel and adds it to the JScrollPane. However, the JScrollPane scroll bar is fixed. Is there a property where I can change it's canvas size so I can actually "scroll" the scrolling bar. Or is there a simpler method to achieve what I'm trying to do?

ChatUI.java

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.Font;
import java.awt.Image;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

public class ChatUI {
    static JTextField chat_input = new JTextField(8);
    static JPanel inner_background = new JPanel();
    static JScrollPane scrollPane = new JScrollPane(inner_background);

    public static String getTextFieldText() {
        return chat_input.getText();
    }

    public static void createScrollingList(JLayeredPane layeredPane) {
        // Set the layout manager for the inner background panel
        inner_background.setLayout(new BoxLayout(inner_background, BoxLayout.Y_AXIS));

        // Set the preferred size of inner_background to be larger than the visible area
        inner_background.setPreferredSize(new Dimension(300, 1000));

        // Other settings for the inner background panel
        inner_background.setBackground(Color.decode("#111214"));
        inner_background.setBorder(new LineBorder(Color.decode("#111214"), 2, true));

        layeredPane.add(inner_background, Integer.valueOf(0));

        // Configure the JScrollPane
        scrollPane.setBounds(20, 200, 330, 450);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.getViewport().setBackground(Color.decode("#111214"));

      
        scrollPane.setPreferredSize(new Dimension(400, 1000)); // Set a larger preferred size
      //  scrollPane.setLayout(new BoxLayout(inner_background, BoxLayout.Y_AXIS)); // Use BoxLayout for vertical layout

        // Add some components to the content panel
        for (int i = 0; i < 20; i++) {
            JButton button = new JButton("Button " + i);
            scrollPane.add(button);
        }
        // Add the JScrollPane to the layeredPane
        layeredPane.add(scrollPane, Integer.valueOf(2));
    }


    static void UIVisuals(JFrame frame, JLayeredPane layeredPane) {
        // Left frame
        JPanel topBackground = new JPanel();
        topBackground.setBackground(Color.decode("#62D0F6"));
        topBackground.setBounds(0, 0, 500, 80);

        // Right frame
        JPanel rightFrame = new JPanel();
        rightFrame.setBackground(Color.decode("#9B90B2"));
        rightFrame.setBounds(20, 40, 20, 20);

        // Add frames to layeredPane
        layeredPane.add(rightFrame, Integer.valueOf(1));
        layeredPane.add(topBackground, Integer.valueOf(1));

       // User_Icon
        ImageIcon userIcon = new ImageIcon("C:/Users/pokec/OneDrive/Pictures/user_java_icon.png");
        JLabel userLabel = new JLabel(userIcon);
        userLabel.setBounds(0, 30, userIcon.getIconWidth(), userIcon.getIconHeight()); // Adjust the bounds as needed
        layeredPane.add(userLabel, Integer.valueOf(2));

        // Emoji Button
        ImageIcon imageIcon = new ImageIcon("C:\\Users\\pokec\\OneDrive\\Pictures\\gift_icon.png");

        // Create a JButton and set the image icon
        JButton gift_button = new JButton(imageIcon);
        gift_button.setBounds(320, 670, 40, 40);
        layeredPane.add(gift_button, Integer.valueOf(2));

        // Make button transparent
        gift_button.setOpaque(false);
        gift_button.setContentAreaFilled(false);
        gift_button.setBorderPainted(false);

        // Texting Panel
        chat_input.setBackground(Color.decode("#383A40"));
        chat_input.setForeground(Color.WHITE);
        chat_input.setBorder(new LineBorder(Color.decode("#4B4D54"), 1, true));

        // Font setup
        Font f3 = new Font(Font.DIALOG, Font.BOLD, 15);
        chat_input.setFont(f3);

        // Adjust the position and size of the chat input
        chat_input.setBounds(20, 670, 280, 40);
        layeredPane.add(chat_input, Integer.valueOf(1));

        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(null); // Set null layout for precise positioning
        bottomPanel.add(chat_input);

        // Add components to layeredPane
        layeredPane.add(chat_input, Integer.valueOf(1));
        layeredPane.add(bottomPanel, BorderLayout.SOUTH);

        // Event Listeners
        EventListeners eventListeners = new EventListeners(gift_button, chat_input);
        chat_input.addActionListener(eventListeners);
        gift_button.addActionListener(eventListeners);

        createScrollingList(layeredPane);



        
    }
}

EventListeners.java

  import java.awt.Color;
import java.awt.Dimension;
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.JTextField;
import javax.swing.JViewport;

public class EventListeners implements ActionListener {
    private final JButton button;
    private final JTextField textField;

    public EventListeners(JButton gift_button, JTextField textField) {
        this.button = gift_button;
        this.textField = textField;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            //Gift Button
                System.out.println("Button clicked");
              
        } else if (e.getSource() == textField) {
            //Enter 
            String input = textField.getText();
            if (!input.isEmpty()) {
                     System.out.println("Enter key pressed: " + input);
                textField.setText("");

                // Create a JPanel to add to the JScrollPane
                JPanel chatPanel = new JPanel();
                chatPanel.setPreferredSize(new Dimension(20, 10)); // Set the preferred size
                chatPanel.setBackground(Color.YELLOW); // Set the background color or customize as needed

                // Add the chatPanel to the JScrollPane's viewport
                JViewport viewport = ChatUI.scrollPane.getViewport();
                viewport.add(chatPanel);

                // Repaint the viewport to reflect changes
                viewport.repaint();
            }
        }
    }
}

I've attempted changing the size of the panel that gets added into the ScrollingPane and it doesn't seem to change in size and rather covers the entire scrolling Pane. Again, not sure if there is a proprety I'm not aware of about JScrollPane that can change its canvas size resulting in the scroll bar to actually be scrollable.

https://i.stack.imgur.com/aTTg0.png

0

There are 0 answers