Monday, 15 July 2013

Java Swing JSplitPane not showing as intended -



Java Swing JSplitPane not showing as intended -

i'm trying create programme thats based upon 5 jsplitpanes , internal components, don't seem along well. since when run application split panes concentrated on upper left corner

the code simple:

projectexplorer = new projectexplorer(); editorexplorer = new editorexplorer(); favdownload = new favdownload(); favdownloadexplorer = new favdownloadexplorer(); //define listeners addcomponentlistener(new resizelistener()); hortoppane = new jsplitpane(jsplitpane.horizontal_split); hortoppane.setleftcomponent(projectexplorer); hortoppane.setrightcomponent(editorexplorer); horbotpane = new jsplitpane(jsplitpane.horizontal_split); horbotpane.setleftcomponent(favdownload); horbotpane.setrightcomponent(favdownloadexplorer); verticalpane = new jsplitpane(jsplitpane.vertical_split); verticalpane.settopcomponent(hortoppane); verticalpane.setbottomcomponent(horbotpane); setcontentpane(verticalpane); new timer().schedule(new timertask() { @override public void run() { resizecomponents(); } }, 100);

the resizecomponents method is

public void resizecomponents(){ int width = getwidth(); int height = getheight(); hortoppane.setminimumsize(new dimension(width, (height/2)+(height/4))); hortoppane.setsize(hortoppane.getminimumsize()); horbotpane.setminimumsize(new dimension(width, (height/8))); horbotpane.setsize(horbotpane.getminimumsize()); projectexplorer.setminimumsize(new dimension(width/8, hortoppane.getheight())); projectexplorer.setsize(projectexplorer.getminimumsize()); editorexplorer.setminimumsize(new dimension(width/2, hortoppane.getheight())); editorexplorer.setsize(editorexplorer.getminimumsize()); favdownload.setminimumsize(new dimension(width/8, horbotpane.getheight())); favdownload.setsize(favdownload.getminimumsize()); favdownloadexplorer.setminimumsize(new dimension(width/2, horbotpane.getheight())); favdownloadexplorer.setsize(favdownloadexplorer.getminimumsize()); }

it started setsize, reason didn't resize them @ all, putted setminimumsize , didn't resize either, when move elements bit on programme go minimum size, missing something?

i've made few java gui programs, 1 doesn't want work properly.

what did wrong?

to start with, don't mess setminimum/maximum/preferredsize, have @ should avoid utilize of set(preferred|maximum|minimum)size methods in java swing? more details

next, don't update state of ui components thread other event dispatching thread, swing not thread safe. java.util.timer trigger event notification within it's own thread context.

next, consider overriding (at least), getpreferredsize custom components , homecoming default value, example...

import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jsplitpane; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class splitpanetest { public static void main(string[] args) { new splitpanetest(); } public splitpanetest() { eventqueue.invokelater(new runnable() { @override public void run() { seek { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } grab (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { private final projectexplorer projectexplorer; private final editorexplorer editorexplorer; private final favdownload favdownload; private final favdownloadexplorer favdownloadexplorer; private final jsplitpane hortoppane; private final jsplitpane horbotpane; private final jsplitpane verticalpane; public testpane() { projectexplorer = new projectexplorer(); editorexplorer = new editorexplorer(); favdownload = new favdownload(); favdownloadexplorer = new favdownloadexplorer(); hortoppane = new jsplitpane(jsplitpane.horizontal_split); hortoppane.setleftcomponent(projectexplorer); hortoppane.setrightcomponent(editorexplorer); horbotpane = new jsplitpane(jsplitpane.horizontal_split); horbotpane.setleftcomponent(favdownload); horbotpane.setrightcomponent(favdownloadexplorer); verticalpane = new jsplitpane(jsplitpane.vertical_split); verticalpane.settopcomponent(hortoppane); verticalpane.setbottomcomponent(horbotpane); setlayout(new borderlayout()); add(verticalpane); } } protected abstract class abstractpane extends jpanel { @override public dimension getpreferredsize() { homecoming new dimension(200, 200); } } public class projectexplorer extends abstractpane { public projectexplorer() { setbackground(color.red); } } public class editorexplorer extends abstractpane { public editorexplorer() { setbackground(color.blue); } } public class favdownload extends abstractpane { public favdownload() { setbackground(color.magenta); } } public class favdownloadexplorer extends abstractpane { public favdownloadexplorer() { setbackground(color.cyan); } } }

if have modify divider's location consider using jsplitspane#setdividerlocation(double) or jsplitspane#setdividerlocation(int)

take @ how utilize split panes more details

java swing user-interface

No comments:

Post a Comment