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

上記のようなコードがあります。Jtextfield から取得した Jtable にデータを挿入したいと考えています。また、 JtextField data もクリアしたいと思いますonFocus。どうやってやるの?すぐに助けてください...ありがとう...

4

1 に答える 1

3

JTable table = new JTable(data, columnNames)を構築しますDefaultTableModel

またはDefaultTableModelを使用して新しい行を に追加できます。DefaultTableModel#addRow(Object[])DefaultTableModel#addRow(Vector)

あなたの例では、次のようなものが必要です((DefaultTableModel)table.getModel).addRow(...)

既存の行の値を設定するには、TableModel#setValueAt(row, col)メソッドを使用できます。

これは単純な astable.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);
于 2012-10-19T04:45:36.417 に答える