[...] 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: