2009-11-25 3 views

Répondre

5

Les tables Java utilisent souvent l'interface TableModel pour le stockage.

Vous pouvez obtenir la valeur particulière via:

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

Plus sur ce point: Sun's Swing Table Tutorial

-1

Il y a tout ce dont vous avez besoin here!

0

Vous devez passer par TableModel du JTable, accessible par la méthode getModel(). Cela a toutes les informations dont vous avez besoin.

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(); 
    } 
} 
Questions connexes