elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Introducción a Git (Primera Parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Agregar Scrollbar a JPanel
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] 2 Ir Abajo Respuesta Imprimir
Autor Tema: Agregar Scrollbar a JPanel  (Leído 6,499 veces)
GSecurity

Desconectado Desconectado

Mensajes: 9


Ver Perfil
Agregar Scrollbar a JPanel
« en: 26 Enero 2020, 01:53 am »

Hola a todos,

Este problema es muy posteado en distintos foros de programacion, sin embargo ninguno ha logrado ayudarme o quiza no he logrado entender  >:(.

Tengo un JPanel con layout Flow en donde agrego controles de manera dinamica, simplemente necesito agregar un scrollbar VERTICAL.

Codigo:
        JPanel pnlcenter = new JPanel();
        for (int i = 0; i < 50; i++) {
            JButton btnAgrega = new JButton("Hola");
            pnlcenter.add(btnAgrega);
        }
        pnlcenter.setLayout(new FlowLayout());
        pnlcenter.setBorder(BorderFactory.createLineBorder(Color.black));
        pnlcenter.setBounds(0, 0, 200, 200);
        pnlcenter.setPreferredSize(new Dimension(2000, 2000));
        pnlcenter.setVisible(true);
        JScrollPane scroller = new JScrollPane(pnlcenter);
        scroller.setViewportView(pnlcenter);
        this.add(pnlcenter);

Esto es tan basico que aun asi no logro resolverlo  :-( Help me please !!


En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: Agregar Scrollbar a JPanel
« Respuesta #1 en: 27 Enero 2020, 00:46 am »

Hola a todos,

Este problema es muy posteado en distintos foros de programacion, sin embargo ninguno ha logrado ayudarme o quiza no he logrado entender  >:(.

Tengo un JPanel con layout Flow en donde agrego controles de manera dinamica, simplemente necesito agregar un scrollbar VERTICAL.

Codigo:
        JPanel pnlcenter = new JPanel();
        for (int i = 0; i < 50; i++) {
            JButton btnAgrega = new JButton("Hola");
            pnlcenter.add(btnAgrega);
        }
        pnlcenter.setLayout(new FlowLayout());
        pnlcenter.setBorder(BorderFactory.createLineBorder(Color.black));
        pnlcenter.setBounds(0, 0, 200, 200);
        pnlcenter.setPreferredSize(new Dimension(2000, 2000));
        pnlcenter.setVisible(true);
        JScrollPane scroller = new JScrollPane(pnlcenter);
        scroller.setViewportView(pnlcenter);
        this.add(pnlcenter);

Esto es tan basico que aun asi no logro resolverlo  :-( Help me please !!

 >:D >:D  Mmm eso de que en ningun lugar leiste una solucion, suena a que no buscaste nada, siempre la hay LMAOO

Código
  1. /*
  2.  
  3.  */
  4. public class ScrollPanel extends JFrame {
  5.  
  6.    public ScrollPanel() {
  7.        JPanel pnlcenter = new JPanel();
  8.        for (int i = 0; i < 50; i++) {
  9.            JButton btnAgrega = new JButton("Hola");
  10.            pnlcenter.add(btnAgrega);
  11.        }
  12.        pnlcenter.setLayout(new FlowLayout()); // por defecto usa FlowLayout
  13.        pnlcenter.setBorder(BorderFactory.createLineBorder(Color.black));
  14.        pnlcenter.setBounds(0, 0, 200, 200);
  15.        pnlcenter.setPreferredSize(new Dimension(600, 600));
  16.        pnlcenter.setVisible(true);
  17.  
  18.        final JScrollPane scroller = new JScrollPane(pnlcenter,  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  19.        scroller.setViewportView(pnlcenter);
  20.  
  21.        this.setSize(new Dimension(400,400));
  22.        this.add(scroller);
  23.        this.pack();
  24.  
  25.        this.setLocationRelativeTo(null);
  26.  
  27.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.        this.setVisible(true);
  29.    }
  30.  
  31.    public static void main(String ... blablla) {
  32.        new Thread(ScrollPanel::new).start();
  33.    }
  34.  
  35. }
  36.  

Puedes tambien de la siguiente manera con los métodos suyos

Código
  1. final JScrollPane scroller = new JScrollPane(pnlcenter);
  2. scroller.setViewportView(pnlcenter);
  3. scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  4. scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

tambien que en la linea 22 tu no añadias el JScrollPane al JFrame me imagino.


« Última modificación: 27 Enero 2020, 00:50 am por rub'n » En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
GSecurity

Desconectado Desconectado

Mensajes: 9


Ver Perfil
Re: Agregar Scrollbar a JPanel
« Respuesta #2 en: 1 Febrero 2020, 03:36 am »

>:D >:D  Mmm eso de que en ningun lugar leiste una solucion, suena a que no buscaste nada, siempre la hay LMAOO

Código
  1. /*
  2.  
  3.  */
  4. public class ScrollPanel extends JFrame {
  5.  
  6.    public ScrollPanel() {
  7.        JPanel pnlcenter = new JPanel();
  8.        for (int i = 0; i < 50; i++) {
  9.            JButton btnAgrega = new JButton("Hola");
  10.            pnlcenter.add(btnAgrega);
  11.        }
  12.        pnlcenter.setLayout(new FlowLayout()); // por defecto usa FlowLayout
  13.        pnlcenter.setBorder(BorderFactory.createLineBorder(Color.black));
  14.        pnlcenter.setBounds(0, 0, 200, 200);
  15.        pnlcenter.setPreferredSize(new Dimension(600, 600));
  16.        pnlcenter.setVisible(true);
  17.  
  18.        final JScrollPane scroller = new JScrollPane(pnlcenter,  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  19.        scroller.setViewportView(pnlcenter);
  20.  
  21.        this.setSize(new Dimension(400,400));
  22.        this.add(scroller);
  23.        this.pack();
  24.  
  25.        this.setLocationRelativeTo(null);
  26.  
  27.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.        this.setVisible(true);
  29.    }
  30.  
  31.    public static void main(String ... blablla) {
  32.        new Thread(ScrollPanel::new).start();
  33.    }
  34.  
  35. }
  36.  

Puedes tambien de la siguiente manera con los métodos suyos

Código
  1. final JScrollPane scroller = new JScrollPane(pnlcenter);
  2. scroller.setViewportView(pnlcenter);
  3. scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  4. scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

tambien que en la linea 22 tu no añadias el JScrollPane al JFrame me imagino.

Simplemente agregue toda estas lineas de codigo y ahora al ejecutar el frame se ve completamente vacio.

 final JScrollPane scroller = new JScrollPane(pnlcenter,  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroller.setViewportView(pnlcenter);
 
        this.setSize(new Dimension(400,400));
        this.add(scroller);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

Que puede estar pasando? hay algo que no debo estar entendiendo  :( :(
En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: Agregar Scrollbar a JPanel
« Respuesta #3 en: 1 Febrero 2020, 10:00 am »

Simplemente agregue toda estas lineas de codigo y ahora al ejecutar el frame se ve completamente vacio.

 final JScrollPane scroller = new JScrollPane(pnlcenter,  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroller.setViewportView(pnlcenter);
 
        this.setSize(new Dimension(400,400));
        this.add(scroller);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

Que puede estar pasando? hay algo que no debo estar entendiendo  :( :(


Pasa el codigo. que a en mi maquina funciona LMFAOO

Y usa GeSHI para añadir el codigo java.

« Última modificación: 1 Febrero 2020, 10:04 am por rub'n » En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
GSecurity

Desconectado Desconectado

Mensajes: 9


Ver Perfil
Re: Agregar Scrollbar a JPanel
« Respuesta #4 en: 2 Febrero 2020, 18:00 pm »

Pasa el codigo. que a en mi maquina funciona LMFAOO

Y usa GeSHI para añadir el codigo java.


Aqui te dejo mi codigo, el formulario lo he creado desde la paleta de controles de netbeans.


Código
  1. public frmTest() {
  2.        initComponents();  
  3.        JPanel pnlcenter = new JPanel();
  4.        for (int i = 0; i < 50; i++) {
  5.            JButton btnAgrega = new JButton("Hola");
  6.            pnlcenter.add(btnAgrega);
  7.        }
  8.        pnlcenter.setLayout(new FlowLayout());
  9.        pnlcenter.setBorder(BorderFactory.createLineBorder(Color.black));
  10.        pnlcenter.setBounds(0, 0, 200, 200);
  11.        pnlcenter.setPreferredSize(new Dimension(2000, 2000));
  12.        pnlcenter.setVisible(true);
  13.  
  14.        final JScrollPane scroller = new JScrollPane(pnlcenter, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  15.        scroller.setViewportView(pnlcenter);
  16.  
  17.        this.setSize(new Dimension(400, 400));
  18.        this.add(scroller);
  19.        this.pack();
  20.        this.setLocationRelativeTo(null);
  21.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.        this.setVisible(true);
  23. }

Muchas gracias !!!!
En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: Agregar Scrollbar a JPanel
« Respuesta #5 en: 2 Febrero 2020, 22:04 pm »

Aqui te dejo mi codigo, el formulario lo he creado desde la paleta de controles de netbeans.


Código
  1. public frmTest() {
  2.        initComponents();  
  3.        JPanel pnlcenter = new JPanel();
  4.        for (int i = 0; i < 50; i++) {
  5.            JButton btnAgrega = new JButton("Hola");
  6.            pnlcenter.add(btnAgrega);
  7.        }
  8.        pnlcenter.setLayout(new FlowLayout());
  9.        pnlcenter.setBorder(BorderFactory.createLineBorder(Color.black));
  10.        pnlcenter.setBounds(0, 0, 200, 200);
  11.        pnlcenter.setPreferredSize(new Dimension(2000, 2000));
  12.        pnlcenter.setVisible(true);
  13.  
  14.        final JScrollPane scroller = new JScrollPane(pnlcenter, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  15.        scroller.setViewportView(pnlcenter);
  16.  
  17.        this.setSize(new Dimension(400, 400));
  18.        this.add(scroller);
  19.        this.pack();
  20.        this.setLocationRelativeTo(null);
  21.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.        this.setVisible(true);
  23. }

Muchas gracias !!!!


que hace el metodo initComponents ?  >:D , intenta colaborar a la primera un poco mas para que algo tan simple no se nos haga eterno LMAOOO
En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
GSecurity

Desconectado Desconectado

Mensajes: 9


Ver Perfil
Re: Agregar Scrollbar a JPanel
« Respuesta #6 en: 21 Marzo 2020, 06:12 am »


que hace el metodo initComponents ?  >:D , intenta colaborar a la primera un poco mas para que algo tan simple no se nos haga eterno LMAOOO


El initComponent es para que inicie el JFrame.... no entiendo cual es el problema?  :(  Help Me
En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: Agregar Scrollbar a JPanel
« Respuesta #7 en: 21 Marzo 2020, 13:08 pm »

El initComponent es para que inicie el JFrame.... no entiendo cual es el problema?  :(  Help Me

Ayuda tu mas con el código, pasa mas código, seguro algo falta por ahí y de paso simple dog.
En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
GSecurity

Desconectado Desconectado

Mensajes: 9


Ver Perfil
Re: Agregar Scrollbar a JPanel
« Respuesta #8 en: 21 Marzo 2020, 20:15 pm »

Ayuda tu mas con el código, pasa mas código, seguro algo falta por ahí y de paso simple dog.

Pero es todo el codigo que tengo  :( porque no me creen:

Con este codigo hago que aparescan una serie de botones dentro de un jpanel.

 
Código
  1.   public frmTest() {
  2.        initComponents();
  3.  
  4.        JPanel pnlcenter = new JPanel();
  5.        for (int i = 0; i < 50; i++) {
  6.            JButton btnAgrega = new JButton("Hola");
  7.            pnlcenter.add(btnAgrega);
  8.        }
  9.        pnlcenter.setLayout(new FlowLayout());
  10.        pnlcenter.setBorder(BorderFactory.createLineBorder(Color.black));
  11.        pnlcenter.setBounds(0, 0, 200, 200);
  12.        pnlcenter.setPreferredSize(new Dimension(2000, 2000));
  13.        pnlcenter.setVisible(true);
  14.  
  15.        this.setSize(new Dimension(400, 400));
  16.        this.add(pnlcenter);
  17.    }

La idea es que me aparezca un scrollbar en la parte de la izquierda ya que mi intencion es agregar mas controles dentro del panel. Guiandome por internet agregue el siguiente codigo el cual me dicen que deberia funcionar pero nada  :( :


 
Código
  1.   public frmTest() {
  2.        initComponents();
  3.  
  4.        JPanel pnlcenter = new JPanel();
  5.        for (int i = 0; i < 50; i++) {
  6.            JButton btnAgrega = new JButton("Hola");
  7.            pnlcenter.add(btnAgrega);
  8.        }
  9.        pnlcenter.setLayout(new FlowLayout());
  10.        pnlcenter.setBorder(BorderFactory.createLineBorder(Color.black));
  11.        pnlcenter.setBounds(0, 0, 200, 200);
  12.        pnlcenter.setPreferredSize(new Dimension(2000, 2000));
  13.        pnlcenter.setVisible(true);
  14.  
  15.        final JScrollPane scroller = new JScrollPane(pnlcenter, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  16.        scroller.setViewportView(pnlcenter);
  17.        this.setSize(new Dimension(400, 400));
  18.        this.add(scroller);
  19.        this.pack();
  20.        this.setLocationRelativeTo(null);
  21.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.        this.setVisible(true);
  23.    }

El JFrame lo he creado desde la paleta del netbeans, es todo lo que tengo.

Help me !!
En línea

EdePC
Moderador Global
***
Desconectado Desconectado

Mensajes: 2.065



Ver Perfil
Re: Agregar Scrollbar a JPanel
« Respuesta #9 en: 22 Marzo 2020, 04:23 am »

- A mí si que me funciona. No muestras tu main ni tampoco initComponents así que el problema está ahí. En mi caso el initComponents me da problemas por lo que lo he comentado:

Código
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class frmTest extends JFrame {
  5.  
  6.  public frmTest() {
  7.    //initComponents();
  8.  
  9.    JPanel pnlcenter = new JPanel();
  10.    for (int i = 0; i < 50; i++) {
  11.      JButton btnAgrega = new JButton("Hola");
  12.      pnlcenter.add(btnAgrega);
  13.    }
  14.    pnlcenter.setLayout(new FlowLayout());
  15.    pnlcenter.setBorder(BorderFactory.createLineBorder(Color.black));
  16.    pnlcenter.setBounds(0, 0, 200, 200);
  17.    pnlcenter.setPreferredSize(new Dimension(2000, 2000));
  18.    pnlcenter.setVisible(true);
  19.  
  20.    final JScrollPane scroller = new JScrollPane(pnlcenter, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  21.    scroller.setViewportView(pnlcenter);
  22.    this.setSize(new Dimension(400, 400));
  23.    this.add(scroller);
  24.    this.pack();
  25.    this.setLocationRelativeTo(null);
  26.    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.    this.setVisible(true);
  28.  }
  29.  
  30.  public static void main(String[] args) {
  31.    frmTest frm = new frmTest();
  32.  }
  33. }

- El código anterior funciona incluso sin usar NetBeans, desde un block de notas y compilando a mano con javac frmTest.java && java frmTest

- No te manda ningún error o advertencia? has hecho uno Click derecho > Clean a tu proyecto?
En línea

Páginas: [1] 2 Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Agregar imagen a JPanel
Java
gasoft 4 18,557 Último mensaje 30 Octubre 2009, 06:47 am
por gasoft
Redimensionar tamaño JPanel dentro de otro JPanel [SOLUCIONADO]
Java
pisagatos 3 21,484 Último mensaje 11 Octubre 2012, 04:16 am
por Gianko
Agregar ScrollBar personalizado a un Panel C#
.NET (C#, VB.NET, ASP)
Krähne 6 8,528 Último mensaje 18 Abril 2011, 20:35 pm
por _katze_
Duda sobre uso de GridLayout y agregar botones a JPanel
Java
Diego00 1 4,111 Último mensaje 9 Septiembre 2014, 22:18 pm
por lexoazul
Mover Panel que tiene ScrollBar con las teclas, y que se actualice el ScrollBar
.NET (C#, VB.NET, ASP)
solesvia 0 2,156 Último mensaje 11 Septiembre 2015, 04:54 am
por solesvia
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines