简体   繁体   English

JList / JTable作为按钮面板

[英]JList/JTable as button panel

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). 我即将创建一个新应用程序,我偶然发现了一个非常酷的侧栏(按钮面板),我将尝试添加到该应用程序中(它是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). 在顶部有3个按钮(我假设它是JList或JTable项)。

In the middle we have a "YOUR MUSIC" header (JLabel maybe??) 在中间,我们有一个“ YOUR MUSIC”标题(也许是JLabel ??)

We also have a New Playlist (JButton) 我们还有一个新的播放列表(JButton)

And then the Playlist list (JList or JTable?) 然后是播放列表列表(JList或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. 首先,三个JList(播放列表列表,appList和yourmusicList,我假设其JList)共享相同的JScrollPane,因此JList / JTables(或其他类型)必须根据其包含的项目数量来更改大小(高度) 。 (For Instance: The playlist list change Height dependen on if have 3 or 17 playlists.) (例如:播放列表列表更改高度取决于3或17个播放列表。)

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). 其次:我设法在同一JPanel中添加Buttons,Labels,JLists,但是我不知道如何给它们提供共享的JScrollPane(当我尝试给两个JLists提供相同的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). 我认为,我为应该包含项目的JPanel使用了错误的LayoutManager,并且我使用了错误的组件类型(JList,JTable)。 If theres a Swing Component called JSideBar or something, it would be great! 如果有一个称为JSideBar的Swing组件,那太好了!

Well, thats it! 好,就这样! Theres no code example, since it woudlnt really benefit in any way 没有代码示例,因为它将以任何方式带来真正的好处

Regards Oliver 问候奥利弗

在此处输入图片说明

[...] 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). [...]但我不知道如何给他们一个共享的JScrollPane(当我尝试给两个JLists相同的ScrollPane时出现了问题)。

You can add all your components to a JPanel and set this one as the JScrollPane view port. 您可以将所有组件添加到JPanel并将其设置为JScrollPane视图端口。 Take a look to How to Use Scroll Panes tutorial. 看看如何使用滚动窗格教程。


If theres a Swing Component called JSideBar or something, it would be great! 如果有一个称为JSideBar的Swing组件,那太好了!

Not provided by default but it's extremely easy to make your own scrollable side-bar with a JPanel and using BoxLayout . 默认情况下不提供,但是使用JPanel和BoxLayout制作自己的可滚动侧栏非常容易。 Check out the tutorials: How to Use BoxLayout . 查看教程: 如何使用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: 请注意滚动条:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM