java - Adding scrollbar to FlowLayout -


i adding scrollbar jpanel flowlayout impossible, don't see scrollbar.

i've tried thousand different ways have not accomplished anything. new java , have yet many failures please kind.

here code:

//creamos el panel que contendra los botones de cada producto diferente package com.foolsrecords.tpv.tablaproductos.vista;  //hacemos todas las importaciones necesarias import com.foolsrecords.tpv.modelo.producto; import com.foolsrecords.tpv.modelo.eventos.controladoreventlistener; import com.foolsrecords.tpv.modelo.eventos.productoseleccionadolistener; import com.foolsrecords.tpv.servicios.servicioitemventa; import com.foolsrecords.tpv.servicios.servicioproducto; import com.foolsrecords.tpv.vista.componentes.jproductobutton; import java.awt.color; import java.awt.image; import java.util.hashmap; import java.util.list; import java.util.map; import javax.swing.swingconstants; import javax.swing.uimanager;  //creamos la clase principal public class tablaproductos extends javax.swing.jpanel {  //creamos las variables y objetos necesarios para poder trabajar private map<integer, jproductobutton> mapaproductos; productoseleccionadolistener objeven = new productoseleccionadolistener(this); servicioitemventa servitemventa = new servicioitemventa();  //creamos el constructor public tablaproductos() {     initcomponents();     inicializarpanelproductos(0); //con los subproductos de tipo 1 }  //inicializamos el panel de productos y le pasamos la familia de los productos como       parametro para mostrar los botones correctos public void inicializarpanelproductos(int tipoproducto) {      //creamos un objeto para conectar con la base de datos traves de la clase servicioproducto     servicioproducto servicio = new servicioproducto();      //conectamos con la base de datos y cojemos los productos de la familia enviada como parametro     list<producto> productos = servicio.getproductos(tipoproducto);      //creamos un mapaproductos para poder crear los botones dinamicamente     mapaproductos = new hashmap();      //borramos el panel jpanelproductos para montarlo de nuevo como nosotros queremos     jpanelproductos.removeall();      //creamos los 15 botones que contendra el panel y los hacemos todos invisibles     (int = 1; <= 30; i++) {         mapaproductos.put(i, new jproductobutton());         mapaproductos.get(i).addactionlistener(objeven);         mapaproductos.get(i).setvisible(false);         jpanelproductos.add(mapaproductos.get(i), - 1);     }      //hacemos visibles tantos botones como productos tenemos en la familia enviada como parametro     (int = 0; < productos.size(); i++) {          jproductobutton boton = mapaproductos.get(i + 1);         uimanager.put("button.select", color.white);         boton.setvisible(true);         boton.setproducto(productos.get(i));         boton.sethorizontaltextposition(swingconstants.center);         boton.setverticaltextposition(swingconstants.bottom);         boton.setfocuspainted(false);         boton.setbackground(color.white);         boton.setfont(new java.awt.font("arial", 1, 9));;         boton.settext(productos.get(i).getdescripcion());         boton.seticon(new javax.swing.imageicon(productos.get(i).getimagen().getscaledinstance(88, 70, image.scale_smooth)));         boton.setborder(javax.swing.borderfactory.createlineborder(new java.awt.color(0, 0, 204), 2));         boton.setpreferredsize(new java.awt.dimension(79, 97));         boton.setminimumsize(new java.awt.dimension(79, 97));         boton.setmaximumsize(new java.awt.dimension(79, 97));     }      jpanelproductos.repaint(); }  //este metodo es generado automaticamente por netbeans @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code">                           private void initcomponents() {      jpanel1 = new javax.swing.jpanel();     jscrollpane2 = new javax.swing.jscrollpane();     jpanelproductos = new javax.swing.jpanel();      setminimumsize(new java.awt.dimension(410, 300));     setname(""); // noi18n     setpreferredsize(new java.awt.dimension(410, 300));     setlayout(null);      jpanel1.setpreferredsize(new java.awt.dimension(410, 300));     jpanel1.setlayout(null);      jpanelproductos.setpreferredsize(new java.awt.dimension(400, 800));     jpanelproductos.setlayout(new java.awt.flowlayout(java.awt.flowlayout.left, 2, 2));     jscrollpane2.setviewportview(jpanelproductos);      jpanel1.add(jscrollpane2);     jscrollpane2.setbounds(0, 0, 410, 300);      add(jpanel1);     jpanel1.setbounds(0, 0, 410, 300); }// </editor-fold>                           // variables declaration - not modify                      private javax.swing.jpanel jpanel1; private javax.swing.jscrollpane jscrollpane2; private javax.swing.jpanel jpanelproductos; // end of variables declaration                     //creamos los y los set necesarios public javax.swing.jpanel getjpanelproductos() {     return jpanelproductos; }  public void setjpanelproductos(javax.swing.jpanel jpanelproductos) {     this.jpanelproductos = jpanelproductos; }  public void setcontrolador(controladoreventlistener controlador) {     this.objeven.setcontrolador(controlador); } } 

this...

jpanelproductos.setpreferredsize(new java.awt.dimension(400, 800)); 

is problem. jscrollpane use preferred size of component determine if needs use scroll bars display or not. should let layout manager make determination.

if want affect size of viewport (how big scrollpane appears default), should using scrollable interface

hava @ should avoid use of set(preferred|maximum|minimum)size methods in java swing? more details.

avoid using null layouts, pixel perfect layouts illusion within modern ui design. there many factors affect individual size of components, none of can control. swing designed work layout managers @ core, discarding these lead no end of issues , problems spend more , more time trying rectify


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -