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

 

 


Tema destacado: Sigue las noticias más importantes de seguridad informática en el Twitter! de elhacker.NET


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  ayuda como pasar informacion de un texto a un JTable
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: ayuda como pasar informacion de un texto a un JTable  (Leído 5,627 veces)
miguelsora

Desconectado Desconectado

Mensajes: 10


Ver Perfil
ayuda como pasar informacion de un texto a un JTable
« en: 1 Junio 2013, 22:13 pm »

Hola compañeros solamente quiero saves como pasar de un texto a un Jtable desde un boton este es mi codigo que tengo mieren

Código:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;


public class ventana2 extends JFrame implements ActionListener  {

private JLabel hp,pr,poh,pre,fech;
private JTextField texto,texto3;
private JLabel cantidad;
private JTextField texto2,texto4;
private JButton bt;
private JLabel etiqueta;
  private String HP="";
    JTable ventana2;
   
public ventana2(){
super("ESTADISTICAS DEL PH");
this.setSize(750,500);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
setLayout(new FlowLayout());
   cantidad=new JLabel("Nombre producto");
   texto=new JTextField(50);
   hp=new JLabel( "         Tamañp del producto");
   texto2=new JTextField(50);
   texto3=new JTextField(58);
   texto4=new JTextField(58);
   pre=new JLabel("         Precio");
   fech=new JLabel("     Fecha");               
   bt=new JButton("                                                                              Agregar Datos                                                                                   ");                                                                                   
   bt.setBackground(Color.gray);
   add(cantidad);
   add(texto);
   add(hp);
   add(texto2);
   add(pre);
   add(texto3);
   add(fech);
   add(texto4);
   add(bt);
   bt.addActionListener(this);
   this.setVisible(true);
   String[]columnNames={"PH","Cloro","cantidad","Fecha"};
   
    Object [][] data = {
       
       
    {"","","",""},
    {"","","",""},
    {"","","",""},
    {"","","",""},

    };
            {
    ventana2=new JTable(data,columnNames);
    ventana2.setPreferredScrollableViewportSize(new Dimension (700,250));
    ventana2.setFillsViewportHeight(true);
   
    JScrollPane scrollPane=new JScrollPane(ventana2);
    add(scrollPane);
   
            }

}

    @Override
    public void actionPerformed(ActionEvent e) {

         
   
   
    }}
    }

necesito su ayuda gracias compañeros


En línea

Mitgus

Desconectado Desconectado

Mensajes: 63


Programming Lover


Ver Perfil
Re: ayuda como pasar informacion de un texto a un JTable
« Respuesta #1 en: 8 Junio 2013, 00:41 am »

Código
  1.  
  2. public void actionPerformed(ActionEvent evt)
  3. {
  4.  String[] nombres_columnas={"","","","","","".....};
  5.   Object [][] data = {
  6.  
  7.  
  8.    {tuDato,tuDato,tuDato,tuDato},
  9.    {tuDato,tuDato,tuDato,tuDato},
  10.    {tuDato,tuDato,tuDato,tuDato},
  11.    {tuDato,tuDato,tuDato,tuDato},
  12.  
  13.    };
  14.  
  15.   ventana2 = new JTable(data,nombres_columnas);
  16.   ventana2.setPreferredScrollableViewportSize(new Dimension (700,250));
  17.   ventana2.setFillsViewportHeight(true);
  18.  
  19.    JScrollPane scrollPane=new JScrollPane(ventana2);
  20.    add(scrollPane);
  21.  
  22.  } // fin actionPerformed


Te recomiendo uses paneles y layouts ^^

Código
  1. tuDato = el dato que deseas pasar. Recuerda que si es entero, se pasa de la siguiente manera:
  2.  
  3. new Integer(tuDatoEntero)
  4.  
  5. double:
  6.  
  7. new Double(tuDatoReal)
  8.  
  9. boolean:
  10.  
  11. new Boolean(tuDatoLogico)
  12.  
  13.  
  14. tuDato
  15.  

Si quieres crear una JTable en otro marco:

Código
  1. public class TablaVentas extends JFrame {
  2.  
  3. public TablaVentas(Object[][] data, String[] columnas) {
  4. super("Información de venta");
  5.  
  6.  
  7. //Creacion de la tabla
  8. final JTable table = new JTable(data, columnas);
  9. table.setPreferredScrollableViewportSize(new Dimension(500, 80));
  10.  
  11. //Creamos un scrollpanel y se lo agregamos a la tabla
  12. JScrollPane scrollpane = new JScrollPane(table);
  13.  
  14. //Agregamos el scrollpanel al contenedor
  15. getContentPane().add(scrollpane, BorderLayout.CENTER);
  16.  
  17.  
  18.  
  19. } // fin constructor
  20.  
  21. } // fin clase
  22.  

Entonces el código del evento button sería:

Código
  1. public void actionPerformed(ActionEvent evt)
  2. {
  3.  String[] nombres_columnas={"","","","","","".....};
  4.   Object [][] data = {
  5.  
  6.  
  7.    {tuDato,tuDato,tuDato,tuDato},
  8.    {tuDato,tuDato,tuDato,tuDato},
  9.    {tuDato,tuDato,tuDato,tuDato},
  10.    {tuDato,tuDato,tuDato,tuDato},
  11.  
  12.       // la informacion procesada se envia a la tabla
  13.        TablaVentas miTabla = new TablaVentas(datos,nombres_columnas);
  14.  
  15.  
  16.        miTabla.pack();
  17.        miTabla.setVisible(true);
  18.  
  19.      } // fin metodo actionPerformed
  20.  
  21.  


Saludos.


« Última modificación: 8 Junio 2013, 00:54 am por Migugami » En línea

Linux User #560388
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
JTable tamaño de celdas ajustable y mostrar texto completo
Java
marvic 1 29,343 Último mensaje 12 Abril 2009, 19:57 pm
por marvic
Como pasar un registro a una cadena de texto?
ASM
danielo- 2 8,872 Último mensaje 1 Diciembre 2010, 01:59 am
por danielo-
JTable, leer informacion editada por el usuario
Java
Xedrox 5 11,108 Último mensaje 8 Marzo 2011, 02:26 am
por Xedrox
filtrar Texto en un JTable conectado a un BD con un JButton y un JTextField?
Java
hack-4-life 2 8,833 Último mensaje 21 Marzo 2011, 04:35 am
por hack-4-life
como pasar un resulset a un jcombobox dentro de jtable?
Java
hack-4-life 1 6,840 Último mensaje 20 Mayo 2011, 23:55 pm
por sapito169
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines