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...
|