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

 

 


Tema destacado: Entrar al Canal Oficial Telegram de elhacker.net


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Agregar array de checkbox a JDialog
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Agregar array de checkbox a JDialog  (Leído 5,051 veces)
leogtz
. . .. ... ..... ........ ............. .....................
Colaborador
***
Desconectado Desconectado

Mensajes: 3.069


/^$/


Ver Perfil WWW
Agregar array de checkbox a JDialog
« en: 14 Abril 2011, 00:52 am »

Hola a todos, estoy teniendo un problema, desde un JFrame, abro un JDialog, y trato de agregarle algunos JCheckBox, pero siempre se queda con el último.

Lo que intento es agregar múltiples JcheckBox a un JDialog, pero que todos se puedan seleccionar, no solo uno.

¿Alguien me puede ayudar?

¿cómo puedo seleccionar varios jcheckbox?


« Última modificación: 14 Abril 2011, 00:54 am por Leo Gutiérrez. » En línea

Código
  1. (( 1 / 0 )) &> /dev/null || {
  2. echo -e "stderrrrrrrrrrrrrrrrrrr";
  3. }
  4.  
http://leonardogtzr.wordpress.com/
leogutierrezramirez@gmail.com
1mpuls0


Desconectado Desconectado

Mensajes: 1.186


Ver Perfil
Re: Agregar array de checkbox a JDialog
« Respuesta #1 en: 16 Abril 2011, 08:49 am »

Hola qué tal.

Veo que nadie contestaba asi que me puse a trabajar un par de horas, pensé que sería mas sencillo o será que ya estoy olvidando xD

Aquí el código.

Código
  1. /**
  2.  * @(#)PruebaJCheckBox .java
  3.  *
  4.  *
  5.  * @author 1mpuls0
  6.  * @version 1.00 2011/4/16
  7.  */
  8.  
  9. package foro;
  10.  
  11. import javax.swing.JLabel;
  12. import javax.swing.WindowConstants;
  13. import javax.swing.JFrame;
  14. import javax.swing.JButton;
  15. import javax.swing.BoxLayout;
  16. import java.awt.event.ActionListener;
  17. import java.awt.event.ActionEvent;
  18. import javax.swing.JCheckBox;
  19. import javax.swing.JLabel;
  20. import java.awt.BorderLayout;
  21. import java.awt.event.ItemListener;
  22. import java.awt.event.ItemEvent;
  23. import javax.swing.JDialog;
  24.  
  25.  
  26. public class PruebaJCheckBox extends JFrame {
  27.  
  28. private JCheckBox jCheckBoxs[];
  29. private JButton jbutton;
  30.  
  31.    public static void main(String args[]) {
  32.        new PruebaJCheckBox();
  33.    }
  34.  
  35.    public PruebaJCheckBox() {
  36.        initComponents();
  37.    }
  38.  
  39.    private void initComponents() {
  40.     jbutton = new JButton();
  41.  
  42.        setTitle("jFrame");
  43.        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44.  
  45.        jbutton.setText("get a number");
  46.        jbutton.addActionListener(new ActionListener(){
  47. public void actionPerformed(ActionEvent ae){
  48. int ncb = Integer.parseInt(javax.swing.JOptionPane.showInputDialog(null, "write a number", "number"));
  49.         jCheckBoxs=new JCheckBox[ncb];
  50. new jDialog(PruebaJCheckBox.this, jCheckBoxs);
  51. }
  52. });
  53.  
  54.        getContentPane().add(jbutton, BorderLayout.SOUTH);
  55.  
  56.        setLocationRelativeTo(null);
  57.        setVisible(true);
  58.        pack();
  59.  
  60.  
  61.    }
  62. }
  63.  
  64. class jDialog extends JDialog{
  65.  
  66. public jDialog(JFrame f, JCheckBox jCheckBoxs[] ) {
  67.  
  68. setTitle("JDialog");
  69. getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
  70.  
  71. /*Ciclo para crear los n JCheckBox y colocarle nombre*/
  72.        for(int i=0; i<jCheckBoxs.length; i++){
  73.         jCheckBoxs[i]=new JCheckBox(""+i);
  74.        }
  75.  
  76.        /*Ciclo para agregar el componente al contenedor(se puede realizar con el anterior)*/
  77.        for(int i=0; i<jCheckBoxs.length; i++){
  78.        getContentPane().add(jCheckBoxs[i]);
  79.  
  80.        }
  81.  
  82.        /*Ciclo para seleccionarlos*/
  83.        for(int i=0; i<jCheckBoxs.length; i++){
  84.        jCheckBoxs[i].setSelected(true);
  85.        }
  86.  
  87.        /*Ciclo para eventos de cada jcheckbox*/
  88.        for(int j=0; j<jCheckBoxs.length; j++){
  89.         jCheckBoxs[j].addItemListener(new ItemListener() {
  90.            public void itemStateChanged(ItemEvent evt) {
  91.             JCheckBox cb = (JCheckBox)evt.getItem();
  92.             System.out.println(cb.getText());
  93.            }
  94.        });
  95.        }
  96.  
  97.    setModal(true);
  98.    pack();
  99.    setLocationRelativeTo(null);
  100.    setVisible(true);
  101. }
  102. }
  103.  


Saludos.


« Última modificación: 3 Septiembre 2015, 18:43 pm por 1mpuls0 » En línea

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

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Como se hace un JDialog
Java
apache_scrc 6 23,982 Último mensaje 20 Diciembre 2007, 17:04 pm
por egyware
agregar string array a JList
Java
brian_e 2 11,234 Último mensaje 22 Abril 2009, 17:46 pm
por brian_e
Llamar a un Jdialog
Java
mapers 1 4,195 Último mensaje 15 Agosto 2011, 10:08 am
por Shell Root
acceder componentes en JDialog | eclipse
Java
leogtz 1 3,121 Último mensaje 6 Julio 2012, 01:48 am
por leogtz
Ventana de JDialog Flota muy feo
Java
soy_nicanor 5 2,488 Último mensaje 7 Mayo 2015, 03:49 am
por PabloPbl
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines