Java 如何将JtextField值插入Jtable?

Java 如何将JtextField值插入Jtable?,java,swing,jtable,jtextfield,Java,Swing,Jtable,Jtextfield,我有一个类似上面的代码。我想将数据插入来自Jtextfield的Jtable。 我还想清除JtextField dataonFocus。我该怎么做? 请尽快帮我。。。thanx…JTable table=新的JTable(数据、列名)构造一个DefaultTableModel 您可以使用或将新行添加到DefaultTableModel中 在您的示例中,需要类似于((DefaultTableModel)table.getModel).addRow(…) 要设置现有行的值,可以使用TableMode

我有一个类似上面的代码。我想将数据插入来自Jtextfield的Jtable。 我还想清除JtextField data
onFocus
。我该怎么做?
请尽快帮我。。。thanx…

JTable table=新的JTable(数据、列名)
构造一个
DefaultTableModel

您可以使用或将新行添加到
DefaultTableModel

在您的示例中,需要类似于
((DefaultTableModel)table.getModel).addRow(…)

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

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

如果用户对行进行了排序或移动了列,则可能需要通过将行索引从视图转换为模型,将列索引转换为模型

在很多情况下,这是涵盖,很值得你的时间

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

类似于

 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);
然后,您只需创建侦听器的一个实例并应用于每个字段

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

不同意FocusListener,让我们来看看JButtons,不管怎样,@mKorbel你建议OP如何实现在获得焦点时清除文本字段的能力?我不会起诉
FocusListener
,要创建
JButton“clear me”
,我对你答案中的代码没有任何问题,只要使用OPs逻辑来清除
focusGained()
@mKorbel上的任何内容,我总是对替代方法感兴趣,这就是为什么要问的原因。不过说得好
ClearOnFocusGained clearListener = new ClearOnFocusGained();
txtTotal.addFocusListener(clearListener);