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 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
{ String[] nombres_columnas ={"", "", "", "", "", ""..... }; {tuDato,tuDato,tuDato,tuDato}, {tuDato,tuDato,tuDato,tuDato}, {tuDato,tuDato,tuDato,tuDato}, {tuDato,tuDato,tuDato,tuDato}, }; ventana2 = new JTable(data,nombres_columnas ); ventana2. setPreferredScrollableViewportSize(new Dimension (700, 250)); ventana2.setFillsViewportHeight(true); add(scrollPane); } // fin actionPerformed
Te recomiendo uses paneles y layouts ^^ tuDato = el dato que deseas pasar. Recuerda que si es entero, se pasa de la siguiente manera: double: boolean: tuDato
Si quieres crear una JTable en otro marco: public class TablaVentas extends JFrame { super("Información de venta"); //Creacion de la tabla table. setPreferredScrollableViewportSize(new Dimension(500, 80)); //Creamos un scrollpanel y se lo agregamos a la tabla //Agregamos el scrollpanel al contenedor } // fin constructor } // fin clase
Entonces el código del evento button sería: { String[] nombres_columnas ={"", "", "", "", "", ""..... }; {tuDato,tuDato,tuDato,tuDato}, {tuDato,tuDato,tuDato,tuDato}, {tuDato,tuDato,tuDato,tuDato}, {tuDato,tuDato,tuDato,tuDato}, // la informacion procesada se envia a la tabla TablaVentas miTabla = new TablaVentas(datos,nombres_columnas); miTabla.pack(); miTabla.setVisible(true); } // fin metodo actionPerformed
Saludos.
|