Frage

Im about to make a new application, and i stumbled over an very cool sidebar (button-panel) I will try to add to the application (Its the left-sidebar on Spotify). The thing im curious about, is the principble (Take a look at the picture attached).
In the top theres 3 buttons (I assume its either JList or JTable items).

In the middle we have a "YOUR MUSIC" header (JLabel maybe??)

We also have a New Playlist (JButton)

And then the Playlist list (JList or JTable?)

The interresting things i would like to know is:

Firstly, that the three JList (playlistList, appList and yourmusicList, i assume its JLists) share the same JScrollPane, and therefor the JList/JTables(Or whatever it is) have to change size (Height) depended on the amount of items it contains. (For Instance: The playlist list change Height dependen on if have 3 or 17 playlists.)

Secondly: I managed to add Buttons, Labels, JLists in the same JPanel, but i could not figure out how to give them a shared JScrollPane (The problem occured when i tried to give two JLists same ScrollPane).

I think, that i use wrong LayoutManager for the JPanel the items should be contained in, and i use the wrong kind of Component (JList, JTable). If theres a Swing Component called JSideBar or something, it would be great!

Well, thats it! Theres no code example, since it woudlnt really benefit in any way

Regards Oliver

enter image description here

War es hilfreich?

Lösung

[...] but i could not figure out how to give them a shared JScrollPane (The problem occured when i tried to give two JLists same ScrollPane).

You can add all your components to a JPanel and set this one as the JScrollPane view port. Take a look to How to Use Scroll Panes tutorial.


If theres a Swing Component called JSideBar or something, it would be great!

Not provided by default but it's extremely easy to make your own scrollable side-bar with a JPanel and using BoxLayout. Check out the tutorials: How to Use BoxLayout. For instance:

import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 * @author dic19
 */
public class JSideBar extends JComponent {

    private final JPanel content;

    public JSideBar(boolean scrollable) {
        super();

        content = new JPanel();
        content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));

        this.setLayout(new BorderLayout());

        if(scrollable) {
            this.add(new JScrollPane(content));
        } else {
            this.add(content);
        }

    }

    /**
     * Adds a component to this side bar.
     * 
     * @param comp The component.
     * @param alignment One of {@code Component.LEFT_ALIGNMENT, Component.CENTER_ALIGNMENT, Component.RIGHT_ALIGNMENT}
     * 
     * @see java.awt.Component#LEFT_ALIGNMENT
     * @see java.awt.Component#CENTER_ALIGNMENT
     * @see java.awt.Component#RIGHT_ALIGNMENT
     */
    public void addItem(JComponent comp, float alignment) {
        comp.setAlignmentX(alignment);
        content.add(comp);
        if(content.isShowing()) {
            revalidate();
            repaint();
        }
    }

    /**
     * Adds a vertical space to this side bar.
     * @param height Height of vertical space.
     */
    public void addVerticalSpace(int height) {
        content.add(Box.createVerticalStrut(height));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); 
        Graphics graphics = g.create();
        graphics.setColor(getBackground());
        graphics.fillRect(0, 0, getWidth(), getHeight());
        graphics.dispose();
    }
}

And here is an example of use based on your case:

JLabel appFinderLabel = new JLabel("AppFinder");        
JLabel musicxMatchLabel = new JLabel("musicXmatch");
JLabel tuneWikiLabel = new JLabel("TuneWiki");

JLabel yourMusicLabel = new JLabel("YOUR MUSIC");
JLabel songsLabel = new JLabel("Songs");
JLabel albumsLabel = new JLabel("Albums");
JLabel artistsLabel = new JLabel("Artists");
JLabel localFilesLabel = new JLabel("Local Files");

/* 
 * Set icons, background and stuff if you need to
 * A good option would be use undecorated JButtons instead of JLabels
 */

JSideBar sideBar = new JSideBar(true);
sideBar.addItem(appFinderLabel, Component.LEFT_ALIGNMENT);
sideBar.addItem(musicxMatchLabel, Component.LEFT_ALIGNMENT);
sideBar.addItem(tuneWikiLabel, Component.LEFT_ALIGNMENT);

sideBar.addVerticalSpace(20);

sideBar.addItem(yourMusicLabel, Component.LEFT_ALIGNMENT);
sideBar.addItem(songsLabel, Component.LEFT_ALIGNMENT);
sideBar.addItem(albumsLabel, Component.LEFT_ALIGNMENT);
sideBar.addItem(artistsLabel, Component.LEFT_ALIGNMENT);
sideBar.addItem(localFilesLabel, Component.LEFT_ALIGNMENT);

Screenshot

Please note the scrollbar:

enter image description here

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top