1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| private JTable getTable() {
if (table == null) {
String[] nom_col = {"Type du matériel", "Adresse IP", "Fichier de configuration", ""};
Object[][] data = {{"", "", "", "parcourir"}};
table = new JTable(data,nom_col);
//table.setGridColor(Color.white);
table.setRowHeight(20);
table.setShowHorizontalLines(false);
table.setShowVerticalLines(false);
table.setSelectionBackground(Color.white);
table.setRowSelectionAllowed(false);
//table.setModel(new DefaultTableModel());
table.setShowGrid(true);
JComboBox t=new JComboBox();
//t.setBounds(new Rectangle(38, 247, 109, 24));
t.addItem("MSW");
t.addItem("ICP");
t.addItem("DS");
//t.setSelectedItem("MSW");
TableColumn typemat = table.getColumnModel().getColumn(0);
typemat.setCellEditor(new DefaultCellEditor(t));
ip_txt=getIp_txt();
TableColumn ipp = table.getColumnModel().getColumn(1);
ipp.setCellEditor(new DefaultCellEditor(ip_txt));
fichh=new JTextField();
//fichh.setBounds(new Rectangle(318, 245, 142, 23));
TableColumn fich = table.getColumnModel().getColumn(2);
fich.setCellEditor(new DefaultCellEditor(fichh));
table.getColumn("").setCellRenderer(new ButtonRenderer());
table.getColumn("").setCellEditor(new ButtonEditor(new JCheckBox()));
}
return table;
}
public class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else{
setForeground(table.getForeground());
setBackground(UIManager.getColor("Button.background"));
}
setText( (value ==null) ? "" : value.toString() );
return this;
}
}
public class ButtonEditor extends DefaultCellEditor {
public JButton button;
public String label;
public boolean isPushed;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();//création dun nouveau filechosser
chooser.setApproveButtonText("Valider"); //intitulé du bouton
chooser.showOpenDialog(null); //affiche la boite de dialogue
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
fichh.setText(chooser.getSelectedFile().getAbsolutePath()); //si un fichier est selectionné, récupérer le fichier puis sont path et l'afficher dans le champs de texte
}
fireEditingStopped();
}
});
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (isSelected) {
button.setForeground(table.getSelectionForeground());
button.setBackground(table.getSelectionBackground());
} else{
button.setForeground(table.getForeground());
button.setBackground(table.getBackground());
}
label = (value ==null) ? "" : value.toString();
button.setText( label );
isPushed = true;
return button;
}
public Object getCellEditorValue() {
if (isPushed) {
/*JFileChooser chooser = new JFileChooser();//création dun nouveau filechosser
chooser.setApproveButtonText("Valider"); //intitulé du bouton
chooser.showOpenDialog(null); //affiche la boite de dialogue
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
fichh.setText(chooser.getSelectedFile().getAbsolutePath()); //si un fichier est selectionné, récupérer le fichier puis sont path et l'afficher dans le champs de texte
}*/
}
isPushed = false;
return new String( label ) ;
}
public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}
protected void fireEditingStopped() {
super.fireEditingStopped();
}
} |
Partager