Question

At the moment I write a little client application. I have a window with a JTextArea (Display area for the server output) and a user-list.

My plan is to show/hide this user-list over a menu item, but I don't know how. My ideas:

  • Use a BorderLayout: Without a JScrollPane for the list. It works, but I cannot change the width of the user-list (Because BorderLayout.WEST and BorderLayout.EAST ignore the width)

  • Use a BorderLayout with a JScrollPane for the user list and show/hide the JScrollPane -> Does not work, don't ask me why...anyway, this way is not a nice solution

  • Use a JSplitPane, set the resize weight to 0.9. When the user-list should disappear, I minimize the right component (aka the user list) -> How ?

My code at the moment:

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

import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;


public class SplitPaneTest extends JFrame implements ActionListener
{
    private JSplitPane splitPane;
    private JTextArea textDisplay;
    private JList<String> listUser;
    private JScrollPane scrollTextDisplay;
    private JScrollPane scrollListUser;
    private JCheckBox itemShowUser;

    public static void main(String[] args)
    {
        new SplitPaneTest();
    }

    public SplitPaneTest()
    {
        setTitle("Chat Client");
        setMinimumSize(new Dimension(800, 500));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        textDisplay = new JTextArea();
        listUser = new JList<>();

        DefaultListModel<String> modelUser = new DefaultListModel<>();
        listUser.setModel(modelUser);

        modelUser.addElement(new String("User 1"));
        modelUser.addElement(new String("User 2"));
        modelUser.addElement(new String("User 3"));

        scrollTextDisplay = new JScrollPane(textDisplay);
        scrollListUser = new JScrollPane(listUser);

        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setLeftComponent(scrollTextDisplay);
        splitPane.setRightComponent(scrollListUser);
        splitPane.setResizeWeight(0.9);
        setContentPane(splitPane);

        JMenuBar menubar = new JMenuBar();
        JMenu menuWindow = new JMenu("Window");

        itemShowUser = new JCheckBox("Show user list");
        itemShowUser.addActionListener(this);
        itemShowUser.setSelected(true);

        menuWindow.add(itemShowUser);
        menubar.add(menuWindow);
        setJMenuBar(menubar);

        setVisible(true);
    }

    public boolean isUserListEnabled()
    {
        return itemShowUser.isSelected();
    }

    public void setUserListEnabled(boolean status)
    {
        scrollListUser.setVisible(status);
    }

    @Override
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() == itemShowUser)
        {
            boolean status = isUserListEnabled();
            setUserListEnabled(status);
        }
    }
}

And the result is:

enter image description here

And with hidden JScrollPane scrollListUser:

enter image description here

Can anybody give me a tipp ? The user-list is still visible ( I thought the JSplitPane would repaint..) .I come from Qt (C++) and in Qt I could use a dock widget - but Swing does not have one and to use third libs....I don't know - maybe there is a solution.

Was it helpful?

Solution

Looks like the splitPane can't handle invisible components well - a way out is to add/remove the scrollPane as appropriate:

public void setUserListEnabled(boolean status)
{
    splitPane.setRightComponent(status ? scrollListUser : null);
    splitPane.revalidate();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top