如何将JtextField值插入Jtable?

时间:2012-10-19 04:32:52

标签: java swing jtable jtextfield

 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);

我有一个像上面这样的代码。我想将数据插入Jtable,它来自Jtextfield。 我还想清除JtextField数据onFocus。我怎样才能做到这一点? Plz很快帮助我......感谢...

1 个答案:

答案 0 :(得分:3)

JTable table = new JTable(data, columnNames)构建DefaultTableModel

您可以使用DefaultTableModel#addRow(Object[])DefaultTableModel#addRow(Vector)DefaultTableModel中添加新行。

根据您的示例,需要((DefaultTableModel)table.getModel).addRow(...)

要设置现有行的值,可以使用TableModel#setValueAt(row, col)方法。

这是一个简单的table.getModel().setValueAt(row, col),其中row和col是int值。

您可能需要通过JTable#convertRowIndexToModel(int)将行索引从视图转换为模型,并使用Table#convertColumnIndexToModel(int)将列索引转换为模型,以防行排序或列移动用户

很多内容都在How to use Tables中介绍,非常值得您花时间

要清除获得焦点的文字字段,您需要FocusListener并将其附加到您要启用的每个字段。

像...一样的东西。

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

然后,您只需创建一个侦听器实例并应用于每个字段

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