Tu peus essayer ceci:
/* * TableWithFilecooser.java */ import java.awt.*; import java.io.*; import javax.swing.*; import javax.swing.table.*; public class TableWithFilecooser extends JFrame { private JTable table; private String COL1 = "Chemin de l'image"; private String COL2 = "Personnel"; public TableWithFilecooser() { super("TableWithCheckbox"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(400, 300); setLocationRelativeTo(null); table = new JTable(); table.setModel(new DefaultTableModel( new Object [][] { {"1", new Boolean(false)}, {"2", new Boolean(false)}, {"3", new Boolean(false)}, {"4", new Boolean(false)} }, new String [] { COL1, COL2 } ) { Class[] types = new Class [] { String.class, Boolean.class };
public Class getColumnClass(final int columnIndex) { return types [columnIndex]; } }); getContentPane().add(new JScrollPane(table), BorderLayout.CENTER); table.getColumn(COL1).setCellEditor(new FilechooserEditor()); } public static void main(final String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { new TableWithFilecooser().setVisible(true); } }); } private class FilechooserEditor extends DefaultCellEditor { private JFileChooser fileChooser; private JTextField tf; private String value; public FilechooserEditor() { super(new JTextField()); tf = (JTextField) getComponent(); fileChooser = new JFileChooser(); } @Override public Component getTableCellEditorComponent(final JTable table, final Object value, final boolean isSelected, final int row, final int column) { switch (fileChooser.showOpenDialog(null)) { case JFileChooser.APPROVE_OPTION: final File file = fileChooser.getSelectedFile(); System.out.println(SwingUtilities.isEventDispatchThread()); tf.setText(file.getAbsolutePath()); break; case JFileChooser.CANCEL_OPTION: break; } SwingUtilities.invokeLater(new Runnable() { @Override public void run() { stopCellEditing(); } }); return tf; } @Override public Object getCellEditorValue() { value = tf.getText(); return value; } } }
|