Foro de elhacker.net

Programación => Java => Mensaje iniciado por: Kerber0 en 16 Octubre 2009, 02:49 am



Título: JTable y Layouts
Publicado por: Kerber0 en 16 Octubre 2009, 02:49 am
Hola gente, yo trabajo sin layouts
Código:
this.setLayout(null)
y quiero trabajar con un JTable, estuve mirando unos codigos y funciona perfecto de esta forma:

Código:
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;


public class  Ventana extends JFrame {
     public  Ventana() {
        super("Ejemplo 1");

        //array bidimencional de objetos con los datos de la tabla
        Object[][] data = {
            {"Mary", "Campione", "Esquiar", new Integer(5), new Boolean(false)},
            {"Lhucas", "Huml", "Patinar", new Integer(3), new Boolean(true)},
            {"Kathya", "Walrath", "Escalar", new Integer(2), new Boolean(false)},
            {"Marcus", "Andrews", "Correr", new Integer(7), new Boolean(true)},
            {"Angela", "Lalth", "Nadar", new Integer(4), new Boolean(false)}
        };

        //array de String's con los títulos de las columnas
        String[] columnNames = {"Nombre", "Apellido", "Pasatiempo",
                                   "Años de Practica", "Soltero(a)"};

        //se crea la Tabla
        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));


        //Creamos un JscrollPane y le agregamos la JTable
        JScrollPane scrollPane = new JScrollPane(table);

        //Agregamos el JScrollPane al contenedor
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        //manejamos la salida
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        Ventana frame = new  Ventana();
        frame.pack();
        frame.setVisible(true);
    }
}

Ahora yo lo pongo del sgte modo:

Código:
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;


public class Ventana extends JFrame
{


    Object[][] data = {
            {"Mary", "Campione", "Esquiar", new Integer(5), new Boolean(false)},
            {"Lhucas", "Huml", "Patinar", new Integer(3), new Boolean(true)},
            {"Kathya", "Walrath", "Escalar", new Integer(2), new Boolean(false)},
            {"Marcus", "Andrews", "Correr", new Integer(7), new Boolean(true)},
            {"Angela", "Lalth", "Nadar", new Integer(4), new Boolean(false)}
        };


        String[] columnNames = {"Nombre", "Apellido", "Pasatiempo",
                                   "Años de Practica", "Soltero(a)"};


JTable table = new JTable(data,columnNames);
JScrollPane scrollPane = new JScrollPane(table);



    public Ventana()
    {


    this.setLayout(null);
        this.getContentPane().add(table);
    table.setBounds(20, 20, 200, 200);
        this.setSize(600,500);
        this.setTitle("Tabla");
        this.show();
       
    }


    public static void main(String []args)
    {
        new Ventana();

    }
}

Y no me muesta los nombres de las columnas, es decir no me muestra  String[] columnNames = {"Nombre", "Apellido", "Pasatiempo",
                                   "Años de Practica", "Soltero(a)"};, los nombres de las columnas.

Por que pasa eso?

Existe alguna otra forma que no sea de trabajar con los layouts que no sea la de indicandole los lugares (setBounds) porque nunca pude agregar un scrollpane.

Tambien lei que conviene utilizar el JTable con DefaultTableModel, pero tambien eh tenido algunos problemas.

Gracias


Título: Re: JTable y Layouts
Publicado por: 1mpuls0 en 16 Octubre 2009, 05:00 am
Hola

Que te parce si del primer ejemplo que tienes agregamos las siguientes lineas de código..

Código:
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;

public class  Ventana extends JFrame {
     public  Ventana() {
        super("Ejemplo 1");

        getContentPane().setLayout(null);//Colocar esquema al contenedor

        //array bidimencional de objetos con los datos de la tabla
        Object[][] data = {
            {"Mary", "Campione", "Esquiar", new Integer(5), new Boolean(false)},
            {"Lhucas", "Huml", "Patinar", new Integer(3), new Boolean(true)},
            {"Kathya", "Walrath", "Escalar", new Integer(2), new Boolean(false)},
            {"Marcus", "Andrews", "Correr", new Integer(7), new Boolean(true)},
            {"Angela", "Lalth", "Nadar", new Integer(4), new Boolean(false)}
        };

        //array de String's con los títulos de las columnas
        String[] columnNames = {"Nombre", "Apellido", "Pasatiempo",
                                   "Años de Practica", "Soltero(a)"};

        //se crea la Tabla
        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));


        //Creamos un JscrollPane y le agregamos la JTable
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setBounds(30,30,500,70);//Colocar posicion y dimension al JScrollPanel
        

        //Agregamos el JScrollPane al contenedor
        getContentPane().add(scrollPane);


        //manejamos la salida
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        Ventana frame = new  Ventana();
        frame.pack();
        frame.setSize(500, 600);//Colocar Tamaño al JFrame
        frame.setVisible(true);
    }
}

Perdon no sé como colocarlas en color, pero son estas..

Código:
getContentPane().setLayout(null);//Colocar esquema al contenedor
scrollPane.setBounds(30,30,500,70);//Colocar posicion y dimension al JScrollPanel
frame.setSize(500, 600);//Colocar Tamaño al JFrame

Espero qeu te sirva

Saludos


Título: Re: JTable y Layouts
Publicado por: 1mpuls0 en 16 Octubre 2009, 05:15 am
Y respecto al código de tu ejemplo que te parece si te ordenas un poquito mas (no te lo tomes a mal) y hacemos lo siguiente

Código:
import java.awt.*;
import javax.swing.*;

public class Ventana extends JFrame
{

    Object[][] data = {
                                {"Mary", "Campione", "Esquiar", new Integer(5), new Boolean(false)},
                                {"Lhucas", "Huml", "Patinar", new Integer(3), new Boolean(true)},
                                {"Kathya", "Walrath", "Escalar", new Integer(2), new Boolean(false)},
                                {"Marcus", "Andrews", "Correr", new Integer(7), new Boolean(true)},
                                {"Angela", "Lalth", "Nadar", new Integer(4), new Boolean(false)}
                              };
   String[] columnNames = {"Nombre", "Apellido", "Pasatiempo",
                                   "Años de Practica", "Soltero(a)"};
   JTable table = new JTable(data,columnNames);


    public static void main(String []args)
    {
        new Ventana();

    }

    public Ventana()
    {
        setTitle("Tabla");
       getContentPane().setLayout(null);

        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setBounds(30,30,500,70);

        getContentPane().add(scrollPane);

        setSize(600,500);
        show();
    }
}

Cualquier duda ya sabes..

Saludos


Título: Re: JTable y Layouts
Publicado por: Kerber0 en 16 Octubre 2009, 09:46 am
Muchisimas gracias!

Perdon por el desorden tratare de ser mas ordenado,

Gracias!