Question

I am creating a simple messenger in Java(just for learning purposes) and I am trying to have a friends tab that, on the left side is the list of friends, and the right side is the messages with the friend that you click on, but whenever I try to add the JSplitPane in the tab, but it doesn't display. I have done this exact same code(except I only did the JSplitPane stuff and its components, not the other tabs and the menus and such.

All my code

package Client;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ClientMain extends JFrame {

int WIDTH = 640;
int HEIGHT = 480;

JTabbedPane tabbedPane;
JMenuBar topMenuBar;
JMenu userMenu, helpMenu, settingsMenu;
JRadioButtonMenuItem menuItem;
JPanel mainPanel, friendsPanel, groupsPanel, testPanel;
JLabel title;
JScrollPane consoleScrollPane, friendChatScrollPane, friendListScrollPane;
JSplitPane friendsSplitPane;
JTextArea friendChatArea, testArea;
JTextField friendChatField, testField;
JList friendList;
Box box;

DefaultListModel friends;

public ClientMain() {
    super("Messenger Client");

    //Networking();


    /** MAIN PANEL **/


        title = new JLabel("Client!");
        title.setFont(new Font("Impact", Font.PLAIN, 32));

        mainPanel = new JPanel();
        mainPanel.setLayout(null);
        mainPanel.add(title);

    /** TEST PANEL **/


        groupsPanel = new JPanel();
        groupsPanel.setLayout(null);


    /** FRIENDS PANEL **/


        friendsPanel = new JPanel();
        friendsPanel.setLayout(null);

        friends = new DefaultListModel();
        //method here that retrieves users' friends from the server and adds them to the friends DefaultListModel
        friends.addElement("Person1");
        friends.addElement("Person2");

        friendList = new JList(friends);
        friendList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        friendList.setLayoutOrientation(JList.VERTICAL_WRAP);
        friendList.setVisibleRowCount(3);
        friendList.setSize(50, 50);

        friendChatArea = new JTextArea();
        friendChatArea.setSize(50, 50);
        friendChatArea.setFont(new Font("", Font.PLAIN, 13 + 1/2));
        friendChatArea.append("TEST");

        Dimension minimumSize = new Dimension(100, 50);
        friendListScrollPane = new JScrollPane(friendList);
        friendListScrollPane.setMinimumSize(minimumSize);

        friendChatScrollPane = new JScrollPane(friendChatArea);
        friendChatScrollPane.setMinimumSize(minimumSize);

        friendsSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, friendListScrollPane, friendChatScrollPane);
        friendsSplitPane.setOneTouchExpandable(false);
        friendsSplitPane.setDividerLocation(50);

        friendsPanel.add(friendsSplitPane);

    /** TEST PANEL **/


        testPanel = new JPanel();
        testPanel.setLayout(null);

        testArea = new JTextArea();
        testArea.setFont(new Font("", Font.PLAIN, 13 + 1/2));
        testArea.setBounds(0, 0, WIDTH, HEIGHT - 100);
        testArea.setEditable(false);
        testArea.setLineWrap(true);

        testField = new JTextField(20);
        testField.setBounds(0, 380, 640, 25);
        //testField.setLocation(0, HEIGHT - 50);
        testField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ClientNet net = new ClientNet();
                String input = null;
                input = testField.getText();
                testArea.append(input);
                testField.setText("");

                if(input.equalsIgnoreCase("/sendmessage")) {
                    testArea.append("\n Input who you would like to send the message to:");
                    input = null;

                    if(input.equalsIgnoreCase("Hello")) {
                        net.userEntry = input;
                    }
                }
            }
        });

        testPanel.add(testArea);
        testPanel.add(testField);


    /** SET UP **/


        tabbedPane = new JTabbedPane();

        tabbedPane.add(mainPanel, "Main");
        tabbedPane.add(friendsPanel, "Friends");
        tabbedPane.add(groupsPanel, "Groups");
        tabbedPane.add(testPanel, "Test");

        topMenuBar = new JMenuBar();

        userMenu = new JMenu("User");

        settingsMenu = new JMenu("Settings");

        helpMenu = new JMenu("Help");

        menuItem = new JRadioButtonMenuItem("Something here");

        userMenu.add(menuItem);

        topMenuBar.add(userMenu, "User");
        topMenuBar.add(settingsMenu, "Settings");
        topMenuBar.add(helpMenu, "Help");

        add(topMenuBar);
        add(tabbedPane);
}

