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
public void actionPerformed(ActionEvent evt)
{
String[] nombres_columnas={"","","","","","".....};
Object [][] data = {
{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);
JScrollPane scrollPane=new JScrollPane(ventana2);
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:
new Integer(tuDatoEntero)
double:
new Double(tuDatoReal)
boolean:
new Boolean(tuDatoLogico)
String:
tuDato
Si quieres crear una JTable en otro marco:
public class TablaVentas extends JFrame {
public TablaVentas(Object[][] data, String[] columnas) {
super("Información de venta");
//Creacion de la tabla
final JTable table = new JTable(data, columnas);
table.setPreferredScrollableViewportSize(new Dimension(500, 80));
//Creamos un scrollpanel y se lo agregamos a la tabla
JScrollPane scrollpane = new JScrollPane(table);
//Agregamos el scrollpanel al contenedor
getContentPane().add(scrollpane, BorderLayout.CENTER);
} // fin constructor
} // fin clase
Entonces el código del evento button sería:
public void actionPerformed(ActionEvent evt)
{
String[] nombres_columnas={"","","","","","".....};
Object [][] data = {
{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.