java - Creating Vertical Scrollable Pane for JPanels -
i trying make jscrollpane contains jpanels have charts jfreechart api. generatechartpanel method return charts in jpanel can make bunch of them. far have in main:
public static void main (string[] args){ jframe main = new jframe(); main.setlayout(new borderlayout()); main.setpreferredsize(new dimension(500,500)); //creates specframe jscrollpane specscrollpane = new jscrollpane(); jpanel container = new jpanel(); container.setlayout(new gridlayout()); container.add(generatespecpanelfromsmiles("c")); container.add(generatespecpanelfromsmiles("cc")); container.add(generatespecpanelfromsmiles("ccc")); container.setvisible(true); specscrollpane.setvisible(true); specscrollpane.add(container); main.add(specscrollpane); refineryutilities.centerframeonscreen(main); main.setvisible(true);
when run this, nothing shows up. apparently can't pack, setcloseop, setlayout, or center these panes commented them out. missing?
edit: getting dimensions
specpanel.addcomponentlistener(new java.awt.event.componentadapter() { @override public void componentresized(componentevent event) { specpanel.setsize(math.min(specpanel.getpreferredsize().width, specpanel.getwidth()), math.min(specpanel.getpreferredsize().height, specpanel.getheight())); } });
thanks
jscrollpane designated nest 1 jcomponent
you have put jpanel
jscrollpane
,jpanel
can add 3 componentshave change default layout manager (flowlayout) parent
jpanel
(contains 3 components), 1 depends of components, how returns preferredsize, maybe start gridlayout(all childs has same size on screen)
edit
whatever generatespecpanelfromsmiles
it, must returns preferredsize
parentpanel
, e.g. (playing preferredsize
gridlayout
, boxlayout
- box accepting min, max , preferred size)
.
.
.
import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.gridlayout; import java.awt.layoutmanager; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.boxlayout; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; public class jpanelsinsidejscrollpane { private jframe frame = new jframe("jpanels inside jscrollpane"); private jscrollpane scrollpane = new jscrollpane(); private jpanel parentpanel, childone, childtwo, childthree; private jbutton button = new jbutton("change, switch layout manager boxlayout"); public jpanelsinsidejscrollpane() { parentpanel = new jpanel() { @override public dimension getpreferredsize() { return new dimension(new dimension(800, 600)); } @override public void paintcomponent(graphics g) { int margin = 10; dimension dim = getsize(); super.paintcomponent(g); g.setcolor(color.dark_gray); g.fillrect(margin, margin, dim.width - margin * 2, dim.height - margin * 2); } }; parentpanel.setlayout(new gridlayout(0, 1)); childone = new jpanel() { @override public dimension getminimumsize() { return new dimension(100, 100); } @override public dimension getpreferredsize() { return new dimension(200, 300); } @override public dimension getmaximumsize() { return new dimension(500, 300); } @override public void paintcomponent(graphics g) { int margin = 10; dimension dim = getsize(); super.paintcomponent(g); g.setcolor(color.green); g.fillrect(margin, margin, dim.width - margin * 2, dim.height - margin * 2); } }; childtwo = new jpanel() { @override public dimension getminimumsize() { return new dimension(100, 100); } @override public dimension getpreferredsize() { return new dimension(400, 300); } @override public dimension getmaximumsize() { return new dimension(600, 400); } @override public void paintcomponent(graphics g) { int margin = 10; dimension dim = getsize(); super.paintcomponent(g); g.setcolor(color.blue); g.fillrect(margin, margin, dim.width - margin * 2, dim.height - margin * 2); } }; childthree = new jpanel() { @override public dimension getminimumsize() { return new dimension(100, 100); } @override public dimension getpreferredsize() { return new dimension(400, 300); } @override public dimension getmaximumsize() { return new dimension(800, 600); } @override public void paintcomponent(graphics g) { int margin = 10; dimension dim = getsize(); super.paintcomponent(g); g.setcolor(color.orange); g.fillrect(margin, margin, dim.width - margin * 2, dim.height - margin * 2); } }; parentpanel.add(childone); parentpanel.add(childtwo); parentpanel.add(childthree); scrollpane.setviewportview(parentpanel); scrollpane.getverticalscrollbar().setunitincrement(30); scrollpane.gethorizontalscrollbar().setunitincrement(30); button.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { layoutmanager manager = parentpanel.getlayout(); if ((manager != null) && (manager instanceof boxlayout)) { parentpanel.setlayout(new gridlayout(0, 1)); button.settext("change, switch layout manager boxlayout"); } else if ((manager != null) && (manager instanceof gridlayout)) { parentpanel.setlayout(new boxlayout(parentpanel, boxlayout.page_axis)); button.settext("change, switch layout manager gridlayout"); } parentpanel.revalidate(); parentpanel.repaint(); } }); frame.add(scrollpane); frame.add(button, borderlayout.south); frame.setsize(400, 300); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.exit_on_close); } public static void main(string args[]) { javax.swing.swingutilities.invokelater(new runnable() { @override public void run() { new jpanelsinsidejscrollpane(); } }); } }
Comments
Post a Comment