Título: llenar una jtable usando un jtextfield Publicado por: AyitaKuro en 21 Febrero 2014, 05:40 am Hola soy nueva en esto y nesesito ayuda, es pasar lo que escribi en el jtextfiel a mi tabla y viseversa aqui el codigo que tengo hasta el momento:
import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JCheckBox; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.*; import java.awt.event.*; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JCheckBox; import javax.swing.JLabel; import javax.swing.*; import java.awt.*; class TableSortDemo extends JPanel { private boolean DEBUG = false; private JPanel panel, panel1, panel2; private JLabel Label1, Label2; private JTextField text1, text2; private JCheckBox box1; private JButton Button1, Button2, Button3; public TableSortDemo() { super(new GridLayout(1,0)); panel = new JPanel(); panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); add(panel, BorderLayout.PAGE_START); panel1 = new JPanel(); panel1.setLayout(new GridLayout(4, 2, 10, 10)); panel.add(panel1); Label1 = new JLabel("Codigo"); Label2 = new JLabel("Producto"); text1 = new JTextField(10); text2 = new JTextField(10); box1 = new JCheckBox(); JTable table = new JTable(new MyTableModel()); table.setPreferredScrollableViewportSize(new Dimension(500, 150)); table.setFillsViewportHeight(true); table.setAutoCreateRowSorter(true); JScrollPane scrollPane = new JScrollPane(table); panel.add(scrollPane) ; Button1 = new JButton("Nuevo"); Button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ String Codigo = text1.getText(); String Nombre = text2.getText(); } }); Button2 = new JButton("Modificar"); Button2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ } }); Button3 = new JButton("Eliminar"); Button3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ } }); panel1.add(Label1); panel1.add(text1); panel1.add(Label2); panel1.add(text2); panel1.add(box1); panel1.add(Button1); panel1.add(Button2); panel1.add(Button3); } class MyTableModel extends AbstractTableModel { private String[] columnNames = {"CODIGO", "PRODUCTO", "DISPONIBLE"}; private Object[][] data = { {"1", "Arroz",new Boolean(false)}, {"2", "Carne", new Boolean(true)}, {"3", "Pollo", new Boolean(false)}, {"4", "Fideo", new Boolean(true)}, {"5", "Atun", new Boolean(false)} }; public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.length; } public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { return data[row][col]; } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public boolean isCellEditable(int row, int col) { if (col < 2) { return false; } else { return true; } } public void setValueAt(Object value, int row, int col) { if (DEBUG) { System.out.println("Setting value at " + row + "," + col + " to " + value + " (an instance of " + value.getClass() + ")"); } data[row][col] = value; if (DEBUG) { System.out.println("New value of data:"); printDebugData(); } } private void printDebugData() { int numRows = getRowCount(); int numCols = getColumnCount(); for (int i=0; i < numRows; i++) { System.out.print(" row " + i + ":"); for (int j=0; j < numCols; j++) { System.out.print(" " + data[j]); } System.out.println(); } System.out.println("--------------------------"); } } public void createAndShowGUI4(){ } private static void createAndShowGUI() { JFrame frame = new JFrame("TableExample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TableSortDemo newContentPane = new TableSortDemo(); newContentPane.setOpaque(true); frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); frame.setSize(600,400); frame.setLocationRelativeTo(null); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } nesesito ayuda x favor... Título: Re: llenar una jtable usando un jtextfield Publicado por: ThinkByYourself en 21 Febrero 2014, 10:14 am Los siguientes métodos peuden serte de ayuda:
JTextField.getText(): consigues el String del contenido del jtextfield. JTable.setValueAt(...): te cambia el valor de la celda de una tabla por el que le digas tú. No recuerdo el orden de los parámetros exactamente, pero vaya, algo del tipo: (x,y,nuevoValor). En la documentación oficial encontrarás información certera. Título: Re: llenar una jtable usando un jtextfield Publicado por: Yaldabaot en 22 Febrero 2014, 19:05 pm http://jedicerocool.blogspot.com/2012/10/como-agregar-y-eliminar-filas-de-un.html#.Uwjmzc4VXIV
Sólo cambia los valores que contiene el array objeto por los jTextField. Título: Re: llenar una jtable usando un jtextfield Publicado por: adrianmansilla en 23 Febrero 2014, 14:51 pm hola AyitaKuro, estaba leyendo tu problema y si quieres agregar filas al la tabla tienes que trabajar con el modelo, en el evento del boton nuevo tendrias que poner el siguiente codigo:
Vector unafila=new Vector();//utilizamos un vector para agregar una fila a tu tabla unafila.add(codigo); unafila.add(nombre); if(box1.isSelected){ unafila.add(true); }else{ unafila.add(false); } MyTableModel().addRow(unafila);//agregamos la fila que acabamos de crear al modelo, que este automatiamente agregara a la tabla Puede que falle la sintaxis, pero esa es la idea para llenar tu tabla con tus textField, Saludos Título: Re: llenar una jtable usando un jtextfield Publicado por: AyitaKuro en 23 Febrero 2014, 22:19 pm Muuchas Gracias me ayudaron muchísimo :* de verdad les agradezco esto es para un examen.
|