Sunday, 15 May 2011

java - JScrollPanel don't show the scroll -



java - JScrollPanel don't show the scroll -

i have jframe dimension 500 x 500: contains jcomponent size 1000 x 1000 , jcomponent contained jscrollbar scrollbar present. screenshot

the code

/**component */ public class linescomponent extends jpanel .... /***other class: commandpanel contains linescomponents*/ public class commandpanel extends jpanel{ .... private linescomponent panel; /*jscrollpanel*/ private void buildscrollpanel(container container) { jscrollpane scroll = new jscrollpane(panel); scroll.setsize(1000,1000); scroll.setborder(new lineborder(color.black)); scroll.sethorizontalscrollbarpolicy(scrollpaneconstants.horizontal_scrollbar_always); scroll.setverticalscrollbarpolicy(scrollpaneconstants.vertical_scrollbar_always); container.add(scroll); } /**jframe*/ private void buildframe(string title) { this.testframe = new jframe(title); this.testframe.setlayout(new flowlayout()); this.testframe.setdefaultcloseoperation(jframe.exit_on_close); this.testframe.setlocationrelativeto(null); this.testframe.setsize(500,500); this.testframe.setbackground(enumcolor.background_panel.getvalue()); } //end commandpanel

as showed screenshot, window has scrollbar isn't working.

i tried alter dimensions of jpanel , jframe situation doesn't change.

i know scroll appears when dimension of jcomponent greater of dimensione of jpanel container, in moment see lose info don't understand is.

do have suggest, please?

ps screenshot show scroll doesn't work, in desiderata scroll has work in vertical , horizontal senses

ps ps code in 1 class

package testdebug; import java.awt.color; import java.awt.container; import java.awt.dimension; import java.awt.flowlayout; import java.awt.graphics; import java.awt.gridlayout; import java.awt.layoutmanager; import java.awt.event.actionlistener; import java.util.arraylist; import java.util.iterator; import java.util.linkedlist; import java.util.list; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtextfield; import javax.swing.scrollpaneconstants; import javax.swing.border.lineborder; public class allclasses { public static void main(string[] a){ commandpanel commandpanel=new commandpanel("test", new arraylist<mypoint>()); } } class linescomponent extends jpanel{ private static final long serialversionuid = 1l; private final linkedlist<line> lines = new linkedlist<line>(); private static class line { final int x1;//x del primo punto final int y1;//y del primo punto final int x2;//x del secondo punto final int y2;//y del secondo punto final color color; final int pressure; public line(int x1, int y1, int x2, int y2, color color) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.color = color; this.pressure=3; } public line(int x1, int y1, int x2, int y2, int newpressure, color color) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.pressure=newpressure; this.color = color; } }//line public linescomponent(){ setborder(new lineborder(color.black)); setbackground(color.white); setpreferredsize(new dimension(400, 400)); } public void clearlines() { this.lines.clear(); repaint(); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); (line line : this.lines) { g.setcolor(line.color); g.filloval(line.x1,line.y1, line.pressure, line.pressure); } } public void addpoint(mypoint p, color randomcolor) { //call addline(double x, double y, double xtilt, double ytilt, color randomcolor); } private void addline(double x, double y, double xtilt, double ytilt, color randomcolor) { this.lines.add(new line((int) x, (int) y, (int) xtilt, (int) ytilt, randomcolor)); repaint(); } } enum enumbutton { decrease_x("-"), increase_x("+"), decrease_y("-"), increase_y("+"), decrease_zoom("-"), increase_zoom("+"); private jbutton button; public jbutton button(){ homecoming this.button; } private enumbutton(string s){ this.button=new jbutton(s); } public void addactionlistener(actionlistener a){ this.button().addactionlistener(a); } } class commandpanel extends jpanel { private static final long serialversionuid = 1l; private static final string initial_position="0"; private jframe testframe = null; private linescomponent panelsignature; private jtextfield positionx=new jtextfield(5); private jtextfield positiony=new jtextfield(5); public commandpanel(string title, list<mypoint> newpoints) { super(); positionx.settext(initial_position); positiony.settext(initial_position); buildframe(title); container container = testframe.getcontentpane(); this.panelsignature=initpanel(); buildscrollpanel(container); paintline(newpoints); allignbuttons(); defineframe(); } private void buildscrollpanel(container container) { jscrollpane scroll = new jscrollpane(panelsignature); scroll.setsize(300,300); scroll.setborder(new lineborder(color.black)); scroll.sethorizontalscrollbarpolicy(scrollpaneconstants.horizontal_scrollbar_always); scroll.setverticalscrollbarpolicy(scrollpaneconstants.vertical_scrollbar_always); container.add(scroll); } private void defineframe() { this.testframe.pack(); this.testframe.setvisible(true); } private void paintline(list<mypoint> points) { iterator<mypoint> iterator = points.iterator(); while (iterator.hasnext()) { mypoint point = iterator.next(); this.panelsignature.addpoint(point,color.blue); } }//disegnalinea private void allignbuttons() { jpanel buttonpanel = new jpanel(new flowlayout()); sectionhorizontalmovement(buttonpanel); sectionverticalmovement(buttonpanel); sectionzoom(buttonpanel); this.testframe.add(buttonpanel); } private void sectionzoom(jpanel buttonpanel) { buttonpanel.add(enumlabel.zoom.label()); buttonpanel.add(enumbutton.increase_zoom.button()); buttonpanel.add(enumbutton.decrease_zoom.button()); buttonpanel.add(enumlabel.zoom_direction.label()); buttonpanel.add(enumlabel.empty.label()); }//sezionezoom private void sectionverticalmovement(jpanel pannellopulsanti) { pannellopulsanti.add(enumlabel.move_up_down.label()); pannellopulsanti.add(enumbutton.increase_y.button()); pannellopulsanti.add(enumbutton.decrease_y.button()); pannellopulsanti.add(enumlabel.y_direction.label()); pannellopulsanti.add(enumlabel.max_y_allowed.label()); }//sezionespostamentoverticale private void sectionhorizontalmovement(jpanel pannellopulsanti) { pannellopulsanti.add(enumlabel.move_right_left.label()); pannellopulsanti.add(enumbutton.increase_x.button()); pannellopulsanti.add(enumbutton.decrease_x.button()); pannellopulsanti.add(enumlabel.x_direction.label()); pannellopulsanti.add(enumlabel.max_x_allowed.label()); }//sezionespostamentoorizzontale public linescomponent initpanel() { linescomponent linescomponent= new linescomponent(); linescomponent.setborder(new lineborder(color.gray)); homecoming linescomponent; } private void buildframe(string titolo) { this.testframe = new jframe(titolo); this.testframe.setlayout(new flowlayout()); this.testframe.setdefaultcloseoperation(jframe.exit_on_close); this.testframe.setlocationrelativeto(null); this.testframe.setsize(700,700); this.testframe.setbackground(color.white); } } enum enumlayout { frame(new flowlayout()), button_panel(new gridlayout(3, 5)); private layoutmanager value; private enumlayout(layoutmanager manager){ this.value=manager; } public layoutmanager layout() { homecoming value; } } enum enumlabel { move_right_left("sposta in orizzontale"), move_up_down("sposta in verticale"), zoom("zoom"), empty(""), max_x_allowed("larghezza: 600"), max_y_allowed("altezza: 600"), x_direction("0"), y_direction("0"), zoom_direction("1") ; private jlabel label; private enumlabel(string lab){ this.label=new jlabel(lab); } public jlabel label (){ homecoming this.label; } } class mypoint { // class data: useless position of panel }

scrollpane , scrollbars exclusively rely on size of component in viewport, which, default, depends of preferred size of component.

check code shows basic illustration of how works:

import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.swingutilities; import javax.swing.timer; public class testscrollpane { public static class customcomponent extends jpanel { private static final int radius = 20; private int x = 0; private int y = 0; private double speed = 18; private double dx; private double dy; public customcomponent() { dx = speed; dy = speed; timer t = new timer(20, new actionlistener() { @override public void actionperformed(actionevent e) { x += dx; y += dy; if (x + radius > getwidth()) { x = getwidth() - radius; dx = -speed; } else if (x < 0) { x = 0; dx = speed; } if (y + radius > getheight()) { y = getheight() - radius; dy = -speed; } else if (y < 0) { y = 0; dy = speed; } repaint(); } }); t.start(); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(color.red); g.filloval(x, y, radius, radius); } @override public dimension getpreferredsize() { homecoming new dimension(600, 600); } } protected void initui() { jframe window = new jframe(testscrollpane.class.getsimplename()); window.setdefaultcloseoperation(jframe.exit_on_close); jscrollpane scroll = new jscrollpane(new customcomponent(), jscrollpane.vertical_scrollbar_always, jscrollpane.horizontal_scrollbar_as_needed); window.add(scroll); window.setsize(600, 500); window.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { new testscrollpane().initui(); } }); } }

java swing jscrollpane jscrollbar

No comments:

Post a Comment