java - JList/JTable as button panel -
im make new application, , stumbled on cool sidebar (button-panel) try add application (its left-sidebar on spotify). thing im curious about, principble (take @ picture attached).
in top theres 3 buttons (i assume either jlist or jtable items).
in middle have "your music" header (jlabel maybe??)
have new playlist (jbutton)
, playlist list (jlist or jtable?)
interresting things know is:
firstly, 3 jlist (playlistlist, applist , yourmusiclist, assume jlists) share same jscrollpane, , therefor jlist/jtables(or whatever is) have change size (height) depended on amount of items contains. (for instance: playlist list change height dependen on if have 3 or 17 playlists.)
secondly: managed add buttons, labels, jlists in same jpanel, not figure out how give them shared jscrollpane (the problem occured when tried give 2 jlists same scrollpane).
think, use wrong layoutmanager jpanel items should contained in, , use wrong kind of component (jlist, jtable). if theres swing component called jsidebar or something, great!
well, thats it! theres no code example, since woudlnt benefit in way
regards oliver

[...] not figure out how give them shared jscrollpane (the problem occured when tried give 2 jlists same scrollpane).
you can add components jpanel , set 1 jscrollpane view port. take how use scroll panes tutorial.
if theres swing component called jsidebar or something, great!
not provided default it's extremely easy make own scrollable side-bar jpanel , using boxlayout. check out tutorials: how use boxlayout. 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 component side bar. * * @param comp component. * @param alignment 1 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 vertical space 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 example of use based on 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 , stuff if need * option 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 scrollbar:

Comments
Post a Comment