java - How to access all components beneath top JFrame -
partly learning purposes, made jframe
4 jpanel
s, 2 jlabel
s, , jmenubar
. put various components (buttons, textboxes, textarea) 4 jpanels. disabled every 1 of components throughout.
i wrote 2 methods try enable everything. worked partially. here's code:
public void enableeverything(){ component [] p = this.getcontentpane().getcomponents(); for(component pp : p) if(pp instanceof jpanel) enableall((jpanel) pp); } public void enableall(jpanel p){ component [] c = p.getcomponents(); for(component cc: c) cc.setenabled(true); jtextarea1.setenabled(true); jscrollpane1.setenabled(true); }
the jtextarea
(inside jscrollpane
) didn't enabled last 2 lines above. why?
i tried:
component [] s = jscrollpane1.getcomponents(); for(component ss: s) ss.enableinputmethods(true);
how enable textarea?
and jmenubar
didn't enabled either. don't know find it. read it's in jlayeredpane, ... tried didn't work:
for(int = 0; < 2; i++){ system.out.println(i); this.getlayeredpane().getcomponent(i).setenabled(true); }
in pane find jmenubar
, how enable jmenu
s? (and jmenuitem
s.)
of course, worked:
menfileandedit.setenabled(true); mnufile.setenabled(true); mnuedit.setenabled(true); mnifilesave.setenabled(true); mnieditundo.setenabled(true); mnieditredo.setenabled(true);
keep in mind i'm experimenting, trying learn , how access programmatically drilling down top jframe
using getcomponents()
.
1st edit
here's how @ menu bar!
component[] m = this.getjmenubar().getcomponents(); for(component mm: m) mm.setenabled(true);
2nd edit
see below recursive partial solution.
this "recursive response" @maaartinus (though second read note stack). it's not answer problem, it's progress.
public void enableeverything(container c){ component [] p = c.getcomponents(); system.out.println("component count " + c.getcomponentcount() + " " + c.tostring().substring(0,40) ); for(component pp : p){ pp.setenabled(true); if(pp instanceof container){ system.out.println("recursive call " + pp.tostring().substring(0,40)); enableeverything((container) pp); } else system.out.println("no recursive call"); }
i had call twice enabled:
gameboard.enableeverything(gameboard.getcontentpane()); gameboard.enableeverything(gameboard.getjmenubar());
it did away 1 method since it's recursive, , produced same results in did not enable jmenuitem
s or jtextarea
.
so i'm still looking how that.
it produced interesting output in every component seems instance of container, doesn't seem right:
gameboard.enableeverything(gameboard.getcontentpane()) component count 6 javax.swing.jpanel[null.contentpane,0,23 recursive call javax.swing.jpanel[pnlgamegrid,12,139,59 component count 1 javax.swing.jpanel[pnlgamegrid,12,139,59 recursive call javax.swing.jtextfield[jtextfield1,233,1 component count 0 javax.swing.jtextfield[jtextfield1,233,1 recursive call javax.swing.jpanel[pnlavailableletters,1 component count 1 javax.swing.jpanel[pnlavailableletters,1 recursive call javax.swing.jtogglebutton[jtogglebutton1 component count 0 javax.swing.jtogglebutton[jtogglebutton1 recursive call javax.swing.jlabel[lblavailableletters,1 component count 0 javax.swing.jlabel[lblavailableletters,1 recursive call javax.swing.jpanel[pnlscore,476,25,107x9 component count 2 javax.swing.jpanel[pnlscore,476,25,107x9 recursive call javax.swing.jtextfield[txtscore,21,14,66 component count 0 javax.swing.jtextfield[txtscore,21,14,66 recursive call javax.swing.jbutton[btnscore,21,61,66x24 component count 0 javax.swing.jbutton[btnscore,21,61,66x24 recursive call javax.swing.jpanel[pnlplays,624,51,271x5 component count 3 javax.swing.jpanel[pnlplays,624,51,271x5 recursive call javax.swing.jscrollpane[jscrollpane1,13, component count 3 javax.swing.jscrollpane[jscrollpane1,13, recursive call javax.swing.jviewport[,1,1,220x80,layout component count 1 javax.swing.jviewport[,1,1,220x80,layout recursive call javax.swing.jtextarea[jtextarea1,0,0,220 component count 0 javax.swing.jtextarea[jtextarea1,0,0,220 recursive call javax.swing.jscrollpane$scrollbar[,0,0,0 component count 2 javax.swing.jscrollpane$scrollbar[,0,0,0 recursive call javax.swing.plaf.metal.metalscrollbutton component count 0 javax.swing.plaf.metal.metalscrollbutton recursive call javax.swing.plaf.metal.metalscrollbutton component count 0 javax.swing.plaf.metal.metalscrollbutton recursive call javax.swing.jscrollpane$scrollbar[,0,0,0 component count 2 javax.swing.jscrollpane$scrollbar[,0,0,0 recursive call javax.swing.plaf.metal.metalscrollbutton component count 0 javax.swing.plaf.metal.metalscrollbutton recursive call javax.swing.plaf.metal.metalscrollbutton component count 0 javax.swing.plaf.metal.metalscrollbutton recursive call javax.swing.jbutton[jbutton1,61,262,81x2 component count 0 javax.swing.jbutton[jbutton1,61,262,81x2 recursive call javax.swing.jcheckbox[jcheckbox1,49,207, component count 0 javax.swing.jcheckbox[jcheckbox1,49,207, recursive call javax.swing.jlabel[lblplays,624,29,100x1 component count 0 javax.swing.jlabel[lblplays,624,29,100x1 gameboard.enableeverything(gameboard.getjmenubar()) component count 2 javax.swing.jmenubar[menfileandedit,0,0, recursive call javax.swing.jmenu[mnufile,0,0,31x21,alig component count 0 javax.swing.jmenu[mnufile,0,0,31x21,alig recursive call javax.swing.jmenu[mnuedit,31,0,33x21,ali component count 0 javax.swing.jmenu[mnuedit,31,0,33x21,ali
i hoping recursion @ jmenuitem
s, no such luck. any thoughts on how so?
i guess need recurse whole tree, like
- start root
- iterate components
- enable each of them
- test if it's instance of container
- if so, recursive call
for scrollpane
you'll need additional instanceof
test , getviewport
or this.
Comments
Post a Comment