java - Allow nested JSplitPanes to control parent JSplitPanes -
below code simple layout created using several nested jsplitpanes
.
import java.awt.color; import java.awt.dimension; import java.awt.gridlayout; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jsplitpane; public class cdburner extends jframe { private static final long serialversionuid = -6027473114929970648l; jsplitpane main, folder, rest; jpanel centeral, folders, favourites, tasks; jlabel label; private cdburner() { super("dan's cd burner"); setdefaultcloseoperation(jframe.exit_on_close); setlayout(new gridlayout(1, 1)); getcontentpane().setbackground(color.black); createlayout(); pack(); setminimumsize(getsize()); setextendedstate(getextendedstate() | jframe.maximized_both); setvisible(true); requestfocus(); } private void createlayout() { createpanels(); rest = new jsplitpane(jsplitpane.horizontal_split, centeral, tasks); rest.setresizeweight(1); rest.setcontinuouslayout(true); rest.setonetouchexpandable(true); folder = new jsplitpane(jsplitpane.vertical_split, favourites, folders); folder.setresizeweight(0.35); folder.setcontinuouslayout(true); folder.setonetouchexpandable(true); main = new jsplitpane(jsplitpane.horizontal_split, folder, rest); main.setresizeweight(0); main.setcontinuouslayout(true); main.setonetouchexpandable(true); getcontentpane().add(main); } private void createpanels() { createfolders(); createcenter(); createtaskspool(); createfavourites(); } private void createfolders() { folders = new jpanel(new gridlayout(1, 1)); label = new jlabel("folder"); folders.setminimumsize(new dimension(300, 100)); folders.add(label); } private void createcenter() { centeral = new jpanel(new gridlayout(1, 1)); label = new jlabel("central"); centeral.add(label); centeral.setminimumsize(new dimension(300, 100)); } private void createtaskspool() { tasks = new jpanel(new gridlayout(1, 1)); label = new jlabel("task"); tasks.setminimumsize(new dimension(300, 100)); tasks.add(label); } private void createfavourites() { favourites = new jpanel(new gridlayout(1, 1)); label = new jlabel("fav"); favourites.setminimumsize(new dimension(300, 100)); favourites.add(label); } public static void main(string[] args) { new cdburner(); } }
due line rest.setresizeweight(1);
can drag main (jsplitpane)
divider right , shrink tasks jpanel
until both jpanels
in rest
minimum size. however, if try opposite nothing happens. see images below see problem.
if gui looks this, can drag main
divider.
and result of this.
however if looks , try drag rest
divider nothing happens.
this because both sides of rest jsplitpane
@ minimum size.
the question
how can make when drag rest
divider effects main jsplitpane
both operations shown in images above possible?
as far know behaviour describe normal behaviour divided views. have on how dividers of professional applications eclipse behave.
anyway created workaround desired behaviour controlling movements of rest jsplitpane
divider programmatically. need add these lines of code after creating rest jsplitpane
in createlayout()
method.
splitpaneui spui = rest.getui(); if (spui instanceof basicsplitpaneui) { ((basicsplitpaneui) spui).getdivider().addmousemotionlistener(new mouseadapter(){ @override public void mousedragged(mouseevent e){ int newpos = e.getpoint().x; if(newpos < 0 && folder.getsize().width > 300 && centeral.getsize().width <= 300){ main.setdividerlocation(main.getdividerlocation() + newpos); validate(); } if(centeral.getsize().width > 300 && tasks.getsize().width > 300){ rest.setdividerlocation(rest.getdividerlocation() + newpos); validate(); } } }); }
explanation:
in first lines take ui object of divider of jsplitpane
, add custom mousemotionlistener
it. need ui object because real jsplitpane
doesn't mouse events directly.
inside listener override mousedragged
method , receive actual x-position of mouse while dragging divider.
if negative value dragging divider left. in case want main divider move if center panel has shrunk minium size of 300 , left folder panel still bigger minimum size.
rest divider should movable if central , task panel on right still bigger minimum size of 300.
Comments
Post a Comment