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 117 118 119 120 121
|
package test;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.JButton;
public class TestTableCouleurs extends JDialog
{
private static final long serialVersionUID = 1L;
private static Object[] columnName = {"Nom", "âge"};
private static Object[][] data = {{"Jean Dupond", "18"}, {"Pierre_François Du Verrou de la Targette", "22"}, {"Jacques Durand", "36"}};
private final JTable table;
// private DefaultTableModel model = null;
TestTableCouleurs()
{
getContentPane().setLayout(null);
setSize(338,213);
setTitle("Test de coloriage des cellules");
table = new JTable(data, columnName);
table.setDefaultRenderer(String.class, new CustomRenderer());
table.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e) {colorier(e);}
public void mouseReleased(MouseEvent e) {colorier(e);}
});
JScrollPane Scroll = new JScrollPane(table );
Scroll.setBounds(10,25,300,76);
getContentPane().add(Scroll);
TableColumnModel colModel = table.getColumnModel();
double factor = Scroll.getWidth();;
double[] pourCent = {0.8,0.2};
for (int i = 0; i < pourCent.length; i++)
{
TableColumn column = colModel.getColumn(i);
column.setPreferredWidth((int) (pourCent[i] * factor));
}
JButton Terminer = new JButton("Terminer");
Terminer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
System.exit(0);
}
});
Terminer.setBounds(110, 122, 89, 23);
getContentPane().add(Terminer);
setLocationRelativeTo(null);
/*
int nbCol = data[0].length; // Nombre de colonnes
for (int i=0; i<nbCol ;i++)
table.getColumnModel().getColumn(i).setCellRenderer(new CustomRenderer());//use custom cell rendered for col 1
*/
setVisible(true);
}
void colorier(MouseEvent e)
{
int row = table.rowAtPoint(e.getPoint());
int column = table.columnAtPoint(e.getPoint());
System.out.println( " OK " );
CustomRenderer.goOn = true;
TableModel model = table.getModel();
String cellVal = (String) table.getValueAt(row , column);
model.setValueAt(1, row , column);
table.setValueAt(cellVal,row , column);
}
JTable getTable() {
return table;
}
static class CustomRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 1L;
private static boolean goOn = false;
public CustomRenderer()
{
super.setOpaque(true);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column)
{
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
System.out.println(goOn + " " + row + " " + column);
if (goOn)
{
c.setBackground(Color.CYAN);
System.out.println( "Changé " );
}
return c;
}
}
public static void main(String[] args)
{
new TestTableCouleurs();
}
} |
Partager