2009-11-25 65 views

回答

5

Java表經常使用TableModel接口進行存儲。

myJTable.getModel().getValueAt(rowIndex, columnIndex); 

更多的是::

您可以通過獲得一個特定值Sun's Swing Table Tutorial

0

你需要去通過JTableTableModel,通過getModel()方法訪問。這有你需要的所有信息。

1
public static void main(String[] args) 
{ 
    try 
    { 
     final JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Container cp = frame.getContentPane(); 
     cp.setLayout(new FlowLayout()); 

     final JTable tbl = new JTable(new String[][]{{"c1r1", "c2r1"}, {"c1r2", "c2r2"}}, new String[]{"col 1", "col 2"}); 

     cp.add(tbl); 
     cp.add(new JButton(new AbstractAction("click") 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       List<String> colValues = new ArrayList<String>(); 

       for (int i = 0; i < tbl.getRowCount(); i++) 
        colValues.add((String) tbl.getValueAt(0, i)); 

       JOptionPane.showMessageDialog(frame, colValues.toString()); 
      } 
     })); 

     frame.pack(); 
     frame.setVisible(true); 
    } 
    catch (Throwable e) 
    { 
     e.printStackTrace(); 
    } 
}