Search code examples
javaswingjtablejtextfield

How to insert JtextField value to a Jtable?


 public LayoutWindow() {
    JLabel lblNewLabel = new JLabel("Receipt No:");

    txtReceiptNo = new JTextField();
    txtReceiptNo.setColumns(10);

    JLabel lblDate = new JLabel("Date:");

    txtDate = new JTextField();
    txtDate.setColumns(10);
    Date date = new Date();
    SimpleDateFormat dateFormate = new SimpleDateFormat("dd-MM-yyyy");
    String newDate = dateFormate.format(date);
    txtDate.setText(newDate);


    JLabel lblProductCode = new JLabel("Product Code");

    txtProductCode = new JTextField();
    txtProductCode.setColumns(10);
    String a = txtProductCode.getText();
    txtProductCode.getDocument().addDocumentListener(new DocumentListener() {
      public void changedUpdate(DocumentEvent e) {
        warn();
      }
      public void removeUpdate(DocumentEvent e) {
        warn();
      }
      public void insertUpdate(DocumentEvent e) {
        warn();
      }

      public void warn() {
         System.out.println("changed....");
         txtQuantity.setText("1");


      }
    });

    JLabel lblQuantity = new JLabel("Quantity");

    txtQuantity = new JTextField();
    txtQuantity.setColumns(10);

    JLabel lblPrice = new JLabel("Price");

    txtPrice = new JTextField();
    txtPrice.setColumns(10);

    JLabel lblNetAmount = new JLabel("Net Amount");

    txtNetAmount = new JTextField();
    txtNetAmount.setColumns(10);

    JLabel lblDiscount = new JLabel("Discount");

    txtDiscount = new JTextField();
    txtDiscount.setColumns(10);

    JLabel lblTotal = new JLabel("Total");

    txtTotal = new JTextField();
    txtTotal.setColumns(10);



    System.out.println(a);
    String[] columnNames = {"Product Code","Description","Price","Quantity","Total"};
    Object[][] data = {{a, "bb","cc", new Integer(5), new Boolean(false)},};

    final JTable table = new JTable(data, columnNames);

    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    if (DEBUG) {
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                printDebugData(table);
            }
        });
    }

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

I have a code like above. I want to insert data into Jtable which is comes from Jtextfield. And I also want to clear JtextField data onFocus. How can I do that? Plz help me soon... thanx...


Solution

  • JTable table = new JTable(data, columnNames) constructs a DefaultTableModel

    You can add a new row into a DefaultTableModel using DefaultTableModel#addRow(Object[]) or DefaultTableModel#addRow(Vector).

    Under your example that would require something like ((DefaultTableModel)table.getModel).addRow(...)

    To set the value of an already existing row, you can use the TableModel#setValueAt(row, col) method.

    This would be a simple as table.getModel().setValueAt(row, col), where row and col are int values.

    You may need to convert the row index from the view to the model via JTable#convertRowIndexToModel(int) and the column index to the model as well using Table#convertColumnIndexToModel(int) in case the rows have being sorted or the columns moved by the user

    At lot of this is covered in How to use Tables, well worth your time

    To clear a text field on focus gained, you will need a FocusListener and attach it to each field you want to enable.

    Something like...

    public class ClearOnFocusGained implements FocusListener {
        public void focusGained(FocusEvent e) {
            // This is an assumption...
            ((JTextComponent)e.getComponent()).setText(null);
        }
        public void focusLost(FocusEvent e) {}
    }
    

    Then you would simply create an instance of the listener and apply to each of your fields

    ClearOnFocusGained clearListener = new ClearOnFocusGained();
    txtTotal.addFocusListener(clearListener);