Foro de elhacker.net

Programación => Java => Mensaje iniciado por: miguelsora en 1 Junio 2013, 22:13 pm



Título: ayuda como pasar informacion de un texto a un JTable
Publicado por: miguelsora 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


Título: Re: ayuda como pasar informacion de un texto a un JTable
Publicado por: Mitgus 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.