2012-10-19 74 views
-2
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數據onFocus。我怎樣才能做到這一點? PLZ幫我很快...感謝名單...如何將JtextField值插入Jtable?

回答

2

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),其中行和列是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); 
+0

與不同意的FocusListener,讓對Jbutton將什麼, – mKorbel

+0

@ mKorbel你如何建議OP能夠清除焦點上的文本字段? – MadProgrammer

+0

我不會起訴'FocusListener',要創建'JButton「Clear me」',我沒有任何關於代碼的問題,只需用OPs邏輯清除'focusGained()' – mKorbel