public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    } catch(UnsupportedLookAndFeelException e) {
        e.printStackTrace();

    } catch (ClassNotFoundException e) {
        e.printStackTrace();

    } catch (InstantiationException e) {
        e.printStackTrace();

    } catch (IllegalAccessException e) {
        e.printStackTrace();

    }

    ClientMain frame = new ClientMain();
    Insets insets = frame.getInsets();
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setSize(frame.WIDTH, frame.HEIGHT);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setJMenuBar(frame.topMenuBar);

}

public void Networking() {
    ClientNet net;
    try {
        net = new ClientNet();
        net.start();

    } catch(Exception e) {
        e.printStackTrace();
    }
}

public void createMenuBar() {
    topMenuBar = new JMenuBar();

    userMenu = new JMenu("User");
    settingsMenu = new JMenu("Settings");
    helpMenu = new JMenu("Help");

    menuItem = new JRadioButtonMenuItem("MenuItem");
    menuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

        }
    });

    topMenuBar.add(userMenu, "User");
    topMenuBar.add(settingsMenu, "Settings");
    topMenuBar.add(helpMenu, "Help");
}

public void createSplitPane() {
    friendListScrollPane = new JScrollPane();
    friendListScrollPane.add(new JLabel("Hello"));

    friendChatArea = new JTextArea();
    friendChatArea.setBounds(0, 150, HEIGHT, HEIGHT);
    friendChatArea.setFont(new Font("", Font.PLAIN, 13 + 1/2));

    Dimension minimumSize = new Dimension(WIDTH/2 , HEIGHT);
    friendListScrollPane.setMinimumSize(minimumSize);

    //friendsSplitPane.setLeftComponent()
    friendsSplitPane.add(friendChatArea);
    friendsSplitPane.setRightComponent(friendChatScrollPane);
}

}

The specific JSplitPane code(part of the code above)

friendsPanel = new JPanel();
        friendsPanel.setLayout(null);

        friends = new DefaultListModel();
        //method here that retrieves users' friends from the server and adds them to the friends DefaultListModel
        friends.addElement("Person1");
        friends.addElement("Person2");

        friendList = new JList(friends);
        friendList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        friendList.setLayoutOrientation(JList.VERTICAL_WRAP);
        friendList.setVisibleRowCount(3);
        friendList.setSize(50, 50);

        friendChatArea = new JTextArea();
        friendChatArea.setSize(50, 50);
        friendChatArea.setFont(new Font("", Font.PLAIN, 13 + 1/2));
        friendChatArea.append("TEST");

        Dimension minimumSize = new Dimension(100, 50);
        friendListScrollPane = new JScrollPane(friendList);
        friendListScrollPane.setMinimumSize(minimumSize);

        friendChatScrollPane = new JScrollPane(friendChatArea);
        friendChatScrollPane.setMinimumSize(minimumSize);

        friendsSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, friendListScrollPane, friendChatScrollPane);
        friendsSplitPane.setOneTouchExpandable(false);
        friendsSplitPane.setDividerLocation(50);

        friendsPanel.add(friendsSplitPane);
Was it helpful?

Solution

friendsPanel.setLayout(null);

your friendsPanel has the null layout and you are adding split pane as:

friendsPanel.add(friendsSplitPane);

to add a component to a container(friendsPanel) with null layout, you must specify the bound of the component you are adding with component.setBounds() method. But seriously why are even using null layout ? Don't use it. It has been already advice from page to page in by the swing family of stack overflow. Try to use one of the layout manager the Swing developer has created for us with effort wasting time from day after day just to save our own time.

Start learning : Lesson: Laying Out Components Within a Container. Let us give some value their efforts for saving our time, which we are spending just to find the answer: uhh! where is my component!?!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top